Tôi có một homescreenwidget trong Android với hai nút. Cả hai nút nên gọi cùng một hoạt động (lớp) chỉ sử dụng một ý định khác nhau cộng với tính năng bổ sung ý định, để biết nút nào được gọi là lớp. Hiện tại chỉ có button1 đang hoạt động và gọi cho hoạt động. Tôi cũng nhận được keyvalue trong hoạt động được gọi là.Hai nút tiện ích Android gọi cùng Hoạt động với các mục đích khác nhau
Tôi làm cách nào để nút thứ hai hoạt động? Dưới đây là mã của tôi:
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);
đây là 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>
Bạn có thể cung cấp thêm chi tiết về những gì không hoạt động cho nút thứ hai không? I E. nó không làm gì cả khi được nhấp vào? –
phải, nó không làm gì cả khi được nhấp vào. – Arnold