java - 带有 RemoteViews 的 Android 通知 - 具有与 RemoteViews 布局关联的 Activity

标签 java android notifications android-activity remoteview

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

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

我已经仔细检查过,在我的布局 xml 中,它似乎有正确的 Activity 类名:

<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>

在 list 文件中,在主应用程序主要 Activity 之后,还添加了通知 Activity :

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

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

下面是我如何在主应用程序 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 Activity 类定义如常:

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

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

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

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

最佳答案

您必须关联 Activity 思想 setOnClickPendingIntent 以从远程 View 启动 Activity ,如下所示...您可以在 RemoteView 中设置任何布局 ID 以单击.

 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

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

    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。一些事情如下...

这是我为我的一个媒体播放器应用程序使用的自定义远程 View 。它有三个按钮来处理点击。

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);
}
}

关于java - 带有 RemoteViews 的 Android 通知 - 具有与 RemoteViews 布局关联的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22585696/

相关文章:

android - 有没有办法让 View 绘制在所有其他 View 之上?

android - 如何从 FragmentActivity 更新 ListFragment 中的 ListView?

ios - UIUserNotificationSettings 拒绝快速接受多种通知类型

android - onClick() 通知打开 customactivity 后,我无法返回 mainactivity

java - 阻止操作系统将对象交换到 Java 中的虚拟内存/磁盘?

java - 变量 sc 可能尚未初始化

java - 我如何在Java中进行定时器登录并分析结果?

java - javax.ejb.SessionSynchronization 和 javax.transaction.Synchronization 之间的区别

android - 如何等待 WifiManager 连接,并确定是否成功/失败?

php - 如何使用 php 和 mysql 检查数据库字段创建 3 天后更改的状态?