通知操作按钮,在锁屏无法点击 [英] Notification action button not clickable in lock screen

查看:261
本文介绍了通知操作按钮,在锁屏无法点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了更好地支持Android的通知5,我现在设置我的应用程序的通知的可见性为公开。审议关于<一个答案后, href=\"http://stackoverflow.com/questions/26932457/lollipop-notification-setvisibility-does-not-work\">Lollipop通知setVisibility()不工作?,现在如预期所显示的通知。然而,当我想点击该通知的操作按钮,我首先要解开这不应该要求该设备。 (动作表明,一个密码数据库被锁定,同时操作按钮将锁定数据库。)

In order to better support Android 5 notifications, I am now setting my app's notification visibilty to "public". After considering the answers on Lollipop Notification setVisibility() Does Not Work?, the notification is now displayed as expected. However, when I want to click the action button of the notification, I first have to unlock the device which should not be required. (The action shows that a password database is unlocked and the action button will lock the database.)

这是code我使用的是创建通知(使用Xamarin的Mono Android版):

This is the code I am using the creating the notification (using Xamarin's Mono for Android):

NotificationCompat.Builder builder =
                new NotificationCompat.Builder(this)
                    .SetOngoing(true)
                    .SetSmallIcon(Resource.Drawable.ic_notify)
                    .SetLargeIcon(...)
                    .SetVisibility((int)Android.App.NotificationVisibility.Public)
                    .SetContentTitle(...)
                    .SetContentText(...);

builder.AddAction(Resource.Drawable.ic_action_lock, GetString(Resource.String.menu_lock), PendingIntent.GetBroadcast(this, 0, new Intent(Intents.LockDatabase), PendingIntentFlags.UpdateCurrent));

其中,这个是服务实例。

我知道MediaStyle的通知具有可点击的按钮,但感觉就像一个黑客使用MediaStyle即使它不是媒体。有什么办法可以让我的行动从锁定屏幕使用吗?

I know that MediaStyle notifications have clickable buttons, but it feels like a hack to use MediaStyle even though it's not about media. Is there any way I can make my action usable from the lock screen?

推荐答案

而不是增加一个动作,定义自己的通知,布局和连接的PendingIntent通过远程视窗解雇了这一点。
(下面的例子是基于全息的外观和感觉,并仍然需要更新棒棒糖,你可以找到所有TE正确的资源,你的SDK的机器人-21 /数据/ res文件夹)

Instead of adding an action, define your own notification layout and connect a pendingIntent to fire to that via RemoteView. (The example below is based on the Holo look and feel and would still need to updated for lollipop. You can find all te correct resources in the android-21/data/res folder of your sdk)

// NOTE: while creating pendingIntent: requestcode must be different!
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(myService)
        .setSmallIcon(R.drawable.notification_icon).setContentTitle("My Title")
        .setContentText("Service running in the background");
Intent openIntent = new Intent(MainActivity.this, MainActivity.class);
PendingIntent pOpenIntent = PendingIntent.getActivity(this, 0, openIntent, 0);
mBuilder.setContentIntent(pOpenIntent);

// Notification with exit button if supported
String ACTION_NOTIFICATION_EXITACTIVITY = "com.jmols.example.exitactivity";
Intent exitIntent = new Intent();
exitIntent.setAction(ACTION_NOTIFICATION_EXITACTIVITY);
PendingIntent pExitIntent = PendingIntent.getBroadcast(this, 1, exitIntent, 0);
RemoteViews view = new RemoteViews(getPackageName(), R.layout.notification_discoveryservice);
view.setOnClickPendingIntent(R.id.notification_closebtn_ib, pExitIntent);
mBuilder.setContent(view);

通过通知布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:internal="http://schemas.android.com/apk/prv/res/android"
    android:id="@+id/status_bar_latest_event_content"
    android:layout_width="match_parent"
    android:layout_height="64dp"
    internal:layout_maxHeight="64dp"
    internal:layout_minHeight="64dp" >

    <ImageView
        android:id="@+id/notification_icon_iv"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:padding="10dp"
        android:layout_alignParentLeft="true"
        android:scaleType="center"
        android:src="@drawable/notification_icon"
        android:background="#3333B5E5" />

    <ImageButton
        android:id="@+id/notification_closebtn_ib"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:scaleType="centerInside"
        android:src="@drawable/notification_exitbtn"
        android:background="@drawable/notification_imagebtn_bg"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="fill_vertical"
        android:gravity="top"
        android:minHeight="64dp"
        android:layout_toRightOf="@id/notification_icon_iv"
        android:layout_toLeftOf="@id/notification_closebtn_ib"
        android:orientation="vertical"
        android:paddingBottom="2dp"
        android:paddingEnd="8dp"
        android:paddingTop="2dp" >

        <TextView
            android:id="@+id/notification_title_tv"
            style="@android:style/TextAppearance.StatusBar.EventContent.Title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:ellipsize="marquee"
            android:paddingTop="6dp"
            android:singleLine="true"
            android:text="JMols Service" />

        <TextView
            android:id="@+id/notification_contenttext_tv"
            style="@android:style/TextAppearance.StatusBar.EventContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="8dp"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:text="Service running in the background" />

    </LinearLayout>

</RelativeLayout>

和通知背景是:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="@android:integer/config_mediumAnimTime">

    <item android:state_pressed="true"  android:drawable="@drawable/notification_bg_normal_pressed" />
    <item android:state_pressed="false" android:drawable="@drawable/notification_bg_normal" />
</selector>


和通知ImageButton的背景是:

And the notification imagebutton background is:

<selector xmlns:android="http://schemas.android.com/apk/res/android"
    android:exitFadeDuration="@android:integer/config_mediumAnimTime">

    <item android:state_pressed="true"  android:drawable="@drawable/notification_imagebtn_bg_normal_pressed" />
    <item android:state_pressed="false" android:drawable="@drawable/notification_imagebtn_bg_normal" />
</selector>


这篇关于通知操作按钮,在锁屏无法点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆