如何在自定义通知中处理按钮单击

时间:2015-08-10 01:38:50

标签: android android-layout android-notifications

我尝试使用RemoteView按钮创建自定义通知,该按钮将执行操作而不关闭通知抽屉,类似于媒体点击工作的方式。我可以使用以下代码生成自定义通知:

public void setListeners(final RemoteViews view){

    //Opens up an activity, closes the notification drawer, and performs the action

    Intent skip = new Intent(getApplicationContext(), NotificationReturnSlot.class);
    skip.putExtra("DO", "Skip");
    skip.putExtra("remoteview", view);
    PendingIntent btn2 = PendingIntent.getActivity(getApplicationContext(), 1, skip, 0);
    view.setOnClickPendingIntent(R.id.skip, btn2);

}

public void setAsUpdate(){

    remoteView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.test);
    remoteView.setTextViewText(R.id.content, "This is some text");
    remoteView.setTextViewText(R.id.name, "The Title");

    NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(getApplicationContext())
            .setSmallIcon(R.drawable.temparrowicon);

    setListeners(remoteView);
    nBuilder.setContent(remoteView);

    int mNotificationId = 1;
    NotificationManager mNotifyMgr = (NotificationManager)     getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(mNotificationId, nBuilder.build());

}

与XML一起使用:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#000000">

<Button
    android:id="@+id/test"
    android:layout_width="65dp"
    android:layout_height="wrap_content"
    android:text="Test"
    style="?android:attr/borderlessButtonStyle"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginTop="6dp"
    android:textColor="#ffffff" />

<ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/imageView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:background="#00bbff" />

<TextView
    android:layout_width="155dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/content"
    android:singleLine="true"
    android:layout_toRightOf="@+id/imageView"
    android:layout_alignRight="@+id/name"
    android:layout_alignEnd="@+id/name" />

<TextView
    android:layout_width="155dp"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/name"
    android:textColor="#ffffff"
    android:singleLine="true"
    android:layout_alignBottom="@+id/imageView"
    android:layout_toRightOf="@+id/imageView"
    android:layout_toLeftOf="@+id/test"
    android:layout_toStartOf="@+id/test" />

它显示带按钮的通知,但我无法在不打开活动的情况下使用该按钮,这将关闭通知抽屉。有没有办法在不关闭通知抽屉的情况下执行操作,就像点击媒体一样?

感谢所有帮助。

由于编辑,某些ID名称可能不正确。

0 个答案:

没有答案