手机锁定时,前台服务通知不执行任何操作

时间:2018-10-08 01:06:25

标签: android android-studio service

我有一个前台服务正在运行,即使锁定,它也会在屏幕上显示通知。如果我单击解锁后的通知,它将成功打开我的活动。

如果在手机处于锁定状态时单击通知,直到解锁手机,都不会发生任何事情。无论我如何处理未决的意图,无论是启动服务,发送广播还是开始活动,都只有在我解锁手机后才会发生。它没有任何东西的onCreate方法。

我如何做到这一点,以便前台服务在锁定时仍能处理未决的意图?

NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("mychannel",
                "mychannel name",
                NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription("channel desc");
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
        channel.enableVibration(true);
        channel.setShowBadge(false);
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
        channel.setVibrationPattern(null);
        if (mNotificationManager != null) {
            mNotificationManager.createNotificationChannel(channel);
        }
    }

    // Create notification builder.
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, "mychannel")
            .setSmallIcon(R.drawable.ic_action_notification)
            .setColor(getResources().getColor(R.color.colorPrimary))
            .setContentTitle("note title")
            .setContentText("note text)
            .setWhen(0)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setTicker(null)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setLights(Color.RED, 3000, 3000)
            .setVibrate(null)
            .setAutoCancel(false);




    Intent intent = new Intent(this, myclass.class);
    PendingIntent pi = PendingIntent.getActivity(this, 2, intent, PendingIntent.FLAG_UPDATE_CURRENT);


    mBuilder.setContentIntent(pi);


    Notification notification = mBuilder.build();

    // Start foreground service.
    startForeground(2, notification);

0 个答案:

没有答案