我正在使用警报管理器来调用接收方法的通知。我在其中添加了一些调试代码,发现该进程甚至没有进入。
以下是我的代码:
的Manifest.xml
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Menu">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ContentInput"
/>
<receiver
android:process=":remote"
android:name=".NoticationBroadcast"/>
</application>
Menu.java(主要类)
Button noti = (Button) findViewById(R.id.noti);
noti.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(ErrorTag, "before onclick");
scheduleNotification(getNotification("ggggg"),2000);
Log.e(ErrorTag, "after onclick");
}
});
private Notification getNotification(String content) {
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Scheduled Notification");
builder.setContentText(content);
builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
Log.e(ErrorTag, "finish get notificattion");
return builder.build();
}
private void scheduleNotification(Notification notification, int delay) {
Intent notificationIntent = new Intent(this, NoticationBroadcast.class);
notificationIntent.putExtra(NoticationBroadcast.NOTIFICATION_ID, 1);
notificationIntent.putExtra(NoticationBroadcast.NOTIFICATION, notification);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Log.e(ErrorTag, "after getBroadcast");
long futureInMillis = SystemClock.elapsedRealtime() + delay;
Calendar calendar = Calendar.getInstance();
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Log.e(ErrorTag, "after alarm");
}
接收者类
public class NoticationBroadcast extends BroadcastReceiver {
public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";
@Override
public void onReceive(Context context, Intent intent) {
Log.e("ERROR", "beginning of on receive");
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = intent.getParcelableExtra(NOTIFICATION);
int id = intent.getIntExtra(NOTIFICATION_ID, 0);
notificationManager.notify(id, notification);
Log.e("ERROR", "end of on receive");
}
}
编辑:
我更改了一行代码
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
到
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (5000), pendingIntent);
它工作得很好。但我不知道为什么会这样。如果有人知道发生了什么,请告诉我。谢谢!
答案 0 :(得分:0)
见这里:
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
您正在安排一项工作现在正好运行,显然不会被解雇。
在第二个版本(工作版本)中,您比现在晚5000毫秒安排作业:
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (5000), pendingIntent);
更改为此将按预期工作:
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis() + 5000,
pendingIntent);