我已经使用下面的代码实现了一个应用小部件,并且除了Android Lollipop之外,它都能正常工作。场景是我创建了一个服务,该服务跟踪时间并使用广播接收器更新小部件的RemoteView。
public class Widget extends AppWidgetProvider {
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
context.startService(new Intent(UpdateTimeService.UPDATE_TIME));
}
@Override
public void onDisabled(Context context) {
super.onDisabled(context);
context.stopService(new Intent(context, UpdateTimeService.class));
}
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
context.startService(new Intent(UpdateTimeService.UPDATE_TIME));
}
/*clock service*/
public static final class UpdateTimeService extends Service {
static final String UPDATE_TIME = "app.xxxxx.widget.action.UPDATE_TIME";
private Calendar mCalendar;
private final static IntentFilter mIntentFilter = new IntentFilter();
static {
mIntentFilter.addAction(Intent.ACTION_TIME_TICK);
mIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
mIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
}
@Override
public void onCreate() {
super.onCreate();
mCalendar = Calendar.getInstance();
registerReceiver(new UpdateTimeReceiver(), mIntentFilter);
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(new UpdateTimeReceiver());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
if (intent != null) {
if (UPDATE_TIME.equals(intent.getAction())) {
updateTime(getApplicationContext());
}
}
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public final class UpdateTimeReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
updateTime(context);
}
}
private void updateTime(Context context) {
mCalendar.setTimeInMillis(System.currentTimeMillis());
RemoteViews mRemoteViews = new RemoteViews(getPackageName(),
R.layout.widget);
try {
String time = DateFormat.format("h:mm",mCalendar).toString();
mRemoteViews.setTextViewText(R.id.Time, time);
updateWeather(mRemoteViews, context);
ComponentName mComponentName = new ComponentName(this,
Widget.class);
AppWidgetManager mAppWidgetManager = AppWidgetManager
.getInstance(this);
mAppWidgetManager.updateAppWidget(mComponentName,mRemoteViews);
} catch (Exception err) {
Toast.makeText(context, "Error", Toast.LENGTH_LONG).show();
}
}
}
}
/ *这是清单声明* /
<receiver
android:name="app.xxxxx.widget.Widget"
android:label="@string/widget_name" >
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/clock_widget" />
<intent-filter>
<action android:name="app.xxxxx.widget.action.UPDATE_TIME" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.TIME_TICK" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
<service
android:name="app.xxxxx.widget.Widget$UpdateTimeService"
>
<intent-filter>
<action android:name="app.xxxxx.widget.action.UPDATE_TIME" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIME_TICK" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</service>
<receiver
android:name="app.xxxxx.widget.Widget$UpdateTimeService$UpdateTimeReceiver"
android:process=":track_time_update" >
<intent-filter>
<action android:name="app.xxxxx.widget.action.UPDATE_TIME" />
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="android.intent.action.TIME_SET" />
<action android:name="android.intent.action.TIME_TICK" />
<action android:name="android.intent.action.TIMEZONE_CHANGED" />
</intent-filter>
</receiver>
棒棒糖版本需要其他配置吗?
最佳答案
在经历整整9天的苦难之后,我自己发现了一个不合逻辑的解决方案,我将targetSdkVersion从22更改为19,然后在Lolipop上运行了。