我在设置闹钟时遇到问题,即发送有关即将发生的事件的通知。我使用这些代码行从我的班级调用它(到目前为止它在应用程序启动后10秒开始,仅用于测试目的):
public void initNotifications(){
AlarmManager alarms = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
BroadcastManager receiver = new BroadcastManager();
IntentFilter filter = new IntentFilter("ALARM_ACTION");
registerReceiver(receiver, filter);
Intent intent = new Intent("ALARM_ACTION");
intent.putExtra("param", "My scheduled action");
PendingIntent operation = PendingIntent.getBroadcast(this, 0, intent, 0);
// I choose 3s after the launch of my application
alarms.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+10000, operation) ;
}
and here is the Broadcast Manager class (separate file):
public class BroadcastManager extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
//Toast.makeText(context, intent.getStringExtra("param"),Toast.LENGTH_SHORT).show();
Intent it = new Intent(context, MainActivity.class);
createNotification(context, it);
}
我还有createNotification函数,工作正常。
现在我将以下行添加到android Manifest:
<receiver
android:name="BroadcastManager"
android:label="BroadcastReceiverAux">
<intent-filter>
<action android:name="ALARM" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
在十秒钟之前退出应用程序时会发生以下错误代码:
Activity com.mysampleapp.MainMenu has leaked IntentReceiver com.mysampleapp.BroadcastManager@3370c300 that was originally registered here. Are you missing a call to unregisterReceiver()?
android.app.IntentReceiverLeaked: Activity com.mysampleapp.MainMenu has leaked IntentReceiver com.mysampleapp.BroadcastManager@3370c300 that was originally registered here. Are you missing a call to unregisterReceiver()?
at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:960)
at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:761)
at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:2002)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1982)
at android.app.ContextImpl.registerReceiver(ContextImpl.java:1976)
at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:503)
at com.mysampleapp.MainMenu.initNotifications(MainMenu.java:672)
at com.mysampleapp.MainMenu.onCreate(MainMenu.java:133)
at android.app.Activity.performCreate(Activity.java:6283)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2642)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2756)
at android.app.ActivityThread.access$900(ActivityThread.java:177)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5940)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
我老老实实地在Stack上查看了几个主题,但我找不到能解决问题的答案。我希望即使在设备重启后我的通知仍然可以运行,但是我想在以后杀死应用程序之后立即显示它们之后再移动。
提前谢谢你, 约翰
答案 0 :(得分:0)
尝试使用这样的AlarmManager,它对我有用:
Intent intent = new Intent(getActivity(), BroadcastManager.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getActivity().getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC, System.currentTimeMillis()+10000, pendingIntent);
} else {
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis()+10000, pendingIntent);
}