处理主屏幕小部件中的按钮单击 [英] Handling a button click in a home screen widget

查看:89
本文介绍了处理主屏幕小部件中的按钮单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试制作一个主屏幕小部件,该小部件将打开和关闭gps(设置中的位置).我仍然不了解如何处理按钮单击事件.

I am trying to make a home screen widget which will toggle on and off the gps (Location in settings). I still don't understand how to handle a button click event.

我从在线教程中获取了这段代码.

I took this code from a tutorial online.

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());
}

}

在onReceive内,Log.v返回"ACTION:android.appwidget.action.APPWIDGET_UPDATE".因此,我需要帮助以使按钮的行为与单击时所需的行为相同.有人可以解释为什么以及为什么吗?

Inside onReceive, Log.v returns "ACTION﹕android.appwidget.action.APPWIDGET_UPDATE". So I need help to make the button behave the way i want when clicked. Can someone explain how and why?

推荐答案

我明白了.

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);


        }


    }




}

这篇关于处理主屏幕小部件中的按钮单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆