Java 处理主屏幕小部件中的按钮单击
我正在尝试制作一个主屏幕小部件,它将打开和关闭gps(设置中的位置)。我仍然不明白如何处理按钮点击事件 我从在线教程中获取了这段代码Java 处理主屏幕小部件中的按钮单击,java,android,android-widget,home-screen-widget,Java,Android,Android Widget,Home Screen Widget,我正在尝试制作一个主屏幕小部件,它将打开和关闭gps(设置中的位置)。我仍然不明白如何处理按钮点击事件 我从在线教程中获取了这段代码 public class LocationWidget extends AppWidgetProvider { private static String SYNC_CLICKED = "automaticWidgetSyncButtonClick"; @Override public void onUpdate(Context context, AppWidg
public class LocationWidget extends AppWidgetProvider {
private static String SYNC_CLICKED = "automaticWidgetSyncButtonClick";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int count = appWidgetIds.length;
String status;
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
for (int i = 0; i < count; i++) {
int widgetId = appWidgetIds[i];
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
status = "OFF";
} else {
status = "ON";
}
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
remoteViews.setTextViewText(R.id.textView, status);
Intent intent = new Intent(context, LocationWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.actionButton, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
remoteViews.setTextViewText(R.id.textView, "Button clicked");
Log.v("ACTION", intent.getAction());
}
公共类LocationWidget扩展了AppWidgetProvider{
私有静态字符串同步\u CLICKED=“automaticWidgetSyncButtonClick”;
@凌驾
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
最终整数计数=appWidgetIds.length;
字符串状态;
LocationManager=(LocationManager)context.getSystemService(context.LOCATION\u服务);
for(int i=0;i
}
在onReceive内部,Log.v返回“ACTION”﹕android.appwidget.action.appwidget\u更新”。因此,我需要帮助使按钮按我所希望的方式单击。有人能解释一下怎么做和为什么吗?你应该能做到
if(intent.getAction()=)
{
/*这里的处理代码*/
}
我将打开我的笔记本电脑,您可以更改并添加您的设置意图
appwidget.java
public class MyWidget extends AppWidgetProvider {
Button btnhelp;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
Intent configIntent = new Intent(context, HelpActivity.class);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.btnhelp, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
显示
<receiver android:name=".MyWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
资源xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/widget_layout"
android:minHeight="70dp"
android:minWidth="70dp"
android:updatePeriodMillis="300000"></appwidget-provider>
布局xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dip"
android:background="@drawable/ic_dashboard_help"
android:clickable="true">
<Button
android:id="@+id/btnhelp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="4dip"
android:background="@android:color/transparent" />
</LinearLayout>
我明白了
public class LocationWidget extends AppWidgetProvider {
public static String WIDGET_BUTTON = "MY_PACKAGE_NAME.WIDGET_BUTTON";
private static final String ACTION_CLICK = "ACTION_CLICK";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int count = appWidgetIds.length;
String status;
LocationManager manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
for (int i = 0; i < count; i++) {
int widgetId = appWidgetIds[i];
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
status = "OFF";
} else {
status = "ON";
}
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
if (status == "ON") {
remoteViews.setInt(R.id.actionButton, "setBackgroundColor", Color.GREEN);
remoteViews.setTextViewText(R.id.actionButton, "Location Service On");
}
if (status == "OFF") {
remoteViews.setInt(R.id.actionButton, "setBackgroundColor", Color.RED);
remoteViews.setTextViewText(R.id.actionButton, "Location Service Off");
}
Intent intent = new Intent(context, LocationWidget.class);
intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);
String buttonclick = "buttonclick";
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.actionButton, getPendingSelfIntent(context, buttonclick));
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
protected PendingIntent getPendingSelfIntent(Context context, String action) {
Intent intent = new Intent(context, LocationWidget.class);
intent.setAction(action);
return PendingIntent.getBroadcast(context, 0, intent, 0);
}
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.activity_main);
if (intent.getAction() == "buttonclick") {
Log.v("ACTION", "buttonclick");
Intent callGPSSettingIntent = new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
callGPSSettingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callGPSSettingIntent);
}
}
}
公共类LocationWidget扩展了AppWidgetProvider{
publicstaticstringwidget\u-BUTTON=“MY\u-PACKAGE\u-NAME.WIDGET\u-BUTTON”;
私有静态最终字符串ACTION\u CLICK=“ACTION\u CLICK”;
@凌驾
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
最终整数计数=appWidgetIds.length;
字符串状态;
LocationManager=(LocationManager)context.getSystemService(context.LOCATION\u服务);
for(int i=0;i
但是什么操作应该返回一个按钮点击,为什么?似乎Stack Exchange上的帖子可以回答您的问题,也许可以在那里查看?