单击 android 小部件按钮上的侦听器按钮不起作用
click listener on android widget Button not working
我正在开发一个 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()
方法,每次我单击小部件时都会调用该方法。
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
。我也做了同样的事情,但没有任何帮助。
谢谢。
我正在开发一个 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()
方法,每次我单击小部件时都会调用该方法。
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
。我也做了同样的事情,但没有任何帮助。
谢谢。