使用配置活动的Android小部件不会更新

时间:2012-11-12 15:23:04

标签: android widget android-appwidget

我正在制作一个Android小部件,以根据公交车站号显示实时交通信息。它显示停靠号码的下3个总线到达,并有一个用户可以刷新数据的按钮。在我尝试添加我的小部件配置活动之前,这工作正常。

现在,当您选择小部件时,将打开一个配置屏幕,您可以在其中输入您的公交车站号码。执行此操作后,窗口小部件将添加到主屏幕,但它不会执行收集数据的方法,刷新按钮也不适用于此。我无法弄清楚原因。

WidgetConfig

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>

非常感谢任何帮助

0 个答案:

没有答案