gpt4 book ai didi

android - Android 小部件的两个按钮以不同的 Intent 调用相同的 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:25:36 26 4
gpt4 key购买 nike

我在 Android 中有一个带有两个按钮的主屏幕小部件。两个按钮都应该调用相同的 Activity (类),只是使用不同的 Intent 加上 Intent 附加功能,以了解哪个按钮调用了该类。目前只有 button1 正在工作并调用该 Activity 。我还在调用的 Activity 中收到键值。

如何让第二个按钮起作用?这是我的代码:

             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>

最佳答案

@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 Button还是value=2nd Button来知道哪个Button被点击了。它应该工作...如果它不工作请让我知道是什么问题...

关于android - Android 小部件的两个按钮以不同的 Intent 调用相同的 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7645949/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com