我是新手程序员,我正在创建一个包含多个活动的闹钟应用程序,每个活动都能设置1个闹钟。我差不多完成了应用程序。我已经设置了一个onboot-broadcastreceiver,我使用多个活动的sharedpreferences保存值,然后在设备重启后访问它们。由于某些未知原因,如果设备未重新启动,则警报接收器正常工作,但即使代码几乎相同,重启后也无法启动。
设备重启后,我也无法使用断点调试器。有人可以说明如何做或更好仍然建议下面的代码可能有什么问题
以下是AlarmReceiver broadcastreceiver的代码(我没有得到toast或重启后的通知)
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();
}
}
这是onboot广播接收器的代码(我得到了吐司以及通知):
@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();
}
答案 0 :(得分:0)
我使用了PendingIntent.GetService而不是PendingIntent.GetBroadcast。这就是代码无效的原因。我也能够在onBoot broadcastreceiver中使用Debug.waitForDebugger()调试器