Android 在自定义通知单击时提示解锁锁定屏幕

Android 在自定义通知单击时提示解锁锁定屏幕,android,notifications,lockscreen,Android,Notifications,Lockscreen,我有一个自定义通知显示在锁屏上。我正在使用广播挂起意图在单击通知后向我的应用程序发送消息。随后在广播接收器中启动一个活动 问题是,一旦我点击通知,它就会从锁屏上消失,并且活动会在锁屏后面启动。它不会要求用户解锁屏幕 我的要求是要求用户在通知的click事件发送广播后立即解锁屏幕。我该怎么做 我可以找到一个看起来像我的问题的问题。但不幸的是,没有答案 下面是一些解释我创建通知的代码 /** * Method to render Notification */ private void show

我有一个自定义通知显示在锁屏上。我正在使用广播挂起意图在单击通知后向我的应用程序发送消息。随后在广播接收器中启动一个活动

问题是,一旦我点击通知,它就会从锁屏上消失,并且活动会在锁屏后面启动。它不会要求用户解锁屏幕

我的要求是要求用户在通知的click事件发送广播后立即解锁屏幕。我该怎么做

我可以找到一个看起来像我的问题的问题。但不幸的是,没有答案

下面是一些解释我创建通知的代码

/**
 * Method to render Notification
 */
private void showNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    /* set mandatory stuff on builder */

    Notification notification = builder.build();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
        notification.bigContentView = createCustomView(context);
    }

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(getNotificationId(), notification);
}

/**
 * Method to return notification click's PendingIntent
 */
private PendingIntent getNotificationClickIntent() {
    Intent bcIntent = new Intent(OPEN_APP);
    bcIntent.putExtra(EXTRA_DEEP_LINK_URL, getDeepLinkUrl());

    return PendingIntent.getBroadcast(
        context, getReqCode(), bcIntent, PendingIntent.FLAG_ONE_SHOT);
}

/**
 * Method to create custom view for notification
 */
private RemoteViews createCustomView(Context context) {
   RemoteViews customView = new RemoteViews(context.getPackageName(), R.layout.custom_layout);

   if (customView != null) {
        // set dat on views

        customView.setOnClickPendingIntent(R.id.my_view, getNotificationClickIntent());
   }

   return customView;
}

在挂起意图中使用活动意图,而不是服务或广播

PendingIntent.getActivity(context, 0, intent,
                                 PendingIntent.FLAG_UPDATE_CURRENT);
更新:

就我而言,我使用服务

private fun activityPendingIntent(context: Context, action: String? = null): PendingIntent {
    Timber.d("activityPendingIntent")
    val intent = Intent(context, DummyBackgroundActivity::class.java)
    action?.let { intent.action = action }
    return PendingIntent.getActivity(context, ACTIVITY_NOTIFICATION_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT)
}
Dummy背景活动

class DummyBackgroundActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val service = Intent(this, BackgroundService::class.java)
        service.action = intent.action
        startService(service)
    }

    override fun onResume() {
        super.onResume()
        finish()
    }
}
服务:

override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
    Timber.d("onStartCommand intent = %s", intent?.action)
    when (intent?.action) {
       //Handle it 
    }

    return Service.START_NOT_STICKY
}

我希望您使用广播复制相同的内容。

不。这不能用于我的情况。我的代码是库的一部分。在图书馆中,我们不知道需要启动哪些活动。因此,我现在依赖广播。在receiver中,使用此库的客户端应用程序将实现活动启动代码。@RahulRaveendran在您的情况下,您可以创建一个虚拟活动,并在onCreate广播事件和onResume中完成活动。我已经更新了答案。希望能有帮助。谢谢你的更新!但我有这个想法。我想看看有没有其他办法来解决这个问题。因为在这一点上,在中间引入一个活动是不可行的:(@RahulRavendran没问题,也许我的答案会帮助其他人。你最终找到了一个更优雅的解决方案吗?@AmirUval我最终使用了一个没有Utihanks的中间活动。这也是我最终所做的。