单击android小部件上的侦听器按钮不工作

单击android小部件上的侦听器按钮不工作,android,android-widget,onclicklistener,Android,Android Widget,Onclicklistener,我正在从事一个android项目,并创建了一个用于切换蓝牙的小部件(即,如果关闭,则按该小部件打开蓝牙,反之亦然)。但问题是,单击按钮的侦听器不起作用,当我单击主屏幕上的小部件时,它什么也不起作用。 我在谷歌上搜索了很多次,尝试了几乎所有的解决方案,但不幸的是,没有一个对我有效。我无法理解为什么我的代码不起作用 以下是项目的清单文件: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://s

我正在从事一个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);
    }
}
公共类BluetoothToggle扩展了AppWidgetProvider{
私有静态最终字符串TAG=“BluetoothToggle”;
私有最终字符串toggleBluetoothAction=“ToggleBluetooth”;
@凌驾
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
Log.d(标记“onUpdate()”);
//可能有多个窗口小部件处于活动状态,因此请更新所有窗口小部件
final int N=appWidgetIds.length;
对于(int i=0;i

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

因此,经过两天的搜索,我终于解决了这个问题。以下是正确的代码:

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
    }
}
请看一下
intent
setAction
putExtra
,在我的例子中,它们对解决问题很有用。我查看了很多网站,但大多数网站都没有包含这段代码,以使小部件可点击。他们所做的只是创建一个
意图
,然后立即创建一个
未决事件
。我也这么做了,但没有任何帮助


谢谢。

搜索了两天后,我终于解决了这个问题。下面是正确的代码:
Intent-Intent=newintent(上下文,BluetoothToggle.class);setAction(AppWidgetManager.ACTION\u APPWIDGET\u UPDATE);intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_id,appWidgetIds);intent.putExtra(toggleBluetoothAction,toggleBluetoothAction)并在
onReceive()
中做了我想做的任何事情,将其作为一个答案写下来,不言自明。其他人可能会面临同样的问题。@aB9根据您的建议。