Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
具有3个按钮的Android RemoteView通知具有意外行为_Android_Android Notifications_Remoteview - Fatal编程技术网

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

具有3个按钮的Android RemoteView通知具有意外行为,android,android-notifications,remoteview,Android,Android Notifications,Remoteview,我正在使用带有三个按钮和一个标签的RemoteView创建一个自定义通知。 当我点击一个按钮时,它正在执行另一个按钮的PendingEvent。这很奇怪 当我点击4G按钮时,它正在执行Close PendingEvent 当我点击3G按钮时,它正在执行4G PendingEvent 当我点击2G按钮时,它正在执行3G PendingEvent 代码如下: RemoteViews remoteWidget = new RemoteViews(context.getPackageName(), R.

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

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

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

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

代码如下:

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());
GetSwitchBandPendingEvent代码:

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>

文本视图的样式:

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

谢谢你的帮助