package com.Widget;
import java.util.Random;
import com.Launcher.LauncherActivity;
import com.Widget.wifiwidget.R;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;
/**
* A very basic {@link AppWidgetProvider} implementation that delegates the
* actual processing to the {@link WidgetService}.
*/
public class WidgetProvider extends AppWidgetProvider {
public static String ACTION_WIDGET_EXPEND = "ActionReceiverExpend";
public static String ACTION_WIDGET_EMERGENCY = "ActionReceiverEmergency";
public static String ACTION_WIDGET_DIAL = "ActionReceiverDial";
public static String EXTRA_WORD = "com.Widget.WORD";
int id;
@SuppressWarnings("deprecation")
@Override
public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
/*
* AppWidgetProvider extends BroadcastReceiver, so we must not spend
* lots of processing time in this class. Actual processing is done in a
* Service so that this method can return as quickly as possible.
*/
Log.d("onUpdate", "onUpdate");
for (int i = 0; i < appWidgetIds.length; i++) {
id = new Random().nextInt(10000000);
Intent svcIntent = new Intent(ctxt, ListItemsService.class);
svcIntent.setAction(ACTION_WIDGET_EXPEND);
svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
appWidgetIds[i] + id);
svcIntent.setData(Uri.parse(svcIntent
.toUri(Intent.URI_INTENT_SCHEME)));
RemoteViews widget = new RemoteViews(ctxt.getPackageName(),
R.layout.widget);
widget.setRemoteAdapter(appWidgetIds[i] + id, R.id.userData,
svcIntent);
Intent clickIntent = new Intent(ctxt, LauncherActivity.class);
PendingIntent clickPI = PendingIntent.getActivity(ctxt, 0,
clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
widget.setPendingIntentTemplate(R.id.userData, clickPI);
appWidgetManager.updateAppWidget(appWidgetIds[i], widget);
}
ctxt.startService(openIntentForlistService(ctxt));
ctxt.startService(getIntentForService(ctxt));
super.onUpdate(ctxt, appWidgetManager, appWidgetIds);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);
Log.d("onReceive", "onReceive");
context.startService(openIntentForlistService(context));
}
/**
* Stops the background service when the widget is removed.
*/
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
Log.d("onDeleted", "onDeleted");
context.stopService(getIntentForService(context));
context.stopService(openIntentForlistService(context));
super.onDeleted(context, appWidgetIds);
}
/**
* Helper method to create the correct {@link Intent} to use when working
* with the {@link WidgetService}.
*
* @param context
* Context to use for the Intent
* @return Intent that can be used to interact with the
* {@link WidgetService}
*/
private Intent getIntentForService(Context context) {
Intent widgetService = new Intent(context.getApplicationContext(),
WidgetService.class);
return widgetService;
}
private Intent openIntentForlistService(Context context) {
Intent dataListService = new Intent(context.getApplicationContext(),
ListItemsService.class);
return dataListService;
}
}
public class ListItemsService extends RemoteViewsService {
@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
Log.d("Start ListItemsService", "Start ListItemsService");
return (new WordListViewsFactory(this.getApplicationContext(), intent));
}
}
}
我的listView小部件未在锁定屏幕上更新,似乎 RemoteViewsService 未从锁定屏幕调用。
请参阅我的WidgetProvider Class代码
我认为这是解释问题的足够代码
答案 0 :(得分:1)
我遇到了同样的问题并得到了解决方案。 在WidgetProvider类的onReceive()方法中,只需输入以下代码即可在锁定屏幕上更新小部件:
try {
// Code for update on lock screen
int ids[] = AppWidgetManager.getInstance(context).getAppWidgetIds(
new ComponentName(context, WidgetProvider.class));
AppWidgetManager appWidgetManager = AppWidgetManager
.getInstance(context);
onUpdate(context, appWidgetManager, ids);
appWidgetManager.notifyAppWidgetViewDataChanged(ids,
R.layout.widget);
} catch (Exception e) {
e.printStackTrace();
}