从安全锁屏上的通知开始 activity

Start an activity from notification over the secure lockScreen

我一直试图在安全锁屏中显示 activity 来自通知(在不安全的锁屏中我实现了这一点)。

我按照一些问题回答了 Whosebug,但 none 解决了我的问题。

我将 post 部分代码。

主要活动(activity)

这个是在onCreate()

中执行的
Intent intentService = new Intent(this,NotificationService.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        startForegroundService(intentService);

    } else {
        startService(intentService);
    }

NotificationService(服务)

这是在onStartCommand中执行的(尝试在onCreate中执行)

  public void crearNotificacion(){
    RemoteViews rmv = new RemoteViews(getPackageName(),R.layout.notification_custom);

    /*Intent intent = new Intent(this, wPerfil.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pIntent = PendingIntent.getActivity(this, 54, intent,PendingIntent.FLAG_UPDATE_CURRENT);*/
    Intent intent = new Intent(this,NotificationIntentService.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getService(this,54,intent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    createNotificationChannel();// Este trozo de codigo es para versiones +26
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this,CHANNEL_ID)
            .setStyle(new NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(rmv)
            .setSmallIcon(R.drawable.ic_trasnparent)
            .setPriority(NotificationCompat.PRIORITY_LOW)
            .setContentIntent(pendingIntent)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setShowWhen(false)
            .setGroupSummary(false)
            .setAutoCancel(true);

    startForeground(intentID,builder.build());

}

// CODIGO DE LA DOC DE ANDROID para versiones +26
private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Notificacion +26";
        String description = "Notificacion para 26+";
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);

    }
}

NotificationIntentService(意向服务)

 @Override
protected void onHandleIntent(Intent intent) {
    if (intent != null) {
        handleNotfication();
    }
}


private void handleNotfication(){
    Intent intent = new Intent(this,wPerfil.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent);
}

ClassWhatIWantToLoad (activity)

这个是在onCreate

中执行的
  if (Build.VERSION.SDK_INT >= 27) {
        setShowWhenLocked(true);
        setTurnScreenOn(true);
    }else{
        getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                        WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

清单中授予的权限

 <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.ACTION_MANAGE_OVERLAY_PERMISSION" />

希望这部分代码能帮助您理解我的问题。我在其他一些问题中读到,人们和我一样做到了这一点,但我做不到。怎么了?如果您有其他解决方案,例如我可以在 lockScreen 中打开的小部件,请告诉我。

尝试从通知创建中删除

.setContentIntent(pendingIntent)

改为添加

rmv.setOnClickPendingIntent(R.id.layoutID, pendingIntent);

在通知上设置自定义视图之前。 R.id.layoutID 应该是您的自定义通知布局的相关布局的 ID。