Android AppWigdet 不适用于棒棒糖
Android AppWigdet is not working on lollipop only
我已经使用以下代码实现了一个应用小部件,除了 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>
Lollipop 版本是否需要任何其他配置
在经历了整整 9 天后,我自己找到了一个不合逻辑的解决方案,我只是将 targetSdkVersion 从 22 更改为 19,然后它就可以在 Lolipop 上运行了。
我已经使用以下代码实现了一个应用小部件,除了 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>
Lollipop 版本是否需要任何其他配置
在经历了整整 9 天后,我自己找到了一个不合逻辑的解决方案,我只是将 targetSdkVersion 从 22 更改为 19,然后它就可以在 Lolipop 上运行了。