我正在构建一个应用程序来提醒我的东西。我正在使用警报管理器重复警报。 但是当我设置多个闹钟时问题,如果我关闭应用程序第一个警报服务崩溃,第二个工作正常。我不知道为什么。
public void setReminder(){
Calendar dm = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(activity , broadcast.class);
intent.putExtra("com.todolist.dailylist.data" , listUID);
PendingIntent pendingintent = PendingIntent.getBroadcast(getApplicationContext(),alarmID, intent , PendingIntent.FLAG_ONE_SHOT);
int month = Integer.parseInt(splitDate(date)[1]);
dm.set(Calendar.MONTH, month -1 );
dm.set(Calendar.DAY_OF_MONTH, Integer.parseInt(splitDate(date)[0]));
dm.set(Calendar.YEAR, Integer.parseInt(splitDate(date)[2]));
dm.set(Calendar.HOUR, Integer.parseInt(formatTime(timeVar)[0]));
dm.set(Calendar.MINUTE, Integer.parseInt(formatTime(timeVar)[1]));
if(formatTime(timeVar)[2].equals("AM")) {
dm.set(Calendar.AM_PM, Calendar.AM);
}else if(formatTime(timeVar)[2].equals("PM")){
dm.set(Calendar.AM_PM, Calendar.PM);
}
long when = dm.getTimeInMillis();
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,when, AlarmManager.INTERVAL_DAY ,pendingintent);
}
广播接收器代码
public class broadcast extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String intentID = intent.getStringExtra("com.todolist.dailylist.data");
Intent service = new Intent(context, service.class);
service.putExtra("com.todolist.dailylist.listID", intentID);
context.startService(service);
}
}
当警报触发时,服务会显示覆盖窗口,就像最常用的应用程序一样。
服务类。
public class service extends Service {
private WindowManager windowManager;
private LayoutInflater inflater;
private View myview;
private diUtil databaseutil;
private String listID;
private String value = null;
private TextView listTitle;
private TextView listDescription;
private RelativeLayout rootLayout;
private RelativeLayout controllerIcons;
private ImageButton doneImage;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
databaseutil = new diUtil(this);
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_TOAST,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.CENTER;
myview = inflater.inflate(R.layout.notificationwindow , null);
listTitle = (TextView)myview.findViewById(R.id.listTitleHeader);
listDescription = (TextView)myview.findViewById(R.id.listDescriptionHeader);
controllerIcons = (RelativeLayout)myview.findViewById(R.id.controller_icons);
rootLayout = (RelativeLayout)myview.findViewById(R.id.layout_controller);
doneImage = (ImageButton) myview.findViewById(R.id.doneIconController);
windowManager.addView(myview, params);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
listID = intent.getStringExtra("com.todolist.dailylist.listID");
ArrayList<listData> data = databaseutil.getListitem(listID);
for(int i=0; i < data.size();i++){
listTitle.setText(data.get(i).getListName());
if(data.get(i).getListText() != null){
Toast.makeText(this, "description present", Toast.LENGTH_SHORT).show();
}else if(data.get(i).getListText() == null){
Toast.makeText(this, "description not present", Toast.LENGTH_SHORT).show();
}
doneImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
windowManager.removeView(myview);
}
});
}
return Service.START_STICKY;
}
@Override
public void onDestroy() {
windowManager.removeView(myview);
super.onDestroy();
}
}
这是我从logcat控制台获得的错误
Process: com.todolist.dailylist, PID: 5601
java.lang.RuntimeException: Unable to start service com.todolist.dailylist.services.service@6a10f79 with null: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3314)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.todolist.dailylist.services.service.onStartCommand(service.java:74)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3297)
at android.app.ActivityThread.-wrap21(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1565)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
有人知道为什么会这样,以及如何解决这个问题?非常感谢您的阅读。非常感谢!