Skip to content Skip to sidebar Skip to footer

Handling A Button Click In A Home Screen Widget

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

Solution 1:

i will open my heplactivity you can change and add your setting intent

appwidget.java

publicclassMyWidgetextendsAppWidgetProvider {
        Button btnhelp;

        @OverridepublicvoidonUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

            RemoteViewsremoteViews=newRemoteViews(context.getPackageName(), R.layout.widget_layout);
            IntentconfigIntent=newIntent(context, HelpActivity.class);
            PendingIntentconfigPendingIntent= PendingIntent.getActivity(context, 0, configIntent, 0);
            remoteViews.setOnClickPendingIntent(R.id.btnhelp, configPendingIntent);
            appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
        }
    }

manifest

<receiverandroid:name=".MyWidget"><intent-filter><actionandroid:name="android.appwidget.action.APPWIDGET_UPDATE" /></intent-filter><meta-dataandroid:name="android.appwidget.provider"android:resource="@xml/widget_info" /></receiver>

resource xml

<?xml version="1.0" encoding="utf-8"?><appwidget-providerxmlns:android="http://schemas.android.com/apk/res/android"android:initialLayout="@layout/widget_layout"android:minHeight="70dp"android:minWidth="70dp"android:updatePeriodMillis="300000"></appwidget-provider>

layout xml

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns: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"><Buttonandroid: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>

Solution 2:

I got it.

publicclassLocationWidgetextendsAppWidgetProvider {
    publicstaticStringWIDGET_BUTTON="MY_PACKAGE_NAME.WIDGET_BUTTON";

    privatestaticfinalStringACTION_CLICK="ACTION_CLICK";

    @OverridepublicvoidonUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        finalintcount= appWidgetIds.length;
        String status;
        LocationManagermanager= (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        for (inti=0; i < count; i++) {
            intwidgetId= appWidgetIds[i];
            if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                status = "OFF";
            } else {
                status = "ON";
            }
            RemoteViewsremoteViews=newRemoteViews(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");
            }
            Intentintent=newIntent(context, LocationWidget.class);
            intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
            intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, appWidgetIds);

            Stringbuttonclick="buttonclick";

            PendingIntentpendingIntent= 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) {
        Intentintent=newIntent(context, LocationWidget.class);
        intent.setAction(action);
        return PendingIntent.getBroadcast(context, 0, intent, 0);
    }

    publicvoidonReceive(Context context, Intent intent) {
        super.onReceive(context, intent);
        RemoteViewsremoteViews=newRemoteViews(context.getPackageName(), R.layout.activity_main);


        if (intent.getAction() == "buttonclick") {
            Log.v("ACTION", "buttonclick");

            IntentcallGPSSettingIntent=newIntent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            callGPSSettingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(callGPSSettingIntent);


        }


    }




}

Solution 3:

You should be able to do

if( intent.getAction() == <action_type> ) { /*Handling code here*/ }

Post a Comment for "Handling A Button Click In A Home Screen Widget"