我开发了一个Android小部件,允许用户:
我的问题是:
这是我的代码:
LatestScuttleWidgetProvider扩展AppWidgetProvider
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
// TODO Auto-generated method stub
ComponentName latestWidget = new ComponentName(context,
LatestScuttleWidgetProvider.class);
int[] allWidgets = appWidgetManager.getAppWidgetIds(latestWidget);
for (int widgetId : allWidgets) {
Intent serviceIntent = new Intent(context,
DownloadLatestScuttleService.class);
serviceIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
widgetId);
context.startService(serviceIntent);
LogUtil.e("Latest scutify widget update", "update remote view");
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
/*
* (non-Javadoc)
*
* @see
* android.appwidget.AppWidgetProvider#onEnabled(android.content.Context)
*/
@Override
public void onEnabled(Context context) {
// TODO Auto-generated method stub
super.onEnabled(context);
}
/*
* (non-Javadoc)
*
* @see
* android.appwidget.AppWidgetProvider#onReceive(android.content.Context,
* android.content.Intent)
*/
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
if (intent.getAction().equals(
CommonConstant.DOWNLOAD_LATEST_SCUTTLE_WIDGET_ACTION)) {
LogUtil.e("Onreceive broadcast", intent.getAction());
int appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
LogUtil.e("updateWidgetListview", appWidgetId
+ "---------------------------");
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.layout_widget_latest_scuttles);
boolean isDownloading = intent.getBooleanExtra(
CommonConstant.DOWNLOAD_LATEST_SCUTTLE_WIDGET_DOWNLOADING,
false);
if (isDownloading) {
remoteViews.setViewVisibility(R.id.latest_widget_loading,
View.VISIBLE);
} else {
remoteViews.setViewVisibility(R.id.latest_widget_loading,
View.GONE);
// Set adapter for list view
Intent svcIntent = new Intent(context,
LatestScuttlesWidgetService.class);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
svcIntent.setData(Uri.parse(svcIntent
.toUri(Intent.URI_INTENT_SCHEME)));
remoteViews.setRemoteAdapter(R.id.latest_widget_listview,
svcIntent);
// Set item click event
Intent startActivityIntent = new Intent(context,
LatestScuttlesActivity.class);
PendingIntent startActivityPendingIntent = PendingIntent
.getActivity(context, 0, startActivityIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews
.setPendingIntentTemplate(R.id.latest_widget_listview,
startActivityPendingIntent);
// Update button click
Intent updateIntent = new Intent(context,
DownloadLatestScuttleService.class);
updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
PendingIntent updatePendingIntent = PendingIntent.getService(
context, 0, updateIntent, 0);
remoteViews.setOnClickPendingIntent(R.id.latest_widget_update,
updatePendingIntent);
// Notice data changes and update widget views
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId,
R.id.latest_widget_listview);
}
appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}
}
LatestScuttlesWidgetService扩展了RemoteViewsService
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
// TODO Auto-generated method stub
LogUtil.e("RemoteViewsFactory",
"--------------------RemoteViewsFactory------------------");
return (new LatestWidgetAdapter(getApplicationContext(), intent));
}
LatestWidgetAdapter实现了RemoteViewsFactory
public RemoteViews getViewAt(int position) {
// TODO Auto-generated method stub
final RemoteViews remoteView = new RemoteViews(
mContext.getPackageName(),
R.layout.layout_widget_latest_scuttles_list_item);
/*
* Date date = new Date(System.currentTimeMillis()); String updateTime =
* DateFormat.format("yyyy/MM/dd HH:mm:ss", date) .toString();
*/
ScuttleModel scuttle = (ScuttleModel) mScuttles.get(position);
String timeAgo = Utilities.getTimeAgo(scuttle.getSubmitted());
remoteView.setTextViewText(R.id.latest_widget_list_text,
scuttle.getUsername());
remoteView.setTextViewText(R.id.latest_widget_list_text2,
scuttle.getScuttle());
remoteView.setTextViewText(R.id.latest_widget_posted_time, timeAgo);
// Start activity when click reply button
Intent intent = new Intent();
// intent.setClass(mContext, LatestScuttlesActivity.class);
remoteView.setOnClickFillInIntent(R.id.latest_widget_reply, intent);
return remoteView;
}
/*
* (non-Javadoc)
*
* @see
* android.widget.RemoteViewsService.RemoteViewsFactory#getViewTypeCount()
*/
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 1;
}
/*
* (non-Javadoc)
*
* @see android.widget.RemoteViewsService.RemoteViewsFactory#hasStableIds()
*/
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return false;
}
/*
* (non-Javadoc)
*
* @see android.widget.RemoteViewsService.RemoteViewsFactory#onCreate()
*/
@Override
public void onCreate() {
// TODO Auto-generated method stub
mDatabase = new MySqliteDAO(mContext);
mDatabase.open();
mScuttles = mDatabase.getScuttles();
LogUtil.e("RemoteViewsService.RemoteViewsFactory#onCreate()", ""
+ mScuttles.size());
mDatabase.close();
}
/*
* (non-Javadoc)
*
* @see
* android.widget.RemoteViewsService.RemoteViewsFactory#onDataSetChanged()
*/
@Override
public void onDataSetChanged() {
// TODO Auto-generated method stub
mDatabase.open();
mScuttles = mDatabase.getScuttles();
LogUtil.e("RemoteViewsService.RemoteViewsFactory#onDataSetChanged()",
"" + mScuttles.size());
mDatabase.close();
}
DownloadLatestScuttleService扩展了IntentService
int appWidgetId = 0;
if (intent.hasExtra(AppWidgetManager.EXTRA_APPWIDGET_ID))
appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
// Send broadcast to display loading view
Intent widgetUpdateIntent = new Intent();
widgetUpdateIntent
.setAction(CommonConstant.DOWNLOAD_LATEST_SCUTTLE_WIDGET_ACTION);
widgetUpdateIntent
.putExtra(
CommonConstant.DOWNLOAD_LATEST_SCUTTLE_WIDGET_DOWNLOADING,
true);
widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
sendBroadcast(widgetUpdateIntent);
// Get data from api
HomeApi homeApi = new HomeApi();
List<BaseModel> scuttles = homeApi.getScuttles(30).getModels();
// Insert data to database
MySqliteDAO database = new MySqliteDAO(getApplicationContext());
database.open();
database.insertLatestScuttles(scuttles);
database.close();
// Callback
widgetUpdateIntent = new Intent();
widgetUpdateIntent
.setAction(CommonConstant.DOWNLOAD_LATEST_SCUTTLE_WIDGET_ACTION);
widgetUpdateIntent.putExtra(
CommonConstant.DOWNLOAD_LATEST_SCUTTLE_WIDGET_DOWNLOADING,
false);
widgetUpdateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetId);
sendBroadcast(widgetUpdateIntent);
LogUtil.e("onHandleIntent",
"------- sendBroadcast(widgetUpdateIntent) ----------");
并显示
<!-- Widget -->
<receiver android:name="com.scutify.widgets.LatestScuttleWidgetProvider" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="com.scutify.widgets.LatestScuttleCallBack" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/latest_scutlles_widget_provider" />
</receiver>
<service
android:name="com.scutify.widgets.services.DownloadLatestScuttleService"
android:permission="android.permission.BIND_REMOTEVIEWS" >
</service>
<service
android:name="com.scutify.widgets.services.LatestScuttlesWidgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
答案 0 :(得分:2)
在LatestScuttleWidgetProvider extends AppWidgetProvider
从:改变:
PendingIntent updatePendingIntent = PendingIntent.getService(
context, 0, updateIntent, 0);
为:
PendingIntent updatePendingIntent = PendingIntent.getService(
context, 0, updateIntent,
PendingIntent.FLAG_UPDATE_CURRENT);