具有3个按钮的Android RemoteView通知具有意外行为

时间:2015-06-25 14:50:41

标签: android android-notifications remoteview

我使用带有三个按钮和标签的RemoteView创建自定义通知。 当我点击一个按钮时,它正在执行另一个按钮的PendingIntent。这很奇怪。

当我点击4G按钮时,它正在执行Close PendingIntent

当我点击3G按钮时,它正在执行4G PendingIntent

当我点击2G按钮时,它正在执行3G PendingIntent

以下代码:

RemoteViews remoteWidget = new RemoteViews(context.getPackageName(), R.layout.layout_notification);

    remoteWidget.setOnClickPendingIntent(R.id.btn4G, getSwitchBandPendingIntent(context, "4G", 0));
    remoteWidget.setOnClickPendingIntent(R.id.btn3G, getSwitchBandPendingIntent(context, "3G", 1));
    remoteWidget.setOnClickPendingIntent(R.id.btn2G, getSwitchBandPendingIntent(context, "2G", 2));

    Intent closeIntent = new Intent("com.degelo.action.CLOSE_NOTIFICATION");
    PendingIntent closePendingIntent = PendingIntent.getBroadcast(context, 3, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    remoteWidget.setOnClickPendingIntent(R.id.btnClose, closePendingIntent);
    //remoteWidget.setTextViewText(R.id.labelCurrentPreferredBand, "Teste");

    Notification.Builder mBuilder =
            new Notification.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("Network Switcher")
                    .setContent(remoteWidget)
                    .setOngoing(true)
                    .setOnlyAlertOnce(true);        

    NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);        
    nm.notify(1000, mBuilder.build());

getSwitchBandPendingIntent code:

private static PendingIntent getSwitchBandPendingIntent(Context context, String band, int requestCode) {
    Intent intent = new Intent(context, NetworkSwitcherService.class);
    intent.putExtra("BAND", band);
    PendingIntent pendingIntent = PendingIntent.getService(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    return pendingIntent;
}

最后是布局xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/notificationLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView android:id="@+id/labelCurrentPreferredBand"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Teste"
          style="@style/NotificationTitle" />

<LinearLayout android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="horizontal">

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2G"
            android:id="@+id/btn2G" />

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="3G"
            android:id="@+id/btn3G" />

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="4G"
            android:id="@+id/btn4G" />

    <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Close"
            android:id="@+id/btnClose" />
</LinearLayout> 
</LinearLayout>

textview样式:

<style name="NotificationText" parent="android:TextAppearance.StatusBar.EventContent" />

感谢您的帮助。

0 个答案:

没有答案