带有 RemoteViews 的 Android 通知 - 具有与 RemoteViews 布局相关联的活动 [英] Android notification with RemoteViews - having activity associated with RemoteViews layout

查看:23
本文介绍了带有 RemoteViews 的 Android 通知 - 具有与 RemoteViews 布局相关联的活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究如何使用 RemoteView 创建自定义布局通知.

I've been researching on how to create custom-layout notification using RemoteView.

到目前为止,我能够使用 contentViewbigContentView 创建一个通知,该通知指向带有自定义布局 xml 的 RemoteView.但是,没有发生的是在创建此 RemoteView 时启动 Activity(与自定义布局相关联).

So far, I am able to create a notification with contentView and bigContentView pointing to a RemoteView with a custom layout xml. However, what does not happen, is to have Activity (associated with custom layout) started when this RemoteView is created.

我仔细检查了一下,在我的布局 xml 中,它似乎具有正确的 Activity 类名:

I've double checked and in my layout xml, it appears to have correct Activity class name:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="0dp"
    android:paddingLeft="0dp"
    android:paddingRight="0dp"
    android:paddingTop="0dp"
    tools:context=".LLMNotificationActivity" >

..... the rest are standard layout items: images, buttons and text

</RelativeLayout>

在清单文件中,在主应用程序主活动之后,还添加了通知活动:

In manifest file, right after main application main activity, notification activity is also added:

<activity
    android:name=".LLMNotificationActivity"
    android:label="@string/app_name">
    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

我希望当通知使用 RemoteView 作为其内容时,这个 RemoteView 将启动附加到其布局定义的活动.然而它似乎不是.

I would expect when notification uses RemoteView for its content, that this RemoteView will launch activity that is attached to its layout definition. However it appears not.

以下是我在主应用程序 Activity 中创建通知的方法:

Here is how I create a notification in main application Activity:

protected void startNoti() {
    if( noti!=null ) return;

    Context context = getApplicationContext();  

    RemoteViews contentView = new RemoteViews(context.getPackageName(),R.layout.activity_noti1);

    Notification.Builder notibuilder = new Notification.Builder(context);
    notibuilder.setContentTitle(" ");
    notibuilder.setContentText(" ");
    notibuilder.setSmallIcon(R.drawable.ic_launcher);
    notibuilder.setOngoing(true);

    manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    noti = notibuilder.build();

    noti.contentView = contentView;

    manager.notify(NOTIFICATION_ID, noti);  
}

LLMNotificationActivity 活动类像往常一样定义:

LLMNotificationActivity activity class is defined as usual:

public class LLMNotificationActivity extends Activity {
    .... etc.... constructor, some button on-click handlers, nothing spectacular...
}

任何人都可以指出我缺少什么,或者我是否误解了 RemoteView 可以做什么?我的理解是 RemoteView 应该在创建后调用与其布局关联的活动.或者 - 是否有一些我遗漏的 API 可以明确设置 RemoteView 的意图?

Can anyone point to me what I am missing or if I have misunderstood what RemoteView can do? My understanding is that RemoteView should, once created, invoke activity associated with its layout. Or - is there some API I've missed that explicitly can set intent of the RemoteView?

到目前为止我发现的只是设置内容Intent,一旦用户触摸通知,它基本上只是启动一个Activity.我正在寻找的是处理自定义布局通知中某些 UI 元素的触摸,而不是启动 Activity 无论用户点击通知表面的位置.

What I have found so far are only setting content Intent which basically just launches an Activity once user touches notification. What I'm looking for is to handle touches to some of UI elements inside custom-layout notification, not to launch an Activity regardless where the user clicks on notification surface.

例如,如果我在通知使用的 RemoteView 中有 3 个图标(即 ImageView),我希望能够处理每个图标上的触摸他们.我无法想象这是不可能的,如果不是,在通知中使用 RemoteView 有什么意义?

For example, if I have 3 icons (i.e. ImageView) in a RemoteView which notification uses, I'd like to be able to handle touch on each one of them. I can't imagine this wouldn't be possible as if it's not, what's the point of having RemoteView in notification?

推荐答案

您必须将活动关联到 setOnClickPendingIntent 以从远程视图启动活动,如下所示...您可以设置任何RemoteView 中的布局 id 以单击.

You have to associate the activity thought setOnClickPendingIntent to launch the activity from remote view as below...You can set any of the layout id in the RemoteView to click.

 Intent intent = new Intent(context, YourActivity.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,   PendingIntent.FLAG_UPDATE_CURRENT);
 RemoteViews removeWidget = new RemoteViews(context.getPackageName(), R.layout.your_layout);
 removeWidget.setOnClickPendingIntent(R.id.layout_id, pendingIntent);

为您使用的 RelativeLayout 提供一个 +id/layout_id.

provide an +id/layout_id to the RelativeLayout your using.

如果您必须在用户点击通知时启动活动,那么您必须使用 PendingIntent 作为....

If you have to launch the activity when user click on the notification, then you have to use PendingIntent as....

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("title")
                    .setContent(mRemoteControl);
    Intent notificationIntent = new Intent(this, YourActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(
            this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    mBuilder.setContentIntent(contentIntent);
    mNM.notify(1000,mBuilder.build());

对于 3 个按钮,您必须使用创建自定义 RemoteView 并使用 PendingIntent.一些事情如下...

For 3 buttons , You have to use create a custom RemoteView and use PendingIntent. some thing as below...

这是我用于我的媒体播放器应用程序之一的自定义远程视图.它有三个按钮来处理点击.

Here is the custom remote view i am using for one of my media player app. it has three button to handler click.

public class RemoveControlWidget extends RemoteViews
{
private final Context mContext;

public static final String ACTION_PLAY = "com.mediabook.app.ACTION_PLAY";

public static final String ACTION_PREVIOUS = "com.mediabook.app.ACTION_PREVIOUS";

public static final String ACTION_NEXT = "com.mediabook.app.ACTION_NEXT";

public RemoveControlWidget(Context context , String packageName, int layoutId)
{
    super(packageName, layoutId);
    mContext = context;
    Intent intent = new Intent(ACTION_PLAY);
    PendingIntent pendingIntent = PendingIntent.getService(mContext.getApplicationContext(),100,
            intent,PendingIntent.FLAG_UPDATE_CURRENT);
    setOnClickPendingIntent(R.id.play_control,pendingIntent);
    setOnClickPendingIntent(R.id.pause_control,pendingIntent);
    intent = new Intent(ACTION_PREVIOUS);
    pendingIntent = PendingIntent.getService(mContext.getApplicationContext(),101,
            intent,PendingIntent.FLAG_UPDATE_CURRENT);
    setOnClickPendingIntent(R.id.previous_control,pendingIntent);
    intent = new Intent(ACTION_NEXT);
    pendingIntent = PendingIntent.getService(mContext.getApplicationContext(),102,
            intent,PendingIntent.FLAG_UPDATE_CURRENT);
    setOnClickPendingIntent(R.id.next_control,pendingIntent);
}
}

这篇关于带有 RemoteViews 的 Android 通知 - 具有与 RemoteViews 布局相关联的活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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