我试图建立一个简单的报警管理的服务。
如果我从活动开始报警管理,它的工作原理
Intent intentzz = new Intent(getApplicationContext(), Homev2.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 0, intentzz, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.SECOND, 5);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
但是,如果我尝试从服务启动一样,它只是不,我没有得到任何错误
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
// throw new UnsupportedOperationException("Not yet implemented");
return null;
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.e("we here", "here");
Intent intent1 = new Intent(getApplicationContext(), SettingsActivity.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1,intent1, 0);
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MINUTE, 5);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
return super.onStartCommand(intent, flags, startId);
}
}
可能是什么问题?
尝试改变这一点:
Intent intent1 = new Intent(getApplicationContext(), SettingsActivity.class);
PendingIntent pi = PendingIntent.getActivity(getApplicationContext(), 1,intent1, 0);
为此:
Intent intent1 = new Intent(this, SettingsActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 1,intent1, 0);