how to launch activity after widget button is pressed?

℡╲_俬逩灬. 提交于 2019-12-05 15:16:42
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" />
   ....
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!