我的小部件中有两个按钮,我想调用两个不同的类。这是我的代码示例
package com.example.test;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import android.annotation.SuppressLint;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
//import android.text.format.DateFormat;
import android.util.Log;
//import android.view.View;
//import android.view.View.OnClickListener;
//import android.widget.Button;
//import android.widget.LinearLayout;
import android.widget.RemoteViews;
@SuppressLint("SimpleDateFormat")
public class MainActivity extends AppWidgetProvider {
SimpleDateFormat df = new SimpleDateFormat("hh:mm:ss");
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Log.i("ExampleWidget", "Updating widgets " + Arrays.asList(appWidgetIds));
// Perform this loop procedure for each App Widget that belongs to this
// provider
for (int i = 0; i < N; i++) {
int appWidgetId = appWidgetIds[i];
// Create an Intent to launch ExampleActivity
Intent intent = new Intent(context, SecondClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
Intent intent1 = new Intent(context, ThirdClass.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent1, 0);
RemoteViews views1 = new RemoteViews(context.getPackageName(), R.layout.activity_main);
views1.setOnClickPendingIntent(R.id.button2, pending);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
}
}
清单文件是
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.test.ThirdClass"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.test.SecondClass"
android:label="@string/name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.example.test.MainActivity" android:label="8 test">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/widget1_info" />
</receiver>
</application>
</manifest>
当我按下button1时,&#34; SecondClass.class&#34;被调用但按下button2&#34; ThirdClass.class&#34;不叫。有人能帮帮我吗。 Thnaks
答案 0 :(得分:0)
您已创建了第二个RemoteViews对象(称为views1
),并为其中一个视图提供了启动ThirdClass的PendingIntent。但是当你更新appwidget时,你会给出第一个RemoteViews(称为views
)。显然这些不一样,所以你的小部件中没有PendingIntent可以转到ThirdClass。
我想你想这样做:
int appWidgetId = appWidgetIds[i];
// Get the layout for the App Widget and attach an on-click listener
// to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.activity_main);
// Create an Intent to launch SecondClass
Intent intent = new Intent(context, SecondClass.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.button1, pendingIntent);
// Create an Intent to launch ThirdClass
Intent intent1 = new Intent(context, ThirdClass.class);
PendingIntent pending = PendingIntent.getActivity(context, 0, intent1, 0);
views.setOnClickPendingIntent(R.id.button2, pending);
appWidgetManager.updateAppWidget(appWidgetId, views);