Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Alarm extends BroadcastReceiver
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- Toast.makeText(context, "Starting Alarm Manager", Toast.LENGTH_LONG).show(); // For example
- PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
- PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "");
- wl.acquire();
- // Put here YOUR code.
- Toast.makeText(context, "Alarm !!!!!!!!!!", Toast.LENGTH_LONG).show(); // For example
- wl.release();
- }
- public void SetAlarm(Context context)
- {
- Toast.makeText(context, "Setting Alarm Manager", Toast.LENGTH_LONG).show(); // For example
- AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
- Intent i = new Intent(context, Alarm.class);
- PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
- am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 1000 * 60 , pi); // Millisec * Second * Minute
- }
- public void CancelAlarm(Context context)
- {
- Intent intent = new Intent(context, Alarm.class);
- PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
- AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
- alarmManager.cancel(sender);
- }
- public class YourService extends Service
- {
- Alarm alarm = new Alarm();
- public void onCreate()
- {
- super.onCreate();
- Toast.makeText(YourService.this, "Service Created", Toast.LENGTH_LONG).show(); // For example
- }
- public void onStart(Context context,Intent intent, int startId)
- {
- Toast.makeText(YourService.this, "Setting from Service", Toast.LENGTH_LONG).show(); // For example
- alarm.SetAlarm(context);
- }
- @Override
- public IBinder onBind(Intent intent)
- {
- return null;
- }
- }
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- buttonStart = (Button) findViewById(R.id.buttonStart);
- buttonStop = (Button) findViewById(R.id.buttonStop);
- buttonStart.setOnClickListener(this);
- buttonStop.setOnClickListener(this);
- }
- public void onClick(View src) {
- switch (src.getId()) {
- case R.id.buttonStart:
- Toast.makeText(ServicesDemo.this, "Button Pressed", Toast.LENGTH_LONG).show(); // For example
- Log.d(TAG, "onClick: starting srvice");
- startService(new Intent(this, YourService.class));
- break;
- case R.id.buttonStop:
- Log.d(TAG, "onClick: stopping srvice");
- stopService(new Intent(this, YourService.class));
- break;
- }
- }
- <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
- <activity android:name=".ServicesDemo" android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <service android:enabled="true" android:name=".YourService" />
- <receiver android:process=":remote" android:name="Alarm"></receiver>
- </application>
- <uses-sdk android:minSdkVersion="8" />
- <uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
- </manifest>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement