2012-02-19 107 views
0

我正在开发一个应用程序小部件,它启动一个更改应用程序小部件布局的活动...当我再次单击它时,我希望它启动另一个活动。看起来普遍的共识是,你不能将两个听众设置为同一个按钮,但是无论如何都有这个问题吗?任何人都可以给我一些关于如何做到这一点或解决它的信息?第二个按钮点击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); 


}; 

} 

任何帮助,不胜感激!谢谢!

回答

0

我想通了。虽然实际上不能在同一个按钮上放置另一个点击侦听器,但您可以将意图导航到一个活动,该活动设置一个,然后自行关闭。如本例:

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); 
    ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);  

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


    appWidgetManager.updateAppWidget(thisWidget, remoteViews); 

} 
} 

然后在second_activity2.class写这篇文章后的onCreate()

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

    Intent configIntent = new Intent(context, second_activity3.class); 
    configIntent.setAction(ACTION_WIDGET_CONFIGURE); 
    PendingIntent configPendingIntent = PendingIntent.getActivity(context, 0, configIntent, 0); 
    remoteViews.setOnClickPendingIntent(R.id.update2, configPendingIntent); 

    appWidgetManager.updateAppWidget(thisWidget, remoteViews); 
0
// Global to the AppWidget 
int clickCount = 0; 

// In onClick listener 
clickCount++; 
switch (clickCount) { 
    case(1): 
     // Start Activity 1 
     break; 
    case(2): 
     // Start Activity 2 
     break; 
} 

还是那太简单了?

+0

谢谢你的努力,但不幸的是,这将始终启动第一意图。只要第一个被调用的应用程序被刷新,因为它跳过第二个案例(只有一次点击)并刷新小部件。 – Christian 2012-02-19 06:40:01

+0

我想通了。虽然实际上不能在同一个按钮上放置另一个点击侦听器,但您可以将意图导航到一个活动,该活动设置一个,然后自行关闭。 – Christian 2012-02-20 23:10:26

相关问题