【发布时间】:2013-12-02 12:24:05
【问题描述】:
我是新手程序员,我正在创建一个包含多个活动的警报应用程序,每个活动都能够设置 1 个警报。我几乎完成了应用程序的制作。我已经设置了一个 onboot-broadcastreceiver,并使用来自多个活动的 sharedpreferences 保存值,然后在设备重新启动后访问它们。由于某些未知原因,如果设备未重新启动,警报接收器工作正常,但在重新启动后无法关闭,即使代码几乎相同。
设备重启后我也无法使用断点调试器。有人可以展示如何这样做还是更好地提出以下代码中可能存在的问题
这是 AlarmReceiver 广播接收器的代码(重启后我没有收到 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 广播接收器的代码(我得到了 toast 和通知):
@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();
}
【问题讨论】:
标签: android debugging android-alarms