想在onReceive里头更新AppWidget的Textview,为什么不行
想在onReceive里面更新AppWidget的Textview,为什么不行?
在Service里面每秒发送一次广播,在AppWidgetProvider的onReceive()中接收广播,想让appwidget的Textview每秒钟更新一次。不知道为什么,onReceive可以接收到广播,但是不能更新Textview..
------解决思路----------------------
你这段代码写的少东西
应该用AppWidgetManager的updateAppWidget方法调用下才会更新,参数是
widgetId和RemoteView
切记在onReceive不调用这个方法是不会更新的
------解决思路----------------------
在Service里面每秒发送一次广播,在AppWidgetProvider的onReceive()中接收广播,想让appwidget的Textview每秒钟更新一次。不知道为什么,onReceive可以接收到广播,但是不能更新Textview..
package com.example.s02e05_appwidget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.TextView;
public class Timer_appwidget_provider extends AppWidgetProvider{
private static final String STOP_COUNT = "com.example.s02e05_appwidget.STOP_COUNT";
private static final String START_SUSPEND_COUNT =
"com.example.s02e05_appwidget.START_SUSPEND_COUNT";
private TextView textview;
private RemoteViews rvs = new RemoteViews("com.example.s02e05_appwidget", R.layout.timer_appwidget);
private int count = 0;
/**
* args3: appWidgetIds: 可以在桌面上添加多个同款appWidget,每添加一个,为其分配一个Id
*/
@Override
public void onReceive(Context context, Intent intent) {
Log.d("mmm", "@....onReceive");
String action = intent.getAction();
if(action.equals("com.example.s02e05_appwidget.UPDATE_TEXTVIEW")){
count = intent.getIntExtra("COUNT", 0);
//rvs = new RemoteViews(context.getPackageName(), R.layout.timer_appwidget);
rvs.setTextViewText(R.id.textviewId, ""+count);
Log.d("mmm", "AppWidgetProvider的onReceive^^^^^^^^^^ "+count);
}
else{
super.onReceive(context, intent);
}
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
count++;
Log.d("mmm","#########"+count);
/*//用于启动Service的Intent
Intent intent1 = new Intent();
intent1.setClass(context, TimerService.class);
PendingIntent pIntent_StartService = PendingIntent.getService(context, 0, intent1, 0);*/
Intent intent = new Intent();
intent.setClass(context, TimerService.class);
context.startService(intent);
//用于停止Service中计时的intent() 通过广播
Intent intent2 = new Intent();
intent2.setAction(STOP_COUNT);
PendingIntent pIntent_StopCount = PendingIntent.getBroadcast(context, 0, intent2, 0);
//用于暂停Service中计时的intent() 通过广播
Intent intent3 = new Intent();
intent3.setAction(START_SUSPEND_COUNT);
PendingIntent pIntent__START_Suspend = PendingIntent.getBroadcast(context, 0, intent3, 0);
//rvs = new RemoteViews(context.getPackageName(), R.layout.timer_appwidget);
rvs.setTextViewText(R.id.textviewId, "0");
rvs.setOnClickPendingIntent(R.id.startButtonId, pIntent__START_Suspend);
rvs.setOnClickPendingIntent(R.id.stopButtonId, pIntent_StopCount);
appWidgetManager.updateAppWidget(appWidgetIds, rvs);
}
}
------解决思路----------------------
你这段代码写的少东西
应该用AppWidgetManager的updateAppWidget方法调用下才会更新,参数是
widgetId和RemoteView
切记在onReceive不调用这个方法是不会更新的
------解决思路----------------------
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
//如果更新所有的widget,那么
int[] widgetIDs = appWidgetManager
.getAppWidgetIds(new ComponentName(context,
NoteWidgetProvider.class));
appWidgetManager.updateAppWidget(widgetIDs, views);
//如果更新所有的widget,如果更新某一个widget,那么要从intent中得到id
//当然这个widgetID时你事先知道的,比如我想要点击某个widget,去配置它
Intent intent = new Intent(context, Editor.class);
intent.setAction(TAG + appWidgetId); // setAction to make intent
// unique or all widgets
// will use one intent
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
// intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK
------解决思路----------------------
// Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
views.setOnClickPendingIntent(R.id.root, pendingIntent);
//配置完成后给这个NoteWidgetProvider发送广播,从intent获取id
int widgetID = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
...
appWidgetManager.updateAppWidget(widgetID, views);