简体   繁体   English

通过安全锁屏上的通知启动活动

[英]Start an activity from notification over the secure lockScreen

im stuck trying to show an activity from notification in secure lockscreen (in not secure lockscreen I achieve this).我试图在安全锁屏中显示通知中的活动(在非安全锁屏中我实现了这一点)。

I follow some questions an answers from StackOverflow but none resolve my problem.我遵循 StackOverflow 的一些问题和答案,但没有解决我的问题。

I will post parts of my code.我将发布部分代码。

MainActivity (activity) MainActivity(活动)

This is executed in onCreate()这是在onCreate() 中执行的

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

    } else {
        startService(intentService);
    }

NotificationService (service)通知服务(服务)

This is executed in onStartCommand (tried to in onCreate)这是在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 (IntentService)通知意图服务(IntentService)

 @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) ClassWhatIWantToLoad(活动)

This is executed in the onCreate这是在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);
    }

Permissions gived in Manifest清单中授予的权限

 <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" />

Hope this parts of code help you to understand my problem.希望这部分代码可以帮助您理解我的问题。 I read in some other questions people achieve this doing the same I do, but i cant.我在其他一些问题中读到了人们做同样的事情,但我不能。 What is wrong?怎么了? If you have another solution like a widget that I can open in lockScreen tell me please.如果您有其他解决方案,例如我可以在 lockScreen 中打开的小部件,请告诉我。

Try deleting from the notification creation尝试从通知创建中删除

.setContentIntent(pendingIntent)

And instead add而是添加

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

before you set the customview on the notification.在通知上设置自定义视图之前。 R.id.layoutID should be the ID of the relativelayout for your custom notification layout. R.id.layoutID 应该是您的自定义通知布局的相对布局的 ID。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM