自定义通知,带两个按钮

时间:2015-08-27 14:15:22

标签: android button notifications

嗨,在我的项目中,我正在显示通知,两个按钮1用于同步,另一个用于关闭。我已经使用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>

这是我的BroadcastReceiver

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

1 个答案:

答案 0 :(得分:1)

这是因为您可能将相同的意图ID传递给待处理的Intent并为这两个操作获取公共广播。

将以下行中的ID更改为1 PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this,0,             switchIntent,0);

喜欢这个 PendingIntent pendingSwitchIntent = PendingIntent.getBroadcast(this,1,             switchIntent,0);

现在应该可以正常使用了!