单击应用程序小部件中的第二个按钮?
我正在开发一个应用程序小部件,它启动一个活动,该活动会更改应用程序小部件的布局...当我再次单击它时,我希望它启动另一个活动。似乎普遍的共识是你不能将两个监听器设置为同一个按钮,但是有没有办法解决这个问题?谁能给我一些关于如何执行此操作或解决此问题的信息?
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);
}
}
和 secondary_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);
};
}
非常感谢任何帮助!谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
还是这太简单了?
Or is that too simple?
我想通了。虽然您实际上无法在同一个按钮上放置另一个单击侦听器,但您可以告诉意图导航到设置一个然后自行关闭的活动。如本例所示:
然后在 secondary_activity2.class 中在 onCreate() 之后写入此内容
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()