Thursday 22 August 2013

Create Alarm Service for before time

At your main activity

Intent intent = new Intent(context, AlaramService.class);
                        intent.putExtra("startdate", strmysdate);
                        intent.putExtra("rem_hr", setreminderhrs);
                        intent.putExtra("rem_min", setremindermin);
                        intent.putExtra("alarmstart_eventid", myeventid);
                        context.startService(intent);


create service name AlaramService

public class AlaramService extends Service{
   
   
    private static final String tag = AlaramService.class.getSimpleName();
    String taskdate,tasktime,user_id,startdatetime,sdate,edate,upeventid,myhr,mymin,eventidfromactive;
    Calendar calCurrent = Calendar.getInstance();
    UpcomingEventParser upcomingeventparser;
    UpcomingEventSetget upcomingeventsetget;
    List<UpcomingEventSetget> upcomingsetgetlist;
     public static AlarmManager mAlarmManager;
     public static PendingIntent pendingintentbeforemin;
    long hrs,min;
   
    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
      
        try{
          
        if(null != intent.getExtras()){
            if(intent.getExtras().containsKey("startdate")){
                startdatetime = intent.getExtras().getString("startdate");      
            }if(intent.getExtras().containsKey("alarmstart_eventid")){
                upeventid = intent.getExtras().getString("alarmstart_eventid");      
            }if(intent.getExtras().containsKey("rem_hr")){
                myhr = intent.getExtras().getString("rem_hr");      
            }if(intent.getExtras().containsKey("rem_min")){
                mymin = intent.getExtras().getString("rem_min");      
            }
          
            Utility.setSharedKey("startdate", startdatetime, AlaramService.this);
            Utility.setSharedKey("alarmstart_eventid", upeventid, AlaramService.this);
            Utility.setSharedKey("rem_hr", myhr, AlaramService.this);
            Utility.setSharedKey("rem_min", mymin, AlaramService.this);
        }else{
            startdatetime = Utility.getSharedKey("startdate", AlaramService.this);
            upeventid = Utility.getSharedKey("alarmstart_eventid", AlaramService.this);
            myhr = Utility.getSharedKey("rem_hr", AlaramService.this);
            mymin = Utility.getSharedKey("rem_min", AlaramService.this);
          
            if(upeventid == null){
                upeventid = AddEvent.myeventid;
            }if(startdatetime == null){
                startdatetime = AddEvent.strmysdate;
            }if(myhr == null){
                myhr = AddEvent.setreminderhrs;
            }if(mymin == null){
                mymin = AddEvent.setremindermin;
            }
        }
      
        Log.i(tag, "Get Event id.."+upeventid);
        Log.i(tag, "Get Start date.."+startdatetime);
        Log.i(tag, "myhr.."+myhr);
        Log.i(tag, "mymin.."+mymin);
        }catch(Exception e){
            e.printStackTrace();
        }
        new AsyncTask<Void, Void, Void> (){
            @Override
            protected Void doInBackground(Void... params) {
                updateNotofications();  
                return null;
            }
         }.execute();
    }

    private void updateNotofications() {
      
        try{
        String[] minis = null;
        String[] hris = null;
        Log.d(tag, "updateNotofications for start time");
        SimpleDateFormat mDateFormat = new  SimpleDateFormat("MM-dd-yyyy HH:mm");
        if(myhr != null){
            hris = myhr.split(" ");  
            if(hris != null){
                hrs = Integer.parseInt(hris[0].toString()) * 60 * 60 * 1000;  
            }
        }if(mymin != null){
            minis = mymin.split(" ");
            if(minis != null){
                min = Integer.parseInt(minis[0].toString()) * 60 * 1000;  
            }
        }
      
        //long min = 2 * 60 * 1000;
        long oneDay = 24 * 60 * 60 * 60 * 1000;
      
            Calendar calDb = Calendar.getInstance();
            calDb.setTime(mDateFormat.parse(startdatetime));
            long diff = calDb.getTimeInMillis() - calCurrent.getTimeInMillis();
      
            if(diff < oneDay && diff >= 0) {
              
                Intent mIntent = new Intent(this, NotificationService1.class);
                mIntent.putExtra("event_id", upeventid);
                mIntent.putExtra("reminder_min",minis[0].toString());
                mIntent.putExtra("reminder_hr",hris[0].toString());
                mIntent.putExtra("event_from", "start");

                 mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
              
              
                long diffr  = calCurrent.getTimeInMillis() + diff-(hrs+min);
                System.out.println("d is...."+diffr);
          
                long cur = calCurrent.getTimeInMillis();
                System.out.println("calcurrent is..."+cur);
              
                if(cur < diffr)
                {
                    pendingintentbeforemin = PendingIntent.getService(getApplicationContext(), Integer.parseInt(upeventid), mIntent, PendingIntent.FLAG_ONE_SHOT);
                    mAlarmManager.set(AlarmManager.RTC_WAKEUP, calCurrent.getTimeInMillis() + diff-(hrs+min),  pendingintentbeforemin);
                    System.out.println("Alarm set for start " + startdatetime);
                }
            }
          
        }catch(Exception e){
            e.printStackTrace();
        }

        stopSelf();
    }

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

   
}
 


create service name NotificationService1

public class NotificationService1 extends Service{

    //private int upeventid;
    String eventfrom,eventhr,eventmin,activeeventid,upeventid;
    CharSequence contentText,contentTitle;
   
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        System.out.println("NotificationService1 is started");

    }
   
    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
      
        try{
        upeventid = intent.getExtras().getString("event_id");
        eventhr = intent.getExtras().getString("reminder_hr");
        eventmin = intent.getExtras().getString("reminder_min");
        eventfrom = intent.getStringExtra("event_from");
        activeeventid = Utility.getSharedKey("curentevntis", NotificationService1.this);
      
        Log.i("Notification Srevice", "activeeventid.."+activeeventid);
        Log.i("Notification Srevice", "Get hr is.."+eventhr);
        Log.i("Notification Srevice", "Get min is.."+eventmin);
        Log.i("Notification Srevice", "Get upeventid is.."+upeventid);
        Log.i("Notification Srevice", "Get eventfrom is.."+eventfrom);
      
      
        if (upeventid.equalsIgnoreCase("-1")) {
            stopSelf();
            return;
        } else {
          
            if(!TextUtils.isEmpty(eventfrom)){
                if(eventfrom.equalsIgnoreCase("start")){
                    if(!TextUtils.isEmpty(eventhr)){
                        if(!TextUtils.isEmpty(eventmin)){
                            createNotification();      
                        }
                    }  
                }else{
                    if(upeventid.equalsIgnoreCase(activeeventid)){
                        createNotification();  
                    }
                }
            }
            stopSelf();
        }
      
        }catch(Exception e){
            e.printStackTrace();
        }
    }
   
    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }
   
    private void createNotification(){
        System.out.println("createNotification");
      
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        int icon = R.drawable.tixeappicon;
        CharSequence tickerText = getString(R.string.app_name);
      
        Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(new long[] { 0, 1000, 200, 1000 }, -1);
      
        Notification notification = new Notification(icon, tickerText,
                System.currentTimeMillis());
        notification.flags |= Notification.FLAG_AUTO_CANCEL;

        Intent notificationIntent = new Intent(NotificationService1.this,UpcomingEvent.class);
        Intent notificationIntentactive = new Intent(NotificationService1.this,ActiveEvents.class);
      
         contentTitle = "Title";
        if(!TextUtils.isEmpty(eventfrom)){
            if(eventfrom.equalsIgnoreCase("start")){
                Log.i("Notification Service", "Event from Start event service");
                if(eventhr.equalsIgnoreCase("0")){
                    contentText = "Your event Starts with in next "+eventmin+" minutes";
                }else{
                    contentText = "Your event Starts with in next "+eventhr+":"+eventmin+" minutes";
                }
            }else{
            Log.i("Notification Service", "Event from End event service");
                 contentText = "Your event ends with in next 10 minutes";
        }
        }
      
        if(eventfrom.equalsIgnoreCase("start")){
            PendingIntent contentIntent = PendingIntent.getActivity(this, Integer.parseInt(upeventid),notificationIntent, 0);
            notification.setLatestEventInfo(this, contentTitle, contentText,contentIntent);
            mNotificationManager.notify(Integer.parseInt(upeventid), notification);
        }else{
            PendingIntent contentIntent = PendingIntent.getActivity(this, Integer.parseInt(upeventid),notificationIntentactive, 0);
            notification.setLatestEventInfo(this, contentTitle, contentText,contentIntent);
            mNotificationManager.notify(Integer.parseInt(upeventid), notification);
        }
      
    }

}
 


No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...