Android广播接收器已呼叫但未运行
我已经创建了一个广播接收器和报警管理器(以下代码): 报警管理器Android广播接收器已呼叫但未运行,android,broadcastreceiver,android-service,alarmmanager,Android,Broadcastreceiver,Android Service,Alarmmanager,我已经创建了一个广播接收器和报警管理器(以下代码): 报警管理器 Calendar cal = Calendar.getInstance(); cal.set(Calendar.HOUR, 11); cal.set(Calendar.MINUTE, 18); cal.set(Calendar.SECOND, 00); // Create a new PendingIntent and add it to the AlarmMana
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR, 11);
cal.set(Calendar.MINUTE, 18);
cal.set(Calendar.SECOND, 00);
// Create a new PendingIntent and add it to the AlarmManager
Intent intent = new Intent(this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,
1234567890, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(Activity.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
Myreceiver
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.println("SERVICE RECIEVED");
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
MyAlarmService
public class MyAlarmService extends Service {
private NotificationManager mManager;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
@SuppressWarnings("static-access")
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
mManager = (NotificationManager) this.getApplicationContext()
.getSystemService(
this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),
MainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,
"Alarm", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(
this.getApplicationContext(), 0, intent1,
PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(),
"Alarm", "Open the app now",
pendingNotificationIntent);
mManager.notify(0, notification);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
}
显示
<service
android:name=".MyAlarmService"
android:enabled="true" />
<receiver android:name=".MyReceiver"/>
包值是包名称
有人能解释一下哪里出了问题吗?报警调用正常,但功能不运行。尝试使用
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,
1234567890, intent, PendingIntent.FLAG_UPDATE_CURRENT);
而不是
PendingIntent pendingIntent = PendingIntent.getActivity(this,
1234567890, intent, PendingIntent.FLAG_UPDATE_CURRENT);
onStart
在API 5中被弃用
将其更改为onStartCommand
。照此
希望它有帮助什么不起作用??intent1.addFlags(Intent.FLAG\u ACTIVITY\u NEW\u TASK);将getActivity更改为getBroadcast工作得非常好
PendingIntent pendingIntent = PendingIntent.getActivity(this,
1234567890, intent, PendingIntent.FLAG_UPDATE_CURRENT);