我使用AlarmManager每天触发一次通知。我已经实现了以下逻辑来触发通知。但它不起作用。请纠正我的错误。
我在MainActivity的onCreate()方法中调用以下代码:
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(this, MyStartServiceReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmIntent.setData(Uri.parse("custom://" + System.currentTimeMillis()));
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
Calendar now = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 5);
calendar.set(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 0);
if (now.after(calendar)) {
Log.e("AlarmManager", "Added a day");
calendar.add(Calendar.DATE, 1);
}
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
这是我的广播接收器:
public class MyStartServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("AlarmReceiver", "Started");
Intent intent1 = new Intent(context, AlarmService.class);
intent1.setData(Uri.parse("custom://" + System.currentTimeMillis()));
context.startService(intent1);
}
}
以下是我的服务,其中会触发通知:
public class AlarmService extends IntentService {
public AlarmService() {
super("AlarmService");
}
private NotificationManager notificationManager;
private PendingIntent pendingIntent;
private static int NOTIFICATION_ID = 1;
Notification notification;
@Override
protected void onHandleIntent(Intent intent) {
Log.e("Service", "I ran");
Context context = this.getApplicationContext();
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, MenuActivity.class);
pendingIntent = PendingIntent.getActivity(context, 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Resources resources = this.getResources();
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
notification = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.mipmap.ic_launcher))
.setTicker("ticker value")
.setPriority(8)
.setSound(soundUri)
.setContentTitle("Sun Rise")
.setContentText("Time to draw kolam").build();
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
notification.ledARGB = 0xFFFFA500;
notification.ledOnMS = 800;
notification.ledOffMS = 1000;
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID, notification);
Log.i("notif", "Notifications sent.");
}
}
最后这里是我的Manifest.xml:
<receiver
android:name=".MenuActivity$MyStartServiceReceiver"
android:enabled="true"/>
<service
android:name=".services.AlarmService"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="NOTIFICATION_SERVICE" />
</intent-filter>
</service>
<receiver
android:name=".BootReceiver"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
更新:
我的Logcat:
java.lang.RuntimeException: Unable to instantiate receiver com.ignite.a01hw909350.kolamdemo.MenuActivity$MyStartServiceReceiver: java.lang.InstantiationException: java.lang.Class<com.ignite.a01hw909350.kolamdemo.MenuActivity$MyStartServiceReceiver> has no zero argument constructor
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2753)
at android.app.ActivityThread.access$1800(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1450)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5609)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.InstantiationException: java.lang.Class<com.ignite.a01hw909350.kolamdemo.MenuActivity$MyStartServiceReceiver> has no zero argument constructor
at java.lang.Class.newInstance(Native Method)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2748)
at android.app.ActivityThread.access$1800(ActivityThread.java:168)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1450)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5609)
at java.lang.reflect.Method.invoke(Native Method)
我的AlarmManager无法触发广播接收器。
答案 0 :(得分:0)
根据文件 -
ELAPSED_REALTIME_WAKEUP
SystemClock.elapsedRealtime()中的闹钟时间(自启动以来的时间, 包括睡眠),它会在设备熄灭时唤醒它。
因此,ELAPSED_REALTIME_WAKEUP将在设备启动后触发警报,而RTC_WAKEUP将根据时钟的时间触发警报。
我用
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);
用于在我的项目中设置警报,并且工作正常。