0
嗨,在我的項目中,我顯示兩個按鈕1的通知是爲syn,另一個是關閉。我用的PendingIntent來得到通知的數據,但是當我點擊任何按鈕,它給人一種相同的值plz幫助我在這兩個按鈕的自定義通知
有我的編碼
@SuppressWarnings("deprecation")
private void startNotification(){
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager =
(NotificationManager) getSystemService(ns);
Notification notification = new Notification(R.drawable.ic_launcher, null,
System.currentTimeMillis());
RemoteViews notificationView = new RemoteViews(getPackageName(),
R.layout.number);
//the intent that is started when the notification is clicked (works)
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.contentView = notificationView;
notification.contentIntent = pendingNotificationIntent;
notification.flags |= Notification.FLAG_NO_CLEAR;
//this is the intent that is supposed to be called when the
//button is clicked
Intent switchIntent = new Intent(this, switchButtonListener.class);
switchIntent.putExtra("do_action", "close");
PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this, 0,
switchIntent, 0);
notificationView.setOnClickPendingIntent(R.id.close,
pendingSwitchIntent);
Intent switchIntentsyn = new Intent(this, switchButtonListener.class);
switchIntentsyn.putExtra("do_action", "syn");
PendingIntent pendingSwitchIntentsyn = PendingIntent.getBroadcast(this, 0,
switchIntentsyn, 0);
notificationView.setOnClickPendingIntent(R.id.startsyn,
pendingSwitchIntentsyn);
notificationManager.notify(100, notification);
}
,這是我的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" >
<ImageView
android:id="@+id/notifiation_image"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="30"
android:contentDescription="Title name"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/appName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:gravity="center"
android:text="test"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical"
>
<Button
android:id="@+id/startsyn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Syn now" />
<Button
android:id="@+id/close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="close" />
</LinearLayout>
</LinearLayout>
這是我的廣播接收器
public static class switchButtonListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = (String) intent.getExtras().get("do_action");
Log.i("tag", "action :"+action);
if (action != null) {
if (action.equalsIgnoreCase("syn")) {
// for example play a music
Log.i("tag", "inside syn method");
} else if (action.equalsIgnoreCase("close")) {
// close current notification
Log.i("tag", "inside close method");
}
}
}
}
這是我的Android清單
<receiver android:name="com.example.testdemo.MainActivity$switchButtonListener" />
我出去把當過我點擊這兩個按鈕
08-27 19:35:01.347: I/tag(8547): action :close
08-27 19:35:01.347: I/tag(8547): inside close method
notificationView.setOnClickPendingIntent(R.id.close, pendingSwitchIntent);和 notificationView.setOnClickPendingIntent(R.id.startsyn, pendingSwitchIntentsyn);你設置了兩次「notificationView.OnClickPendingIntent」,我認爲這一定是失敗的原因。 –
@sedpol我用這隻用於onclickpendingintent notificationView.setOnClickPendingIntent(R.id.startsyn, pendingSwitchIntentsyn); notificationView.setOnClickPendingIntent(R.id.close, pendingSwitchIntent); – Jagan
那麼如何獲得onclick這兩個按鈕 – Jagan