在onUpdate之后没有调用onGetViewFactory

时间:2017-09-12 12:56:31

标签: android service android-widget

我在设置我的应用程序的小部件时遇到问题,每当我尝试将小部件添加到我的主屏幕时,正在调用onReceive并且正在调用onUpdate之后 - 但是在onUpdate完成之后 - 我的自定义RemoteViewsService不是完全被召唤......

NotesWidgetProvider.class

public class NotesWidgetProvider extends AppWidgetProvider {

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    for (int appWidgetId : appWidgetIds) {
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.notes_widget);
        Intent intent = new Intent(context, NotesWidgetService.class);
        views.setRemoteAdapter(R.id.widget_notes, intent);

        Intent clickIntent = new Intent(context, NoteActivity.class);
        PendingIntent clickPendingIntentTemplate = TaskStackBuilder.create(context)
                .addNextIntentWithParentStack(clickIntent)
                .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        views.setOnClickPendingIntent(R.id.widget_notes, clickPendingIntentTemplate);

        appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetId, R.id.widget_notes);

        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}

@Override
public void onReceive(Context context, Intent intent) {
    final String action = intent.getAction();
    if (action.equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE)) {
        AppWidgetManager mgr = AppWidgetManager.getInstance(context);
        ComponentName cn = new ComponentName(context, NotesWidgetProvider.class);
        mgr.notifyAppWidgetViewDataChanged(mgr.getAppWidgetIds(cn), R.id.widget_notes);
    }
    super.onReceive(context, intent);
}

public static void sendRefreshBroadcast(Context context) {
    Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
    intent.setComponent(new ComponentName(context, NotesWidgetProvider.class));
    context.sendBroadcast(intent);
}
}

NotesWidgetService.class

public class NotesWidgetService extends RemoteViewsService {

@Override
public RemoteViewsFactory onGetViewFactory(Intent intent) {
    return new NotesRemoteViewsFactory(this.getApplicationContext(), intent);
}

public class NotesRemoteViewsFactory implements RemoteViewsService.RemoteViewsFactory {

    private Context mContext;
    private Cursor mCursor;

    public NotesRemoteViewsFactory(Context applicationContext, Intent intent) {
        mContext = applicationContext;
    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onDataSetChanged() {
        if (mCursor != null) {
            mCursor.close();
        }
        final long identityToken = Binder.clearCallingIdentity();
        Uri uri = DBHandler.CONTENT_URI;
        String [] projection = {Constants.TITLE_COL, Constants.CONTENT_COL, Constants.COLOR_COL, Constants.DATE_COL};
        mCursor = mContext.getContentResolver().query(uri, projection, null, null, null);
        Binder.restoreCallingIdentity(identityToken);
    }

    @Override
    public void onDestroy() {
        if (mCursor != null) {
            mCursor.close();
        }
    }

    @Override
    public int getCount() {
        return mCursor == null ? 0 : mCursor.getCount();
    }

    @Override
    public RemoteViews getViewAt(int i) {
        if (i == AdapterView.INVALID_POSITION || mCursor == null || !mCursor.moveToPosition(getCount() - 1 - i)) {
            return null;
        }
        RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);
        rv.setTextViewText(R.id.widget_note_title, mCursor.getString(mCursor.getColumnIndex(Constants.TITLE_COL)));
        rv.setTextViewText(R.id.widget_note_content, mCursor.getString(mCursor.getColumnIndex(Constants.CONTENT_COL)));
        rv.setInt(R.id.widget_note_body, "setBackgroundColor", Color.parseColor(mCursor.getString(mCursor.getColumnIndex(Constants.COLOR_COL))));
        Intent fillInIntent = new Intent();
        fillInIntent.putExtra(Constants.POSITION_COL, getCount() - 1 - i);
        fillInIntent.putExtra("Code", NoteActivity.EDIT_CODE);
        rv.setOnClickFillInIntent(R.id.widget_note_body, fillInIntent);
        return rv;
    }

    @Override
    public RemoteViews getLoadingView() {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public long getItemId(int i) {
        return mCursor.moveToPosition(i) ? mCursor.getLong(0) : i;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }
}
}

清单

<receiver
        android:name=".widget.NotesWidgetProvider"
        android:label="@string/your_notes">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/notes_widget_info" />
    </receiver>

    <service
        android:name=".widget.NotesWidgetService"
        android:permission="android.permission.BIND_REMOTEVIEWS"
        android:exported="false"/>

notes_widget_info.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/notes_widget"
android:minHeight="180dp"
android:minWidth="110dp"
android:previewImage="@drawable/example_appwidget_preview"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="3600000"
android:widgetCategory="home_screen"/>

我错过了什么?有人可以帮忙搞清楚吗?

更新

我发现问题在于我使用了AppCompat.ImageButton而不是ImageButton,现在调用了GetGetViewFactory - 但是当到达onGetViewFactory时,widget变为“Problem loading widget”。

1 个答案:

答案 0 :(得分:0)

回答可能为时已晚,但我认为您的问题来自 notes_widget_info.xml ,请将其更改为以下内容:

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/notes_widget"
    android:minHeight="180dp"
    android:minWidth="110dp"
    android:previewImage="@drawable/example_appwidget_preview"
    android:resizeMode="horizontal|vertical"
    android:updatePeriodMillis="3600000"
    android:widgetCategory="home_screen">
</appwidget-provider>