单击应用程序小部件中的第二个按钮?

发布于 2025-01-07 03:40:13 字数 2844 浏览 0 评论 0原文

我正在开发一个应用程序小部件,它启动一个活动,该活动会更改应用程序小部件的布局...当我再次单击它时,我希望它启动另一个活动。似乎普遍的共识是你不能将两个监听器设置为同一个按钮,但是有没有办法解决这个问题?谁能给我一些关于如何执行此操作或解决此问题的信息?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你怎么这么可爱啊 2025-01-14 03:40:13
// 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;
}

还是这太简单了?

// 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;
}

Or is that too simple?

不顾 2025-01-14 03:40:13

我想通了。虽然您实际上无法在同一个按钮上放置另一个单击侦听器,但您可以告诉意图导航到设置一个然后自行关闭的活动。如本例所示:

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

}
}

然后在 secondary_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);

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:

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

}
}

Then in second_activity2.class write this after 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文