使用RemoteViews的Android通知 - 具有与RemoteViews布局相关的活动

Sin*_*isa 9 java notifications android remoteview android-activity

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

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

我已经双重检查并在我的布局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>
Run Code Online (Sandbox Code Playgroud)

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

<activity
    android:name=".LLMNotificationActivity"
    android:label="@string/app_name">
    <intent-filter>
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)

我希望当通知RemoteView用于其内容时,这RemoteView将启动附加到其布局定义的活动.但似乎没有.

以下是我在主应用程序中创建通知的方法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);  
}
Run Code Online (Sandbox Code Playgroud)

LLMNotificationActivity 活动类定义为通常:

public class LLMNotificationActivity extends Activity {
    .... etc.... constructor, some button on-click handlers, nothing spectacular...
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以向我指出我所缺少的东西,或者我是否误解了RemoteView可以做什么?我的理解是RemoteView,一旦创建,就应该调用与其布局相关的活动.或者 - 是否有一些我错过了明确可以设置意图的API RemoteView

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

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

Lib*_*bin 8

您必须setOnClickPendingIntent将从远程视图启动活动的活动关联起来,如下所示...您可以在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);
Run Code Online (Sandbox Code Playgroud)

提供+id/layout_idRelativeLayout你的使用.

如果您必须在用户单击通知时启动活动,则必须使用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());
Run Code Online (Sandbox Code Playgroud)

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

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

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