簡體   English   中英

通過安全鎖屏上的通知啟動活動

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

我試圖在安全鎖屏中顯示通知中的活動(在非安全鎖屏中我實現了這一點)。

我遵循 StackOverflow 的一些問題和答案,但沒有解決我的問題。

我將發布部分代碼。

MainActivity(活動)

這是在onCreate() 中執行的

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

    } else {
        startService(intentService);
    }

通知服務(服務)

這是在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);

    }
}

通知意圖服務(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(活動)

這是在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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM