Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to launch activity after widget button is pressed?

Following this tutorial I quickly created a simple widget which displays the current time on the home screen:

enter image description here

I've modified the code so that the widget also comes with a button. My goal is to launch an activity, once the button get's pressed. Unfortunately, I do not really understand where to listen to the button click. Does this have to go into the broadcast receiver section of the manifest file?

<!--  Broadcast Receiver -->
<receiver android:name=".WifiSSIDWidget" android:label="@string/app_name">
    <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
    </intent-filter>
    <meta-data android:name="android.appwidget.provider" android:resource="@xml/wifi_ssid_widget_provider" />
</receiver>    

Or does the code rather go into onUpdate? Here's the widget class code:

public class HelloWidget extends AppWidgetProvider {

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1, 1000);
    }

    private class MyTime extends TimerTask {
        RemoteViews remoteViews;
        AppWidgetManager appWidgetManager;
        ComponentName thisWidget;
        DateFormat format = SimpleDateFormat.getTimeInstance(SimpleDateFormat.MEDIUM, Locale.getDefault());

    public MyTime(Context context, AppWidgetManager appWidgetManager) {
        this.appWidgetManager = appWidgetManager;
        remoteViews = new RemoteViews(context.getPackageName(), R.layout.main);
        thisWidget = new ComponentName(context, HelloWidget.class);
    }

    @Override
    public void run() {
        remoteViews.setTextViewText(R.id.widget_textview, "TIME = " +format.format(new Date()));
        appWidgetManager.updateAppWidget(thisWidget, remoteViews);
    }

    } 
}

Here's the code that I want to be called after the button is clicked:

public void openWifiSettings() {
    final Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);
    final ComponentName cn = new ComponentName("com.android.settings", "com.android.settings.wifi.WifiSettings");
    intent.setComponent(cn);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}   
like image 396
memyself Avatar asked Mar 24 '23 21:03

memyself


1 Answers

import android.app.PendingIntent;
...
public static final String ACTION_BUTTON1_CLICKED = "com.example.myapp.BUTTON1_CLICKED";

in onUpdate add the following to your button:

Intent intent = new Intent(ACTION_BUTTON1_CLICKED);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.button_1, pendingIntent);

and in onReceive of your AppWidget

@Override
public void onReceive(Context context, Intent intent)
{
 Log.d(TAG, "onReceive() " + intent.getAction());
 super.onReceive(context, intent);
...
if (ACTION_BUTTON1_CLICKED.equals(intent.getAction()))
{
 // your code
}

also add your intent to the manifest

<intent-filter>
   <action android:name="com.example.myapp.BUTTON1_CLICKED" />
   ....
like image 195
j.holetzeck Avatar answered Mar 31 '23 23:03

j.holetzeck