Android 小部件的两个按钮以不同的意图调用相同的 Activity

发布于 2024-12-08 13:59:42 字数 2800 浏览 2 评论 0原文

我在 Android 中有一个带有两个按钮的主屏幕小部件。两个按钮应该调用相同的活动(类),只需使用不同的意图加上意图附加,即可知道哪个按钮调用了该类。 目前只有 Button1 正在工作并调用该活动。我还在被调用的活动中收到键值。

我怎样才能让第二个按钮起作用? 这是我的代码:

             public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    super.onUpdate(context, appWidgetManager, appWidgetIds);

    for ( int i =0; i<appWidgetIds.length ; i++){

        int appWidgetId = appWidgetIds[i];

        Intent intent2 = new Intent(context, Main.class);
        Intent intent1 = new Intent(context, Main.class);

        // Intent put extras Button 1
        String bread1 = "secure";
        Bundle basket1 = new Bundle();
        basket1.putString("key", bread1);
        intent1.putExtras(basket1);

        // Intent put extras Button 2
        String bread2 = "insecure";
        Bundle basket2 = new Bundle();
        basket2.putString("key", bread2);
        intent2.putExtras(basket2);

        PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, 0);
        PendingIntent pending2 = PendingIntent.getActivity(context, 0, intent2, 0);

        RemoteViews views1 = new RemoteViews(context.getPackageName(),R.layout.maina);
        RemoteViews views2 = new RemoteViews(context.getPackageName(),R.layout.maina);

        views1.setOnClickPendingIntent(R.id.button1, pending1);
        views2.setOnClickPendingIntent(R.id.button2, pending2);

        appWidgetManager.updateAppWidget(appWidgetId, views1);
        appWidgetManager.updateAppWidget(appWidgetId, views2);

这是 maina.xml

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" android:weightSum="1"                android:orientation="vertical">
          <TextView android:layout_width="wrap_content"   android:layout_height="wrap_content" android:text="TextView" android:id="@+id/tvWidget"  android:textAppearance="?android:attr/textAppearanceLarge"></TextView>

           <LinearLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" android:weightSum="1"  android:orientation="horizontal">



           <Button android:text="@string/button1" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
            <Button android:text="@string/button2" android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>

          </LinearLayout>

</LinearLayout>

I have a homescreenwidget in Android with two buttons. Both buttons should call the same activity ( class ) just use a different intent plus intent extras, to know which button called the class.
For now only button1 is working and calling the activity. I also receive the keyvalue in the called Activity.

How can i make the second button work?
Here's my code:

             public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    super.onUpdate(context, appWidgetManager, appWidgetIds);

    for ( int i =0; i<appWidgetIds.length ; i++){

        int appWidgetId = appWidgetIds[i];

        Intent intent2 = new Intent(context, Main.class);
        Intent intent1 = new Intent(context, Main.class);

        // Intent put extras Button 1
        String bread1 = "secure";
        Bundle basket1 = new Bundle();
        basket1.putString("key", bread1);
        intent1.putExtras(basket1);

        // Intent put extras Button 2
        String bread2 = "insecure";
        Bundle basket2 = new Bundle();
        basket2.putString("key", bread2);
        intent2.putExtras(basket2);

        PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, 0);
        PendingIntent pending2 = PendingIntent.getActivity(context, 0, intent2, 0);

        RemoteViews views1 = new RemoteViews(context.getPackageName(),R.layout.maina);
        RemoteViews views2 = new RemoteViews(context.getPackageName(),R.layout.maina);

        views1.setOnClickPendingIntent(R.id.button1, pending1);
        views2.setOnClickPendingIntent(R.id.button2, pending2);

        appWidgetManager.updateAppWidget(appWidgetId, views1);
        appWidgetManager.updateAppWidget(appWidgetId, views2);

here is the maina.xml

          <?xml version="1.0" encoding="utf-8"?>
          <LinearLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent" android:weightSum="1"                android:orientation="vertical">
          <TextView android:layout_width="wrap_content"   android:layout_height="wrap_content" android:text="TextView" android:id="@+id/tvWidget"  android:textAppearance="?android:attr/textAppearanceLarge"></TextView>

           <LinearLayout
           xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="fill_parent"
           android:layout_height="fill_parent" android:weightSum="1"  android:orientation="horizontal">



           <Button android:text="@string/button1" android:id="@+id/button1" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>
            <Button android:text="@string/button2" android:id="@+id/button2" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>

          </LinearLayout>

</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

墨小墨 2024-12-15 13:59:43

@Arnold您创建了2个PendingIntent,它们是......

PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, 0);
PendingIntent pending2 = PendingIntent.getActivity(context, 0, intent2, 0);

其中PendingIntent.getActivity(Context context, int requestCode, Intent Intent, int flags)有4个参数。您必须为不同的 PendingIntents 发送不同的“requestCode”。

您的代码应该...

PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pending2 = PendingIntent.getActivity(context, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

在主类中,您需要

String value;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

            Bundle b=intent.getExtras();


    try{
    value=b.getString("key");
    }
     catch (Exception e) {
        // TODO: handle exception
    }

    super.onReceive(context, intent);

}

使用 onUpdate 代码创建这个...

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    // Get all ids
    ComponentName thisWidget = new ComponentName(context,
            main.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    for (int widgetId : allWidgetIds) {
        // Create some random data

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.main);


        // Register an onClickListener for 1st button
        Intent intent = new Intent(context, main.class);

        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
        intent.putExtra("key", "1st Button");

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent);

        // Register an onClickListener for 2nd button............
        Intent intent2 = new Intent(context, main.class);

           intent2.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                   intent2.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
           intent2.putExtra("key", "2nd Button");

        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context,
                1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.button2, pendingIntent2);

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
}

然后您可以检查是否 value=1st Buttonvalue=第二个按钮知道哪个按钮被点击。
它应该可以工作...如果它不起作用,请告诉我问题是什么...

@Arnold you have created 2 PendingIntent which are....

PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, 0);
PendingIntent pending2 = PendingIntent.getActivity(context, 0, intent2, 0);

Where PendingIntent.getActivity(Context context, int requestCode, Intent intent, int flags) has 4 parameters. You have to send different "requestCode" for different PendingIntents.

Your code should be....

PendingIntent pending1 = PendingIntent.getActivity(context,0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pending2 = PendingIntent.getActivity(context, 1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

in the main class you need to create this...

String value;
@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub

            Bundle b=intent.getExtras();


    try{
    value=b.getString("key");
    }
     catch (Exception e) {
        // TODO: handle exception
    }

    super.onReceive(context, intent);

}

with the onUpdate code....

    @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
        int[] appWidgetIds) {

    // Get all ids
    ComponentName thisWidget = new ComponentName(context,
            main.class);
    int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);

    for (int widgetId : allWidgetIds) {
        // Create some random data

        RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
                R.layout.main);


        // Register an onClickListener for 1st button
        Intent intent = new Intent(context, main.class);

        intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
        intent.putExtra("key", "1st Button");

        PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
                0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.button1, pendingIntent);

        // Register an onClickListener for 2nd button............
        Intent intent2 = new Intent(context, main.class);

           intent2.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                   intent2.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,allWidgetIds);
           intent2.putExtra("key", "2nd Button");

        PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context,
                1, intent2, PendingIntent.FLAG_UPDATE_CURRENT);

        remoteViews.setOnClickPendingIntent(R.id.button2, pendingIntent2);

        appWidgetManager.updateAppWidget(widgetId, remoteViews);
    }
}

Then you can check whether value=1st Button or value=2nd Button to know which Button has been clicked.
It should work... IF it does not work please let me know what is the problem...

旧情别恋 2024-12-15 13:59:43

我在小部件上使用 2 个按钮时遇到了类似的问题。我对它们都设置了 OnClickPendingIntents,通过不同的 setExtra() 调用来区分。但即使单击第一个按钮,也只会调用后一个意图。
我找到了2个解决方案:
- 为两个 PendingIntents 分配不同的操作
- 在第二个 PendingEvent 设置 PendingEvent.FLAG_CANCEL_CURRENT 标志

我对 PendingEvents 和 Widget 非常不熟悉,所以我看不到优点和缺点,并将坚持第一个解决方案。

也许这也可以帮助您解决问题。

I had a similar problem with using 2 buttons on a widget. I set OnClickPendingIntents on both of them, distinguished by different setExtra() calls. But only the latter intent was called, even when clicking the first button.
I found 2 solutions:
- assign different actions to both PendingIntents
- on the second PendingEvent set the PendingEvent.FLAG_CANCEL_CURRENT flag

I am quite unfamiliar with PendingEvents and Widgets, so I can't see the pros and cons and will stick to the first solution.

Maybe that might help you with your problem, too.

叫嚣ゝ 2024-12-15 13:59:43

您的小部件将只有 1 个远程视图。尝试将代码片段的末尾更改为:

RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.maina);

remoteView.setOnClickPendingIntent(R.id.button1, pending1);
remoteView.setOnClickPendingIntent(R.id.button2, pending2);

appWidgetManager.updateAppWidget(appWidgetId, remoteView);

如果失败,在 R.layout.maina 中查看布局会很有用。

Your widget will only have 1 remote view. Try changing the end of your code snippet to:

RemoteViews remoteView = new RemoteViews(context.getPackageName(),R.layout.maina);

remoteView.setOnClickPendingIntent(R.id.button1, pending1);
remoteView.setOnClickPendingIntent(R.id.button2, pending2);

appWidgetManager.updateAppWidget(appWidgetId, remoteView);

Failing that, it would be useful to see your layout in R.layout.maina.

遗失的美好 2024-12-15 13:59:43

就我而言:

remoteViews.setOnClickPendingIntent(R.id.widget_head, touch_man(context));

remoteViews.setOnClickPendingIntent(R.id.widget_head2, touch_woman(context));

public static PendingIntent touch_man(Context context) {

    Intent intent = new Intent();
    intent.setAction("touch_man");
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

}

public static PendingIntent touch_woman(Context context) {

    Intent intent = new Intent();
    intent.setAction("touch_woman");
    return PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

}

在 AndroidManifest 中

<receiver
        android:name="Receiver"
        android:label="widgetBroadcastReceiver" >
        <intent-filter>
            <action android:name="touch_man" />
            <action android:name="touch_woman"/>
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/demo_widget_provider" />
    </receiver>

它正在工作。

In my case:

remoteViews.setOnClickPendingIntent(R.id.widget_head, touch_man(context));

remoteViews.setOnClickPendingIntent(R.id.widget_head2, touch_woman(context));

public static PendingIntent touch_man(Context context) {

    Intent intent = new Intent();
    intent.setAction("touch_man");
    return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

}

public static PendingIntent touch_woman(Context context) {

    Intent intent = new Intent();
    intent.setAction("touch_woman");
    return PendingIntent.getBroadcast(context, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

}

In AndroidManifest

<receiver
        android:name="Receiver"
        android:label="widgetBroadcastReceiver" >
        <intent-filter>
            <action android:name="touch_man" />
            <action android:name="touch_woman"/>
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/demo_widget_provider" />
    </receiver>

It's working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文