我正在制作一个 Android 小部件来显示基于公交车站号的实时交通信息。它显示停靠站号的下 3 个巴士到达站,并有一个按钮,用户可以在其中刷新数据。在我尝试添加我的小部件配置活动之前,这一切正常。
现在,当您选择小部件时,会打开一个配置屏幕,您可以在其中输入您的巴士站号。一旦你这样做了,小部件就会被添加到主屏幕,但它不会执行收集数据的方法,刷新按钮也不会为此工作。我一直无法弄清楚为什么。
小部件配置
public class WidgetConfig extends Activity implements OnClickListener{
EditText info;
AppWidgetManager awm;
Context c;
int awID;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.widget_config);
Button b = (Button)findViewById(R.id.bWidgetConfig);
b.setOnClickListener(this);
c = WidgetConfig.this;
//getting info about the widget that launched this activity
Intent i = getIntent();
Bundle extras = i.getExtras();
if (extras != null){
awID = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}else{
finish();
}
awm = AppWidgetManager.getInstance(c);
}
public void onClick(View v) {
// TODO Auto-generated method stub
info = (EditText)findViewById(R.id.etWidgetConfig);
String e = info.getText().toString();
RemoteViews views = new RemoteViews(c.getPackageName(), R.layout.widget_layout);
views.setTextViewText(R.id.tvStopNum, e);
Intent in = new Intent(c, WidgetConfig.class);
PendingIntent pi = PendingIntent.getActivity(c, 0, in, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.tvStopNum, pi);
awm.updateAppWidget(awID, views);
Intent result = new Intent();
result.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, awID);
setResult(RESULT_OK, result);
finish();
}
AppWidgetProvider
public class MyWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
// Create a new intent that will target this class
Intent intent = new Intent(context, MyWidgetProvider.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[0]);
intent.setAction("update");
// Create a new PendingIntent which will be run whenever the refresh button is pressed
// This PendingIntent will run the intent we just created before this
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.button1, pi);
// Manually run the AsyncTask to initially populate the 5 question fields
new RTPIinformation().execute(context);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
// This method is called when it receives an Intent. In particular, it will
// receive an intent whenever the refresh button is clicked
@Override
public void onReceive(Context context, Intent intent){
super.onReceive(context, intent);
if (intent.getAction().equals("update")){
new RTPIinformation().execute(context);
}
}
class RTPIinformation extends AsyncTask<Context, Void, List<RealtimeData>>{
.
.
.
}
显现
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ruben.dublin.bus.widget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".WidgetConfig"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<receiver
android:name=".MyWidgetProvider"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_info" />
</receiver>
</application>
任何帮助将不胜感激