How to create service for date change in android

| | August 6, 2015

My MainActivity

Calendar calendar = Calendar.getInstance();

      calendar.set(Calendar.MONTH, 1);
      calendar.set(Calendar.YEAR, 2015);
      calendar.set(Calendar.DAY_OF_MONTH, 13);

      calendar.set(Calendar.HOUR_OF_DAY, 3);
      calendar.set(Calendar.MINUTE, 48);
      calendar.set(Calendar.SECOND, 10);
      calendar.set(Calendar.AM_PM,Calendar.PM);



      Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
      pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, myIntent,0);

      System.out.println("=== date Set IS ; " + calendar.getTime() + "DCurrent Date : " + new Date());

BroadcasrReceive Class..

Toast.makeText(context, "Receiver", Toast.LENGTH_SHORT).show();
   Intent service1 = new Intent(context, MyAlarmService.class);
   context.startService(service1);

and My Service is

public class MyAlarmService extends Service {

private NotificationManager mManager;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
}

@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

    mManager = (NotificationManager) this.getApplicationContext()
            .getSystemService(
                    this.getApplicationContext().NOTIFICATION_SERVICE);
    Intent intent1 = new Intent(this.getApplicationContext(),
            MainActivity.class);

    Notification notification = new Notification(R.drawable.ic_launcher,
            "This is a test message!", System.currentTimeMillis());
    intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
            | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
            this.getApplicationContext(), 0, intent1,
            PendingIntent.FLAG_UPDATE_CURRENT);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.setLatestEventInfo(this.getApplicationContext(),
            "AlarmManagerDemo", "This is a test message!",
            pendingNotificationIntent);

    mManager.notify(0, notification);
}

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
}

}

My Manifeast File is …

 <service
        android:name="com.example.alarammanagerdemo.MyAlarmService"
        android:enabled="true" />

    <receiver android:name="com.example.alarammanagerdemo.MyReceiver" >
        <intent-filter >
            <action android:name="android.intent.action.DATE_CHANGED"/>
        </intent-filter>
    </receiver>

Leave a Reply