如何在设备启动时实施广播接收器?

时间:2019-10-14 11:37:20

标签: android notifications broadcastreceiver alarmmanager

我有一个简单的应用程序,可以在按下按钮5分钟后安排通知。这对我来说可以。但是,如果我在5分钟内重新启动手机,则不会收到通知。我已经对设备重新启动时的警报管理器和计划通知进行了研究。我的应用程序中有5个课程。

它们是:

  • MainActivity.class
  • NotificationUtil.class
  • NotificationPublisher.class
  • NotificationView.class
  • DeviceBootReceiver.class

我有2个扩展BroadCast接收器的类。他们是:

  • NotificationPublisher.class
  • DeviceBootReceiver.class

从研究和其他堆栈溢出问题中,我了解到,为了在设备重启后接收通知,我应该创建一个扩展BroadCast Receiver的类。每当电话启动时,它将运行onReceive()方法中的任何代码。

这是我的MainActivity.class

public class MainActivity extends AppCompatActivity {
    private Button button;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = findViewById(R.id.notification);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NotificationUtil.createNotification(MainActivity.this,NotificationView.class,"Notification","You have a new Task");
            }
        });
    }
}

这是我的NotificationUtil.class。这是处理所有通知的类,这是我在MainActivity中的Button Click上调用的类。

public class NotificationUtil
{
    public static void createNotification(Context context,Class<?> cls, String title, String content)
    {
        Intent intent = new Intent(context,cls);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        sendNotification(getNotification(pendingIntent,context,title,content),context);
    }

    private static void sendNotification(Notification notification,Context context)
    {
        Intent notificationIntent = new Intent(context, NotificationPublisher.class);
        Intent deviceBootIntent = new Intent(context,DeviceBootReceiver.class);

        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID,1);
        notificationIntent.putExtra(NotificationPublisher.NOTIFICATION,notification);

        deviceBootIntent.putExtra(DeviceBootReceiver.NOTIFICATION_ID, 1);
        deviceBootIntent.putExtra(DeviceBootReceiver.NOTIFICATION, notification);

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60 * 1000,pendingIntent);
    }

    private static Notification getNotification(PendingIntent pendingIntent, Context context, String title, String content)
    {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context,"ChannelID");
        builder.setSmallIcon(R.drawable.notification_bell);
        builder.setContentTitle(title);
        builder.setContentText("You have a Notification");
        builder.setSubText("Tap To View");
        builder.setStyle(new NotificationCompat.BigTextStyle().bigText(content));
        builder.setContentIntent(pendingIntent);
        return builder.build();
    }
}

这是我的NotificationPublisher.class。这是负责发送通知的类。

public class NotificationPublisher extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification_id";
    public static String NOTIFICATION = "notification";

    @Override
    public void onReceive(final Context context, Intent intent) {

        try
        {
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

            Notification notification = intent.getParcelableExtra(NOTIFICATION);
            int notificationId = intent.getIntExtra(NOTIFICATION_ID, 1);
            notificationManager.notify(notificationId, notification);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

这是我的DeviceBootReceiver.class。我正在尝试在此处实施我的代码,以便在设备重启后接收通知。

public class DeviceBootReceiver extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification_id";
    public static String NOTIFICATION = "notification";

    public void onReceive(Context context, Intent intent)
    {
        if(Objects.requireNonNull(intent.getAction()).equalsIgnoreCase("android.intent.action.BOOT_COMPLETED"))
        {
            Notification notification = intent.getParcelableExtra(NOTIFICATION);
            int notificationID = intent.getIntExtra(NOTIFICATION_ID,1);

            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(notificationID, notification);
        }
    }
}

重新启动手机后,我收到消息“应用程序已停止工作”。我不确定我要去哪里。

这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.notificationtest">

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity android:name=".NotificationView"
            android:parentActivityName=".MainActivity"/>

        <receiver android:name=".NotificationPublisher"/>


        <receiver android:name=".DeviceBootReceiver"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <category android:name="android.intent.category.DEFAULT" />
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
                <action android:name="android.intent.action.REBOOT" />
                <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                <action android:name="com.htc.intent.action.QUICKBOOT_POWERON" />
                <action android:name="android.intent.action.ACTION_SHUTDOWN" />
            </intent-filter>
        </receiver>


    </application>

</manifest>

据我了解,扩展BroadCast Receivers的类在设备重新启动时被调用。因此,我尝试使用notification

notificationIDIntent.putExtra从NotificationUtil.class传递到DeviceBootReceiver.class这样。
Intent deviceBootIntent = new Intent(context,DeviceBootReceiver.class);
deviceBootIntent.putExtra(DeviceBootReceiver.NOTIFICATION_ID, 1);
deviceBootIntent.putExtra(DeviceBootReceiver.NOTIFICATION, notification);

然后在onReceive方法下的DeviceBootReceiver.class中,我使用getExtra获取值。我进一步将获得的2个值传递给NotificationPublisher.class。

此“ WAS”是我对DeviceBootReceiver.class的较早实现。由于此操作无效,因此我尝试了如上所述的新实现。

public class DeviceBootReceiver extends BroadcastReceiver {

    public static String NOTIFICATION_ID = "notification_id";
    public static String NOTIFICATION = "notification";

    public void onReceive(Context context, Intent intent)
    {
        if(Objects.requireNonNull(intent.getAction()).equals("android.intent.action.BOOT_COMPLETED"))
        {
            Intent pushIntent = new Intent(context, NotificationPublisher.class);

            Notification notification = intent.getParcelableExtra(NOTIFICATION);
            int notificationID = intent.getIntExtra(NOTIFICATION_ID,1);

            pushIntent.putExtra(NotificationPublisher.NOTIFICATION_ID,notificationID);
            pushIntent.putExtra(NotificationPublisher.NOTIFICATION,notification);

            PendingIntent pendingIntent = PendingIntent.getBroadcast(context,0,pushIntent,PendingIntent.FLAG_UPDATE_CURRENT);

            AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
            alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60 * 1000,pendingIntent);
        }

    }
}

我所期望的是,由于DeviceBootReceiver.class在启动时被调用,因此这2个值将被发送到我的NotificationPublisher.class,并且在启动时我将收到通知。取而代之的是,应用程序由于onReceive方法中的旧实现和新实现而崩溃。

具有在设备重启时接收通知的经验的人可以帮助我吗?请仔细阅读我的代码,并尝试在您的项目中实现它,以了解我的工作。我已经在此问题中上传了所有代码。

这些是我的崩溃日志:

10-14 17:26:03.088 5438-5438/? I/SELinux: Function: selinux_android_load_priority [0], There is no sepolicy file.

10-14 17:26:03.088 5438-5438/? I/SELinux: Function: selinux_android_load_priority [1], There is no sepolicy version file.

10-14 17:26:03.088 5438-5438/? I/SELinux: Function: selinux_android_load_priority , priority is 3. priority version is VE=SEPF_SM-G357FZ_4.4.4_A042


10-14 17:26:03.088 5438-5438/? E/SELinux: [DEBUG] get_category: variable seinfocat: default sensitivity: NULL, cateogry: NULL
10-14 17:26:03.088 5438-5438/? E/SELinux: seapp_context_lookup: str_security_ctx is null
10-14 17:26:03.088 5438-5438/? E/dalvikvm: >>>>> Normal User
10-14 17:26:03.088 5438-5438/? E/dalvikvm: >>>>> com.example.notificationtest [ userId:0 | appId:10181 ]
10-14 17:26:03.088 5438-5438/? E/SELinux: [DEBUG] get_category: variable seinfocat: default sensitivity: NULL, cateogry: NULL
10-14 17:26:03.088 5438-5438/? E/SELinux: seapp_context_lookup: str_security_ctx is null
10-14 17:26:03.088 5438-5438/? D/dalvikvm: Late-enabling CheckJNI
10-14 17:26:03.088 5438-5438/? I/libpersona: KNOX_SDCARD checking this for 10181
10-14 17:26:03.088 5438-5438/? I/libpersona: KNOX_SDCARD not a persona
10-14 17:26:03.138 5438-5438/? D/TimaKeyStoreProvider: in addTimaSignatureService
10-14 17:26:03.148 5438-5438/? D/TimaKeyStoreProvider: Cannot add TimaSignature Service, License check Failed
10-14 17:26:03.148 5438-5438/? D/ActivityThread: Added TimaKesytore provider
10-14 17:26:03.168 5438-5438/com.example.notificationtest D/DisplayManagerGlobal: getDisplayInfo: displayId=0, info=DisplayInfo{"Built-in Screen", app 480 x 800, real 480 x 800, largest app 800 x 762, smallest app 480 x 442, 60.0 fps, rotation0, density 240 (217.714 x 216.17) dpi, layerStack 0, type BUILT_IN, FLAG_SECURE, FLAG_SUPPORTS_PROTECTED_BUFFERS}
10-14 17:26:03.168 5438-5438/com.example.notificationtest D/DisplayManagerGlobal: getDisplayInfo: displayId=0, info=DisplayInfo{"Built-in Screen", app 480 x 800, real 480 x 800, largest app 800 x 762, smallest app 480 x 442, 60.0 fps, rotation0, density 240 (217.714 x 216.17) dpi, layerStack 0, type BUILT_IN, FLAG_SECURE, FLAG_SUPPORTS_PROTECTED_BUFFERS}
10-14 17:26:03.168 5438-5438/com.example.notificationtest D/ActivityThread: handleBindApplication:com.example.notificationtest
10-14 17:26:03.168 5438-5438/com.example.notificationtest D/DisplayManagerGlobal: getDisplayInfo: displayId=0, info=DisplayInfo{"Built-in Screen", app 480 x 800, real 480 x 800, largest app 800 x 762, smallest app 480 x 442, 60.0 fps, rotation0, density 240 (217.714 x 216.17) dpi, layerStack 0, type BUILT_IN, FLAG_SECURE, FLAG_SUPPORTS_PROTECTED_BUFFERS}
10-14 17:26:03.168 5438-5438/com.example.notificationtest D/DisplayManagerGlobal: getDisplayInfo: displayId=0, info=DisplayInfo{"Built-in Screen", app 480 x 800, real 480 x 800, largest app 800 x 762, smallest app 480 x 442, 60.0 fps, rotation0, density 240 (217.714 x 216.17) dpi, layerStack 0, type BUILT_IN, FLAG_SECURE, FLAG_SUPPORTS_PROTECTED_BUFFERS}
10-14 17:26:03.238 5438-5438/com.example.notificationtest D/AndroidRuntime: Shutting down VM
10-14 17:26:03.238 5438-5438/com.example.notificationtest W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x416a7d58)
10-14 17:26:03.238 5438-5438/com.example.notificationtest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.notificationtest, PID: 5438
    java.lang.RuntimeException: Unable to start receiver com.example.notificationtest.NotificationPublisher: java.lang.NullPointerException
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2551)
        at android.app.ActivityThread.access$1700(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5426)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
        at android.app.NotificationManager.notify(NotificationManager.java:150)
        at android.app.NotificationManager.notify(NotificationManager.java:120)
        at com.example.notificationtest.NotificationPublisher.onReceive(NotificationPublisher.java:23)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2544)
        at android.app.ActivityThread.access$1700(ActivityThread.java:155) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:136) 
        at android.app.ActivityThread.main(ActivityThread.java:5426) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:515) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
        at dalvik.system.NativeStart.main(Native Method) 
10-14 17:26:07.708 5438-5438/com.example.notificationtest I/Process: Sending signal. PID: 5438 SIG: 9

2 个答案:

答案 0 :(得分:1)

this response

作为 Android 8.0 (API level 26) 后台执行限制的一部分,面向 API级别26 或更高版本的应用无法注册清单中隐式广播的广播接收器。但是,一些广播目前不受这些限制。无论应用程序所针对的API级别如何,应用程序都可以继续注册以下广播的侦听器。

  

即使这些隐式广播仍在后台运行,   您应该避免为他们注册监听器。

您可以改用Implicit Broadcast Exceptions

仅供参考

   java.lang.RuntimeException: Unable to start receiver com.example.notificationtest.NotificationPublisher: java.lang.NullPointerException
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2551)
        at android.app.ActivityThread.access$1700(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1319)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
当应用程序尝试使用具有空值的对象引用时,抛出

scheduled jobs

答案 1 :(得分:0)

ACTION_BOOT_COMPLETED在隐式广播白名单上,因此即使在Android 8.0或更高版本中,您也可以在清单中进行注册。

此答案最初由@CommonsWare at this thread

回答。

总而言之,一旦实施,别忘了对其进行测试。