【问题标题】:click listener on android widget Button not working单击android小部件上的侦听器按钮不起作用
【发布时间】:2015-11-20 04:21:53
【问题描述】:

我正在开发一个 android 项目并创建了一个用于切换蓝牙的小部件(即,如果关闭蓝牙,则按下小部件打开蓝牙,反之亦然)。但问题是按钮的单击侦听器不起作用,当我单击主屏幕上的小部件时,它什么也不做。
我做了很多谷歌搜索并尝试了几乎所有的解决方案,但不幸的是没有对我有用。我无法理解为什么我的代码不起作用。

这是项目的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gmail.redprince007.togglebluetooth" >

    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.BLUETOOTH"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name=".BluetoothToggle" >
            <intent-filter>
                <action                         android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/bluetooth_toggle_info" />
            </receiver>
    </application>

</manifest>

小部件布局的xml文件是:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="@dimen/widget_margin">

    <Button
    android:id="@+id/btnToggleBluetooth"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bluetooth_on" />

</RelativeLayout>  

而 AppWidget-proviver.xml 是:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/bluetooth_toggle"
android:initialLayout="@layout/bluetooth_toggle"
android:minHeight="40dp"
android:minWidth="40dp"
android:previewImage="@drawable/ic_bluetooth_icon"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen|keyguard">

</appwidget-provider>  

BluetoothOnOff.Java 是:

public class BluetoothToggle extends AppWidgetProvider {
    private static final String TAG = "BluetoothToggle";
    private final String toggleBluetoothAction = "ToggleBluetooth";

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Log.d(TAG, "onUpdate()");
        // There may be multiple widgets active, so update all of them
        final int N = appWidgetIds.length;
        for (int i = 0; i < N; i++) {
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, appWidgetIds[i], intent, 0);

            RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.bluetooth_toggle);
            remoteViews.setOnClickPendingIntent(R.id.btnToggleBluetooth, pendingIntent);

            updateAppWidget(context, appWidgetManager, appWidgetIds[i]);
        }
    }

    @Override
    public void onEnabled(Context context) {
        Log.d(TAG, "onEnabled()");
        // Enter relevant functionality for when the first widget is created
    }

    @Override
    public void onDisabled(Context context) {
        Log.d(TAG, "onDisabled()");
        // Enter relevant functionality for when the last widget is disabled
    }

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId) {
        Log.d(TAG, "updateAppWidget()");
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.bluetooth_toggle);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

提前感谢您的回复和帮助。我花了一天的时间来解决这个问题,但我所有的努力都白费了。

【问题讨论】:

  • 搜了两天终于解决了。这是正确的代码:Intent intent = new Intent(context, BluetoothToggle.class); intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds); intent.putExtra(toggleBluetoothAction, toggleBluetoothAction); 并在onReceive() 中做了我想做的任何事情
  • 把它写成一个答案,自我解释的那种。其他人可能面临同样的问题。
  • @aB9 根据您的建议。

标签: android android-widget onclicklistener


【解决方案1】:

所以我在搜索了两天后终于解决了这个问题。这是正确的代码:

Intent intent = new Intent(context, BluetoothToggle.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWi‌​dgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
intent.putExtra(toggleBluetoothAction, toggleBluetoothAction);

这里是onReceive() 方法,每次我点击小部件时都会调用它。

public void onReceive(Context context, Intent intent)
{
    Bundle bundle = intent.getExtras();

    if (bundle != null && bundle.getString(toggleBluetoothAction) != null)
    {
        // My Logic
    }
}

看看intentsetActionputExtra,它们在我的情况下对解决问题很有用。我检查了许多网站,但其中大多数都没有包含这段代码来使小部件可点击。他们所做的只是创建一个Intent,然后立即创建一个PendingIndent。我做了同样的事情,但没有任何帮助。

谢谢。

【讨论】:

    猜你喜欢
    • 2013-03-14
    • 2012-02-11
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-17
    • 1970-01-01
    相关资源
    最近更新 更多