I am developing an app widget that starts one activity which changes layout of the app widget… When I click it again I would like it to start another activity. It seems the general concensus is that you cannot set two listeners to the same button, but is there anyway around this? Can anyone give me some info on how to do this or get around it?
MyWidgetProvider.java
public class MyWidgetProvider extends AppWidgetProvider
{
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
public static String ACTION_WIDGET_CLICK = "ActionReceiverClick";
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews( context.getPackageName(), R.layout.hellowidget_layout );
Intent configIntent = new Intent(context, second_activity2.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.update, configPendingIntent);
RemoteViews remoteViews1 = new RemoteViews( context.getPackageName(), R.layout.hellowidget_layout2 );
configIntent = new Intent(context, second_activity3.class);
configIntent.setAction(ACTION_WIDGET_CONFIGURE);
configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0);
remoteViews1.setOnClickPendingIntent(R.id.update2, configPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
and second_activity2.java
public class second_activity2 extends Activity
{
public static String ACTION_WIDGET_CONFIGURE = "ConfigureWidget";
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.main22 );
getWindow().setWindowAnimations( 0 );
Context context = this;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.hellowidget_layout2);
ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
appWidgetManager.updateAppWidget(thisWidget, remoteViews);
ComponentName locationReceiver1 = new ComponentName( second_activity2.this, SmsReceiver.class );
PackageManager pm1 = getPackageManager();
pm1.setComponentEnabledSetting( locationReceiver1, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP );
Toast toast = Toast.makeText( second_activity2.this, "Your incoming texts are now being blocked.", 3000 );
toast.setGravity( Gravity.TOP, 0, 50 );
toast.show();
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
};
}
Any help is greatly appreciated! Thanks!
I figured it out. While you can’t actually put another click listener on the same button, you can tell the intent to navigate to an activity that sets one up and then closes itself. As in this example:
Then in second_activity2.class write this after onCreate()