Android的通知与RemoteViews - 与RemoteViews布局具有活性的相关活性、布局、通知、Android

由网友(小鬼绅士)分享简介:我一直在研究如何创建使用远程视窗定制版图通知。到目前为止,我能够与内容查看和bigContentView指着一个自定义布局的XML一个远程视窗创建通知。然而,并没有发生,是有活性的(自定义布局关联)开始,创建此远程视窗时。I've been researching on how to create custom-l...

我一直在研究如何创建使用远程视窗定制版图通知。 到目前为止,我能够与内容查看和bigContentView指着一个自定义布局的XML一个远程视窗创建通知。然而,并没有发生,是有活性的(自定义布局关联)开始,创建此远程视窗时。

I've been researching on how to create custom-layout notification using RemoteView. So far, I am able to create 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,它似乎有正确的活动类的名称:

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>

我希望,当通知使用RemoteViews为它的内容,这RemoteViews将启动一个连接到它的布局定义的活动。然而,它似乎没有。

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

下面是我创建的主要应用程序的活动的通知:

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

任何人都可以指向我,我缺少的是什么,或者如果我误会了RemoteViews可以做什么?我的理解是RemoteViews应,一旦创建,调用与它的布局相关的活动。或者 - 有一些API,我已经错过了明确可以设置RemoteViews的意图

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

我已经发现迄今只设置一次,用户触摸通知,它基本上只是推出一项活动内容意图。我正在寻找的是处理触摸到里面的一些定制版图通知UI元素,不考虑推出一项活动,其中用户点击通知表面上。

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 user clicks on notification surface.

例如,如果我有3个图标(ImageView的),其中通知使用RemoteViews,我很希望能够处理触摸它们中的每一个。我无法想象这是不可能的,如果它不是,什么是有RemoteViews在通知之点: - )

For example, if I have 3 icons (ImageView) in a RemoteViews 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 RemoteViews in notification :-)

在此先感谢

推荐答案

您必须将活动相关联思想setOnClickPendingIntent推出从如下远程视图的活动...您可以设置任何布局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);

提供+ ID /你用layout_id的相对布局。

provide an +id/layout_id to the relative layout 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个按键,你必须创建一个自定义远程视窗,并使用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);
}
}
阅读全文

相关推荐

最新文章