这是我的通知,我在点击通知按钮时需要事件。 setOnClickPendingIntent对我不起作用。 R.layout.mynotification是在代码下。 (我不想要addAction)。
Intent intent = new Intent(Fragmentz.ctx, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(Fragmentz.ctx, (int) System.currentTimeMillis(), intent, 0);
// Build notification
RemoteViews notificationView = new RemoteViews(Fragmentz.ctx.getPackageName(),
R.layout.mynotification);
Notification noti = new Notification.Builder(Fragmentz.ctx)
.setContentTitle("Radio")
.setContentText("").setSmallIcon(R.drawable.logo_notif)
.setContentIntent(pIntent)
.build();
noti.contentView = notificationView;
NotificationManager notificationManager = (NotificationManager) Fragmentz.ctx.getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
R.layout.mynotification.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="100" >
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:text="Play"
android:id="@+id/not_button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:src="@drawable/img_btn_play"
android:background="@null"
/>
</LinearLayout>
答案 0 :(得分:0)
您应该通过pendingIntent添加一个意图,如下所示:
Intent intent = new Intent(context, desiredClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
注意标志FLAG_UPDATE_CURRENT并检查它是否适合您的情况。
答案 1 :(得分:0)
这对我有用:
// Creates an explicit intent for an ResultActivity to receive.
Intent resultIntent = new Intent(Fragmentz.ctx, NotificationReceiverActivity.class);
// This ensures that the back button follows the recommended convention for the back key.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(mContext);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
答案 2 :(得分:0)
这应该对你有用
添加此行
Intent listener = new Intent(ctx, NotificationClickHandler.class);
PendingIntent pListener = PendingIntent.getActivity(ctx, 0, listener, 0);
notificationView.setOnClickPendingIntent(R.id.not_button, pListener);
这在你的清单中
<receiver android:name="com.xxx.NotificationClickHandler" /> // Use your package name
最后创建一个类NotificationClickHandler
public class NotificationClickHandler extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//TODO: do your desired work here
}
}
注意:而不是Activity
这也可以使用BroadcastReceiver
来完成。
答案 3 :(得分:0)
这个怎么样?
RemoteViews contentView = new RemoteViews(c
.getPackageName(), R.layout.notification_custom);
contentView.setTextViewText(R.id.buttonNotificationAccept, "Ok");
contentView.setOnClickPendingIntent(R.id.buttonNotificationAccept,
resultPendingIntent);