【问题标题】:Unable to trigger notification through AlarmManager无法通过 AlarmManager 触发通知
【发布时间】:2017-07-01 07:41:02
【问题描述】:

我每天使用 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 无法触发广播接收器。

【问题讨论】:

  • 请附上logcat。
  • 它没有触发广播接收器的onReceive()。它只是将日期添加到Calendar

标签: android android-notifications


【解决方案1】:

根据文档 -

ELAPSED_REALTIME_WAKEUP

SystemClock.elapsedRealtime() 中的闹钟时间(自启动以来的时间, 包括睡眠),它将在设备关闭时唤醒设备。

因此,ELAPSED_REALTIME_WAKEUP 将在设备启动后触发警报,而 RTC_WAKEUP 将根据时钟时间触发警报。

我用

manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_HALF_HOUR, pendingIntent);

用于在我的项目中设置警报,效果很好。

【讨论】:

  • 您在设置闹钟时是否在日志中获得了正确的日历时间?
  • 我什至尝试过每一分钟。但仍然没有收到通知。 AlarmManager 无法触发广播接收器。
  • 您的代码中有一条声明alarmManager.cancel(pendingIntent);。删除后可以试试吗?在这种情况下,我认为在设置警报时不需要调用取消。
  • 我已经更新了logcat。现在应用程序崩溃了。我删除了alarmManage.cancel(pendingIntent)
  • 您的广播接收器是在单独的类中还是在 Activity 类中?从清单看来,它在 MenuActivity 类中。请为其创建一个新的公共类,然后尝试。否则,您的接收器是内部的,并且必须在 Activity 中注册才能仅在 Activity 处于活动状态时进行监听。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-25
  • 1970-01-01
相关资源
最近更新 更多