通过安全锁定屏幕上的通知启动活动 [英] Start an activity from notification over the secure lockScreen
问题描述
im试图在安全锁定屏幕中尝试显示来自通知的活动(在非安全锁定屏幕中,我实现了这一点).
im stuck trying to show an activity from notification in secure lockscreen (in not secure lockscreen I achieve this).
我关注了StackOverflow的一些问题,但没有一个能解决我的问题.
I follow some questions an answers from StackOverflow but none resolve my problem.
我将发布部分代码.
这是在 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中执行)
NotificationService (service)
This is executed in onStartCommand (tried to in 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)
@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中打开的小部件,请告诉我.
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.
推荐答案
尝试从通知创建中删除
.setContentIntent(pendingIntent)
然后添加
rmv.setOnClickPendingIntent(R.id.layoutID, pendingIntent);
在通知上设置customview之前. R.id.layoutID应该是自定义通知布局的相对布局的ID.
before you set the customview on the notification. R.id.layoutID should be the ID of the relativelayout for your custom notification layout.
这篇关于通过安全锁定屏幕上的通知启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!