Lets assume you want to do a job a future time or show a notification at a selected time. AlarmManager can help to send an intent in future time. First we need to select date,
Lets create a date in 2050,
For showing notification we need a service.
Add service to Manifest.xml
We will modify AlarmReceiver for sending notification. But first we need to register a alarm.
This will start AlarmReceiver service In 2050, at the moment. So our service need to show a notification.
Lets create a date in 2050,
Calendar calendar = Calendar.getInstance(); calendar.set(Calendar.YEAR, 2050); Date date = calendar.getTime();
For showing notification we need a service.
public class AlarmReceiver extends Service { public AlarmReceiver() { } @Override public IBinder onBind(Intent intent) { } }
Add service to Manifest.xml
<service android:name="com.kozaxinan.example.alarm.AlarmReceiver" android:enabled="true" android:exported="true" ></service>
We will modify AlarmReceiver for sending notification. But first we need to register a alarm.
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); Intent intent = new Intent(this, AlarmReceiver.class);intent.putExtra("Alarm", true); PendingIntent pendingIntent = PendingIntent.getService(this, 0, intent, 0); // Sets an alarm - note this alarm will be lost if the phone is turned off and on again alarmManager.set(AlarmManager.RTC, date.getTime(), pendingIntent);
This will start AlarmReceiver service In 2050, at the moment. So our service need to show a notification.
public class AlarmReceiver extends Service { public AlarmReceiver() { } public static final int NOTIFICATION_ID = 5111; /** * Class for clients to access */ public class ServiceBinder extends Binder { AlarmReceiver getService() { return AlarmReceiver.this; } } @Override public void onCreate() { Log.i("AlarmReceiver", "onCreate()"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.i("LocalService", "Received start id " + startId + ": " + intent); // If this service was started by out AlarmTask intent then we want to show our notification if (intent.getBooleanExtra("Alarm", false)) { showNotification(); } // We don't care if this service is stopped as we have already delivered our notification return START_NOT_STICKY; } @Override public IBinder onBind(Intent intent) { return mBinder; } // This is the object that receives interactions from clients private final IBinder mBinder = new ServiceBinder(); /** * Creates a notification and shows it in the OS drag-down status bar */ private void showNotification() { NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); Intent intent = new Intent(this, FirstScreen.class); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); mBuilder.setSmallIcon(R.drawable.ic_launcher) .setContentTitle("Title") .setAutoCancel(true) .setContentText("Text"); mBuilder.setContentIntent(contentIntent); mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build()); // Stop the service when we are finished stopSelf(); } }
Comments
Post a Comment