我正在研究一个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);
}
}
提前感谢您的回复和帮助。我花了一天时间来解决这个问题,但我所有的努力都是徒劳的。
答案 0 :(得分:1)
所以最后我在搜索了两天之后解决了这个问题。这是正确的代码:
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()
方法。
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
if (bundle != null && bundle.getString(toggleBluetoothAction) != null)
{
// My Logic
}
}
查看intent
setAction
和putExtra
,这些对我来说很有用,可以解决问题。我检查了很多网站,但大多数网站都没有包含这段代码来使小部件可以点击。他们所做的就是创建Intent
,然后立即创建PendingIndent
。我做了同样的事,但没有任何帮助我。
感谢。