Android 由AlarmManager每天在特定时间启动服务不工作
我想每天在特定的时间启动服务,然后每分钟发送一次服务启动通知,单独运行服务很好,但是当alarmmanager调用服务时,什么也没有发生 此处显示报警代码:Android 由AlarmManager每天在特定时间启动服务不工作,android,service,time,alarmmanager,android-alarms,Android,Service,Time,Alarmmanager,Android Alarms,我想每天在特定的时间启动服务,然后每分钟发送一次服务启动通知,单独运行服务很好,但是当alarmmanager调用服务时,什么也没有发生 此处显示报警代码: Calendar cur_cal = new GregorianCalendar(); cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar Calendar cal = new Grego
Calendar cur_cal = new GregorianCalendar();
cur_cal.setTimeInMillis(System.currentTimeMillis());//set the current time and date for this calendar
Calendar cal = new GregorianCalendar();
cal.add(Calendar.DAY_OF_YEAR, cur_cal.get(Calendar.DAY_OF_YEAR));
cal.set(Calendar.HOUR_OF_DAY, 20);
cal.set(Calendar.MINUTE, 23);
cal.set(Calendar.SECOND, cur_cal.get(Calendar.SECOND));
cal.set(Calendar.MILLISECOND, cur_cal.get(Calendar.MILLISECOND));
cal.set(Calendar.DATE, cur_cal.get(Calendar.DATE));
cal.set(Calendar.MONTH, cur_cal.get(Calendar.MONTH));
Intent intent = new Intent(this, NotifService.class);
PendingIntent pintent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 30 * 1000, pintent);
这里是NotiService.Class:
public class NotifService extends Service {
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
private static final int HELLO_ID = 1;
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
final Intent intent1 = new Intent(this, DialogoActivity.class);
scheduler.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
// Look up the notification manager server
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Create your notification
//int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Dominion";
//long when = System.currentTimeMillis();
//Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "your turn";
String contentText = "click here";
PendingIntent pIntent = PendingIntent.getActivity(NotifService.this, 0, intent1, 0);
Notification notification = new NotificationCompat.Builder(getApplicationContext()).setContentIntent(pIntent).setTicker(tickerText).setContentTitle(contentTitle).setContentText(contentText).setSmallIcon(R.drawable.ic_launcher).addAction(R.drawable.ic_launcher, "Si", pIntent).setVibrate(new long[] {100, 250, 100, 500}).build();
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
// Send the notification
nm.notify(HELLO_ID, notification);
}
}, 1, 1, TimeUnit.MINUTES);
}
@Override
public void onDestroy() {
super.onDestroy();
}
}
可能是您没有在Manifest.XML中声明您的服务
请参阅:广播接收器是否实际调用它?您可能需要从服务而不是广播调用挂起的意图请参阅PendingEvent.getService(上下文、请求代码、意图、标志)我尝试了PendingEvent.getService(上下文、请求代码、意图、标志),但没有任何错误,但也不起作用