how to launch activity after widget button is pressed?

泪湿孤枕 提交于 2019-12-10 09:25:27

问题


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

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

回答1:


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" />
   ....


来源:https://stackoverflow.com/questions/16651421/how-to-launch-activity-after-widget-button-is-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!