首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >onGetViewFactory未在onUpdate之后调用

onGetViewFactory未在onUpdate之后调用
EN

Stack Overflow用户
提问于 2017-09-12 12:56:31
回答 1查看 455关注 0票数 0

我在为我的应用程序设置一个小部件时遇到了困难,每当我试图将一个小部件添加到我的主屏幕上时,onReceive就被调用了,而在onUpdate被调用之后--但是在onUpdate完成之后--我的自定义RemoteViewsService根本就没有被调用.

NotesWidgetProvider.class

代码语言:javascript
复制
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

代码语言:javascript
复制
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;
    }
}
}

流形

代码语言:javascript
复制
<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

代码语言:javascript
复制
<?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,现在调用了onGetViewFactory --但是当到达onGetViewFactory时,小部件更改为“问题加载小部件”。

EN

回答 1

Stack Overflow用户

发布于 2018-04-21 15:22:47

现在回答可能为时已晚,但我认为您的问题来自于您的notes_widget_info.xml,,请将其更改为:

代码语言:javascript
复制
<?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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46177020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档