Android AppWidget 不适用于 Lollipop

标签 android android-appwidget

我已经使用以下代码实现了一个应用小部件,除了 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();
        }
    }
}

/*这是 list 声明*/

<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 AppWidget 不适用于 Lollipop ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31711128/

相关文章:

android - WebView 不调整大小

android - Home 进程重启后解耦的 AppWidget Intents

android - 确定主屏幕的 appwidgets 空间网格大小

java - Android的MediaPlayer是如何通过检查原生代码来工作的?

android - 我不能动态地执行 getResources.getString 吗?

Android:将 ACTION_PICK_ACTIVITY 与 AppWidget 结合使用来确定在按下时打开哪个应用程序

java - Android 服务后台 Android Oreo

android - 将不同的行 ID 从 appWidget 传递到任务中的单个 Activity 实例

java - 如何仅检索特定用户的firebase数据库?

android - 带有监听器的通用 DialogFragment