Вопрос

I am newbie programmer, I am creating an alarm app containing multiple activities, each capable of setting 1 alarm. I am almost done making the app. I have set up an onboot- broadcastreceiver and I save values using sharedpreferences from multiple activities and then access them after device has been rebooted. For some unknown reason the alarm receiver works ok if device is not rebooted but fails to go off when after reboot even though the code is almost identical.

I am also not able to use the breakpoint debugger after device is restarted. Can some one show how to do so or better still suggest what may be wrong in the below code

Here is the code for AlarmReceiver broadcastreceiver(I dont get either the toast or the notification after restart)

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    Toast.makeText
        (context, "toast 2", 10000).show();
    String Title=intent.getStringExtra("title");
    String days=intent.getStringExtra("days");
    String flag=(String) intent.getCharSequenceExtra("flag");
    int id=intent.getIntExtra("id", 0);
     NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification notify = new Notification(R.drawable.ic_launcher,days,System.currentTimeMillis());
        Intent i= new Intent(context,Menu.class);
        PendingIntent p= PendingIntent.getActivity(context,(int)System.currentTimeMillis()+100,i,PendingIntent.FLAG_ONE_SHOT);
        notify.setLatestEventInfo(context, Title, days, p);
        nm.notify(id,notify);
          Toast toast=Toast.makeText(context,"alarm"+flag , 10000);toast.show();            
}

 }

Here is the code for onboot broadcast receiver(I get both the toast as well as notification):

@Override
public void onReceive(Context context, Intent intent) {


NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notify = new Notification(R.drawable.ic_launcher,"restart",System.currentTimeMillis());
    Intent i= new Intent(context,Menu.class);
    PendingIntent p= PendingIntent.getActivity(context,3,i,PendingIntent.FLAG_ONE_SHOT);
    notify.setLatestEventInfo(context,"title","detail", p);
    nm.notify(3,notify);*/

scheduleAlarms(context);        
}     
  static void scheduleAlarms(Context context) {
      SharedPreferences sp= context.getSharedPreferences("time", mode);
    Long ctime= sp.getLong("time",0);
    SharedPreferences sp2= context.getSharedPreferences("day", mode);
    int days= sp2.getInt("day",0);
      Calendar c=GregorianCalendar.getInstance();
      c.setTimeInMillis(ctime);


    AlarmManager mgr=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent in=new Intent(context, AlarmReceiver2.class);
    in.putExtra("title", "title");
    in.putExtra("days",days+" days left");
    in.putExtra("id",4);
    PendingIntent pi=PendingIntent.getService(context,(int)System.currentTimeMillis(), in, 0);
    mgr.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(), pi);
    Toast.makeText
    (context,"toast1", Toast.LENGTH_SHORT).show();   
  }
Это было полезно?

Решение

I used PendingIntent.GetService instead of PendingIntent.GetBroadcast. that is why the code was not working. also i was able to use debugger using Debug.waitForDebugger() in the onBoot broadcastreceiver

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top