做一个天气预报的 小项目,需求将某个城市的天气情况通过widget控件显示在桌面上,另外在widget上添加事件,使点击给widget实例,即进入配置界面,即一个activity,,点击确定即返回,实现更新效果,另外多个每个widget还可实现单独更新。现在我可添加多个widget,并可显示不同的信息。但是却只有一个实例可以进行点击,其他的实例点击都没有效果。不知道为什么。
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;public class WeatherWidget extends AppWidgetProvider {
private Intent mIntent = new Intent("com.sea.service.UPDATEWEATHER");
private Intent i;
private PendingIntent mPendingIntent;
private RemoteViews mRemoteVies; @Override
public void onDisabled(Context context) {
context.stopService(mIntent);
Log.i("WeatherWidget", "onDisabled");
} @Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.i("WeatherWidget", "onUpdate");
mRemoteVies = new RemoteViews(context.getPackageName(),
R.layout.weatherwidget);
//打开activity
mPendingIntent = PendingIntent.getActivity(context, 0, new Intent(
context, ConfigActivity.class), 0);
//准备做什么
mRemoteVies.setOnClickPendingIntent(R.id.linearLayout, mPendingIntent);
       
appWidgetManager.updateAppWidget(appWidgetIds, mRemoteVies);
         
context.startService(mIntent);
}
我在onUpdate中添加的监听事件这是为配置信息的Activity
package com.sea.weatherwidget;import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RemoteViews;
import android.widget.Toast;public class ConfigActivity extends Activity {
private Button btnEnter;
private Button btnCancel;
private EditText edtCode;
private EditText edtTime;
private SharedPreferences mSharedPreferences;
private int mAppWidgetId; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.configuration); btnEnter = (Button) findViewById(R.id.btnEnter);
btnCancel = (Button) findViewById(R.id.btnCancel);
edtCode = (EditText) findViewById(R.id.edtCode);
edtTime = (EditText) findViewById(R.id.edtTime);

//获取id
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras != null) {

mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);

}

Log.i("weather", "mAppWidgetId:"+String.valueOf(mAppWidgetId));
// Toast.makeText(getApplicationContext(), mAppWidgetId+"出来的ID", Toast.LENGTH_SHORT).show();
mSharedPreferences = getSharedPreferences("weather"+mAppWidgetId, MODE_PRIVATE);
edtCode.setText(mSharedPreferences.getString("code", "CHXX0131"));
edtTime.setText(String.valueOf(mSharedPreferences.getInt("time", 10)));
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(getApplicationContext());
RemoteViews views = new RemoteViews(getApplicationContext()
.getPackageName(), R.layout.weatherwidget);
PendingIntent pendingIntent = PendingIntent.getActivity(
getApplicationContext(), 0, new Intent(getApplicationContext(),
ConfigActivity.class), 0); views.setOnClickPendingIntent(R.id.linearLayout, pendingIntent); views.setTextViewText(R.id.textView, "Waiting for loading...");
appWidgetManager.updateAppWidget(mAppWidgetId, views); btnEnter.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Editor editor = mSharedPreferences.edit();
editor.putString("code", edtCode.getText().toString());
editor.putInt("time",
Integer.parseInt(edtTime.getText().toString()));
if (editor.commit()) {
Log.i("WeatherWidget", "保存配置成功");
}
//确定appwidget添加到座面
Intent intent = new Intent();
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
mAppWidgetId);
Toast.makeText(getApplicationContext(), mAppWidgetId+"", Toast.LENGTH_SHORT).show(); setResult(RESULT_OK, intent);
Intent i=new Intent(ConfigActivity.this,WeatherService.class);
i.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
mAppWidgetId);

startService(i);
finish();
}
}); btnCancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
××××××××××××××××××××××××××××××××我用service来控制更新package com.sea.weatherwidget;import java.io.InputStream;
import java.net.URL;import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;import com.sea.weatherwidget.data.WeatherFetcher;
import com.sea.weatherwidget.data.WeatherRecord;public class WeatherService extends Service { private AppWidgetManager mAppWidgetManager;
private ComponentName mComponentName;
private SharedPreferences mSharedPreferences;
private WeatherFetcher mYWeatherFetcher = new WeatherFetcher();
private WeatherRecord mWeatherRecord;

private Runnable mRunnable;
private RemoteViews mRemoteVies;
    private int widgetId;
private int mTime;
@Override
public void onCreate() {
Log.i("service", "onCreate");
mAppWidgetManager = AppWidgetManager
.getInstance(getApplicationContext()); mComponentName = new ComponentName(getApplicationContext(),
WeatherWidget.class);
mRemoteVies = new RemoteViews(getApplicationContext().getPackageName(),
R.layout.weatherwidget);

}
private Handler mHandler = new Handler();
// {
// public void handleMessage(android.os.Message msg) {
// mSharedPreferences = getSharedPreferences("weather"+msg.what, MODE_PRIVATE);
// Log.i("service", "handleMessage"+msg.what+"");
// mYWeatherFetcher.setCode(mSharedPreferences.getString("code",
// "CHXX0131"));
// mTime = mSharedPreferences.getInt("time", 10);
//
// };
// };
    public class MyRunnable implements Runnable
    {
    
     private int id=0;
     private int wid;
     MyRunnable(int id)
     {
     this.wid=id;
       mAppWidgetManager.updateAppWidget(wid, mRemoteVies);
    
     }
    
public void run() {
// Log.i("service", "MyRunnable");
// TODO Auto-generated method stub
mWeatherRecord = mYWeatherFetcher.getWeather(); String str = mWeatherRecord.getCity() + "\n"
+ mWeatherRecord.getDate() + "\n"
+ mWeatherRecord.getTemp() + "C";
Log.i("WeatherWidget", str);
mRemoteVies.setTextViewText(R.id.textView, str); InputStream inputStream = null;
try {
inputStream = new URL(mWeatherRecord.getImage())
.openStream();
} catch (Exception e) {

}
mRemoteVies.setImageViewBitmap(R.id.imageView,
BitmapFactory.decodeStream(inputStream));

// mAppWidgetManager.updateAppWidget(mComponentName, mRemoteVies);
if(id==mAppWidgetManager.getAppWidgetIds(mComponentName).length-1)
{
id=0;
}
//    Message msg=new Message();
//      msg=mHandler.obtainMessage();
//        
//        msg.what=mAppWidgetManager.getAppWidgetIds(mComponentName)[id++];
            mAppWidgetManager.updateAppWidget(mAppWidgetManager.getAppWidgetIds(mComponentName)[id++], mRemoteVies);
//            mHandler.sendMessage(msg);
//       Log.i("service", "runnable:"+msg.what+"");
       mHandler.postDelayed(this, mTime* 1000);
         
            

}
    
    }
@Override
public void onDestroy() {
mHandler.removeCallbacks(mRunnable);
} @Override
public void onStart(Intent intent, int startId) {
Bundle extras = intent.getExtras();
Log.i("service", "onStart");
if (extras != null) {
widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
}
// Toast.makeText(getApplicationContext(), widgetId+"service中ID", Toast.LENGTH_SHORT).show(); Log.i("weather", "onStart"+widgetId);
mSharedPreferences = getSharedPreferences("weather"+widgetId, MODE_PRIVATE);
mYWeatherFetcher.setCode(mSharedPreferences.getString("code",
"CHXX0131"));
mTime = mSharedPreferences.getInt("time", 10);// Log.i("WeatherWidget", "城市代码" + mYWeatherFetcher.getCode());
// Log.i("WeatherWidget", "更新时间" + mTime + "秒"); mSharedPreferences = null;
//    mAppWidgetManager.updateAppWidget(widgetId, mRemoteVies);
mHandler.post(new MyRunnable(widgetId));//提交
} @Override
public IBinder onBind(Intent intent) {
return null;
}}