我正在開發一個應用程序小部件,它啓動一個更改應用程序小部件佈局的活動...當我再次單擊它時,我希望它啓動另一個活動。看起來普遍的共識是,你不能將兩個聽衆設置爲同一個按鈕,但是無論如何都有這個問題嗎?任何人都可以給我一些關於如何做到這一點或解決它的信息?第二個按鈕點擊appwidget?
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);
}
}
和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);
};
}
任何幫助,不勝感激!謝謝!
謝謝你的努力,但不幸的是,這將始終啓動第一意圖。只要第一個被調用的應用程序被刷新,因爲它跳過第二個案例(只有一次點擊)並刷新小部件。 – Christian 2012-02-19 06:40:01
我想通了。雖然實際上不能在同一個按鈕上放置另一個點擊偵聽器,但您可以將意圖導航到一個活動,該活動設置一個,然後自行關閉。 – Christian 2012-02-20 23:10:26