没有 Logcat 打印或来自启动接收器的通知

No Logcat print or notification from bootup receiver

我有一个名为 Bootup 的扩展 class 广播接收器,我想在设备关闭后首次启动时调用它。我已将此实现到我的其他应用程序之一中,因此我将该代码迁移到此应用程序并更改了所有必要的细节。

这是在清单 XML 文件中的应用程序标签下注册的方式:

<receiver android:enabled="true" android:name=".reveivers.Bootup"
    android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</receiver>

这是 class 本身:

public class Bootup extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        //Received when the phone boots up

        //Do some stuff

        //Test notification to check if it worked. Doesn't show, but the method is valid code.
        playNotification(context, "Active English", "Bootup received!");

        //More code, then this bit to check if it works again
        Log.d("test", milliseconds + " _ " + System.currentTimeMillis());

        //Yet more code
    }

    private void playNotification(Context context, String name, String description){
        long[] vibrate = new long[2];
        vibrate[0] = 500;
        vibrate[1] = 1000;
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
            .setContentText(description)
            .setSmallIcon(R.drawable.notificationicon)
            .setLights(Color.GREEN, 500, 2000)
            .setAutoCancel(true)
            .setVibrate(vibrate)
            .setContentTitle(name);
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(1, builder.build());
    }

}

我现在告诉你,所有的代码都是有效的。然而,无论出于何种原因,logcat 消息和通知均未出现在屏幕上。 playNotification 方法在其他应用程序中运行良好,所以这不是问题。

有什么想法吗?

编辑:添加了通知方法的代码,以防万一它对任何人都有用。

尝试添加RECEIVE_BOOT_COMPLETED权限。在您的清单中的应用程序标记外添加此行:

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