如何在锁定屏幕上执行通知操作(单击)?

Nan*_*nne 24 notifications android lockscreen android-service android-5.0-lollipop

TL; DR

如何在没有解锁的情况下从锁定屏幕发出一些有效的通知?单击操作,通知上的按钮或完成通知后,我想进行API调用(无需键入解锁代码)

细节

目标

基于这个问题的答案,我试图通过一个在锁定屏幕上工作的动作发出通知,而无需解锁设备.该操作不需要任何进一步的界面或交互(想想"发送API请求").

状态

通知和单击可以使用未锁定的设备.然而,当锁定时我仍然需要先输入解锁代码,所以要么有新的事情发生,要么我误解了它应该工作的方式.

如果我理解正确,我可以将我的可见性设置为'public'来显示内容(这是有效的),而不是定义一个动作(似乎不公开)我可以处理(现在可见)布局的点击.我尝试使用下面的代码,但显然它不起作用.

我已经尝试将意图发送到我的应用程序和服务,正如弗洛里安建议的那样.

这是我开始通知的代码(它存在于一个Activity中,代码缩短了以方便您使用)

private void startNotification() {

    NotificationCompat.Builder builder = 
            new NotificationCompat.Builder(this)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setOngoing(true)
            .setSmallIcon(R.drawable.abc_ic_menu_share_mtrl_alpha)
            .setContentTitle("title text")
            .setContentText("content text");

    Intent openIntent = new Intent(MyMainActivity.this, MyMainActivity.class);
    openIntent.setAction("some_string");
    PendingIntent pOpenIntent = PendingIntent.getActivity(this, 0, openIntent, 0);
    builder.setContentIntent(pOpenIntent);

    RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification);
    builder.setContent(view);

    NotificationManager mNotificationManager =
            (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotificationManager.notify(id, builder.build());

}
Run Code Online (Sandbox Code Playgroud)

如上所述,我也尝试过florian建议的服务,并将其作为电话:

    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);
Run Code Online (Sandbox Code Playgroud)

该操作没有显示在锁定屏幕上,因此我将其更改为setContentIntent您在上面看到的内容.结果是一样的,但对我没有任何动作:(

Flo*_*rth 16

尝试使用IntentService.用意图服务替换您的意图目标:

    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);
Run Code Online (Sandbox Code Playgroud)

在Manifest中注册您的服务:

  <service
        android:name="app.great.mypackage.MyIntentService"
        android:exported="false"/>
Run Code Online (Sandbox Code Playgroud)

您的服务可能如下所示:

public class MyIntentSerice extends IntentService {
    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("myapp", "I got this awesome intent and will now do stuff in the background!");
        // .... do what you like
    }
}
Run Code Online (Sandbox Code Playgroud)

更新来自Nanne的反馈

诀窍似乎是

  1. 使用服务
  2. 不是将intent作为action或contentIntent添加,而是使用RemoteViews方法.

合并它将是:

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);
Run Code Online (Sandbox Code Playgroud)


Nan*_*nne 5

结合我链接的问题的答案(通知操作按钮在锁定屏幕中不可点击)和上面@florian_barth 给出的答案,我得到了它的工作

诀窍似乎是

  1. 使用服务
  2. 添加意图不是作为一个动作或一个 contentIntent,而是作为一个RemoteViews方法。

结合起来将是:

    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);
Run Code Online (Sandbox Code Playgroud)