Android 如何在锁屏上执行通知操作(单击)? TL;博士
如何在不解锁的情况下从锁屏发出一些工作的通知?单击操作、通知上的按钮或完整通知后,我希望执行API调用(无需键入解锁代码) 细节 目标 根据上的回答,我尝试发出通知,并在锁屏上执行操作,而不解锁设备。该操作不需要任何进一步的接口或交互(比如“发送API请求”) 地位 打开通知并单击“使用解锁的设备工作”。然而,当锁定时,我仍然需要首先输入解锁代码,所以要么是有新的事情发生,要么是我误解了它的工作方式 如果我理解正确,我可以将我的可见性设置为“public”来显示内容(这很有效),而不是定义一个动作(看起来不是公共的),我可以处理(现在可见)布局上的点击。我用下面的代码试过了,但显然不起作用 正如florian在下面建议的那样,我已经尝试将意图发送到我的应用程序和服务 代码 这是我开始通知的代码(这存在于活动中,为了方便起见,代码被缩短) 如上所述,我还尝试了florian建议的服务,将此作为一个电话:Android 如何在锁屏上执行通知操作(单击)? TL;博士,android,notifications,android-service,android-5.0-lollipop,lockscreen,Android,Notifications,Android Service,Android 5.0 Lollipop,Lockscreen,如何在不解锁的情况下从锁屏发出一些工作的通知?单击操作、通知上的按钮或完整通知后,我希望执行API调用(无需键入解锁代码) 细节 目标 根据上的回答,我尝试发出通知,并在锁屏上执行操作,而不解锁设备。该操作不需要任何进一步的接口或交互(比如“发送API请求”) 地位 打开通知并单击“使用解锁的设备工作”。然而,当锁定时,我仍然需要首先输入解锁代码,所以要么是有新的事情发生,要么是我误解了它的工作方式 如果我理解正确,我可以将我的可见性设置为“public”来显示内容(这很有效),而不是定义一个动
Intent yepIntent = new Intent(this, MyIntentService.class);
yepIntent.setAction("test");
yepIntent.putExtra("foo", true);
yepIntent.putExtra("bar", "more info");
PendingIntent yepPendingIntent = PendingIntent.getService(this, notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT);
//builder.addAction(R.drawable.abc_ic_menu_share_mtrl_alpha, "My Action", yepPendingIntent);
builder.setContentIntent(yepPendingIntent);
该操作没有显示在锁定屏幕上,因此我将其更改为上面看到的setContentIntent
。虽然结果是一样的,但对我来说没有任何操作:(尝试使用。
用您的意向服务替换您的意向目标:
Intent yepIntent = new Intent(context, MyIntentService.class);
yepIntent.putExtra("foo", true);
yepIntent.putExtra("bar", "more info");
PendingIntent yepPendingIntent = PendingIntent.getService(context, notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT);
notificationBuilder.addAction(R.drawable.icon_of_choice, "My Action", yepPendingIntent);
在清单中注册您的服务:
<service
android:name="app.great.mypackage.MyIntentService"
android:exported="false"/>
更新Nanne的反馈信息
诀窍似乎是
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
.setContentTitle("My notification")
.setContentText("Hello World!");
int notificationId = 1;
Intent yepIntent = new Intent(this, MyIntentService.class);
yepIntent.setAction("test");
yepIntent.putExtra("foo", true);
yepIntent.putExtra("bar", "more info");
PendingIntent yepPendingIntent = PendingIntent.getService(this, notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// doesn't show up on my lock-screen
//builder.addAction(R.drawable.abc_ic_menu_share_mtrl_alpha, "My Action", yepPendingIntent);
// asks for unlock code for some reason
//builder.setContentIntent(yepPendingIntent);
// Bingo
RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
view.setOnClickPendingIntent(R.id.notification_closebtn_ib, yepPendingIntent);
builder.setContent(view);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
.setContentTitle("My notification")
.setContentText("Hello World!");
int notificationId = 1;
Intent yepIntent = new Intent(this, MyIntentService.class);
yepIntent.setAction("test");
yepIntent.putExtra("foo", true);
yepIntent.putExtra("bar", "more info");
PendingIntent yepPendingIntent = PendingIntent.getService(this, notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// doesn't show up on my lock-screen
//builder.addAction(R.drawable.abc_ic_menu_share_mtrl_alpha, "My Action", yepPendingIntent);
// asks for unlock code for some reason
//builder.setContentIntent(yepPendingIntent);
// Bingo
RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
view.setOnClickPendingIntent(R.id.notification_closebtn_ib, yepPendingIntent);
builder.setContent(view);
将我链接的问题()的答案和上面@florian_barth给出的答案结合起来,我就成功了 诀窍似乎是
remoteview
方法,而不是将意图添加为操作或contentIntentNotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
.setContentTitle("My notification")
.setContentText("Hello World!");
int notificationId = 1;
Intent yepIntent = new Intent(this, MyIntentService.class);
yepIntent.setAction("test");
yepIntent.putExtra("foo", true);
yepIntent.putExtra("bar", "more info");
PendingIntent yepPendingIntent = PendingIntent.getService(this, notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// doesn't show up on my lock-screen
//builder.addAction(R.drawable.abc_ic_menu_share_mtrl_alpha, "My Action", yepPendingIntent);
// asks for unlock code for some reason
//builder.setContentIntent(yepPendingIntent);
// Bingo
RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
view.setOnClickPendingIntent(R.id.notification_closebtn_ib, yepPendingIntent);
builder.setContent(view);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setOngoing(true)
.setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
.setContentTitle("My notification")
.setContentText("Hello World!");
int notificationId = 1;
Intent yepIntent = new Intent(this, MyIntentService.class);
yepIntent.setAction("test");
yepIntent.putExtra("foo", true);
yepIntent.putExtra("bar", "more info");
PendingIntent yepPendingIntent = PendingIntent.getService(this, notificationId, yepIntent, PendingIntent.FLAG_CANCEL_CURRENT);
// doesn't show up on my lock-screen
//builder.addAction(R.drawable.abc_ic_menu_share_mtrl_alpha, "My Action", yepPendingIntent);
// asks for unlock code for some reason
//builder.setContentIntent(yepPendingIntent);
// Bingo
RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
view.setOnClickPendingIntent(R.id.notification_closebtn_ib, yepPendingIntent);
builder.setContent(view);
它还与广播接收器和设置动作一起工作
PendingIntent pendingIntent = PendingIntent.getBroadcast(..
builder.addAction(..
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
在锁定屏幕上,向下滑动通知以将其展开,然后点击操作区域以调用广播接收器,而不解锁手机。您是否打算在点击通知上禁用锁定功能?不,我只是尝试执行一些代码,在这种情况下的API调用是一种安全漏洞。如果您获得访问权限o应用程序被锁定时存在漏洞。就问题而言,您可以禁用键盘防护,但不建议您禁用。不,这是一种故意设置来执行操作。锁定时您可以执行一些不安全的操作,例如将播放器设置为暂停、拍照,如果您设置了我的应用程序,则无法执行打开你的灯(如果你已经设置了应用程序)。请不要随意将事情称为“安全漏洞”。在这种情况下,意图在通知中注册为操作,为了将其用作内容意图,只需使用您在原始代码中使用的NotificationBuilder操作。但作为问题的答案:您是说是的,服务将在活动不起作用的地方工作?是的,我的猜测是,该活动“链接”到用户界面和交互,为此,您需要解锁屏幕。将上面的代码片段输入到您的代码中,让我们找出答案:)我会的!这可能需要我几个小时,因为在私人项目上工作并不是真正的日常工作;)非常正确,期待结果:)