Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 同步适配器中的ContentResolver.requestSync在Android中不工作_Java_Android_Background Process_Stub_Android Syncadapter - Fatal编程技术网

Java 同步适配器中的ContentResolver.requestSync在Android中不工作

Java 同步适配器中的ContentResolver.requestSync在Android中不工作,java,android,background-process,stub,android-syncadapter,Java,Android,Background Process,Stub,Android Syncadapter,我正在尝试使用“StubProvider”和“StubAuthenticator”编写一个同步适配器,我遵循了官方的指导原则,我的代码运行时没有出现任何错误,但是没有调用“onPerformSync()”,我尝试了所有方法,但没有任何用处 我的完整项目可以从 以下是我正在使用的类: 班级活动 public class MainActivity extends FragmentActivity implements View.OnClickListener { // Constants

我正在尝试使用“StubProvider”和“StubAuthenticator”编写一个同步适配器,我遵循了官方的指导原则,我的代码运行时没有出现任何错误,但是没有调用“onPerformSync()”,我尝试了所有方法,但没有任何用处

我的完整项目可以从

以下是我正在使用的类:

班级活动

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    // Constants
    // The authority for the sync adapter's content provider
    public static final String AUTHORITY = "com.syncadaptertest.StubProvider";
    // An account type, in the form of a domain name
    public static final String ACCOUNT_TYPE = "com.syncadaptertest";
    // The account name
    public static final String ACCOUNT = "dummyaccount";
    // Instance fields
    Account mAccount;

    private ImageButton mRefreshBtn = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mRefreshBtn = (ImageButton) findViewById(R.id.refreshBtn);
        mRefreshBtn.setOnClickListener(this);

        // Create the dummy account
        mAccount = CreateSyncAccount(this);

    }

    /**
     * Create a new dummy account for the sync adapter
     *
     * @param context The application context
     */
    public static Account CreateSyncAccount(Context context) {
        // Create the account type and default account
        Account newAccount = new Account(ACCOUNT, ACCOUNT_TYPE);
        // Get an instance of the Android account manager
        AccountManager accountManager = (AccountManager) context.getSystemService(ACCOUNT_SERVICE);
        /*
         * Add the account and account type, no password or user data
         * If successful, return the Account object, otherwise report an error.
         */
        if (accountManager.addAccountExplicitly(newAccount, null, null)) {
            /*
             * If you don't set android:syncable="true" in
             * in your <provider> element in the manifest,
             * then call context.setIsSyncable(account, AUTHORITY, 1)
             * here.
             */
        } else {
            /*
             * The account exists or some other error occurred. Log this, report it,
             * or handle it internally.
             */
        }
        return newAccount;
    }


    @Override
    public void onClick(View v){

        onRefreshButtonClick(v);
    }



    /**
     * Respond to a button click by calling requestSync(). This is an
     * asynchronous operation.
     *
     * This method is attached to the refresh button in the layout
     * XML file
     *
     * @param v The View associated with the method call,
     * in this case a Button
     */
    public void onRefreshButtonClick(View v) {

        // Pass the settings flags by inserting them in a bundle
        Bundle settingsBundle = new Bundle();
        settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
        settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
        /*
         * Request the sync for the default account, authority, and
         * manual sync settings
         */
        ContentResolver.setIsSyncable(mAccount, AUTHORITY, 1);
        ContentResolver.requestSync(mAccount, AUTHORITY, settingsBundle);

        if(ContentResolver.isSyncActive(mAccount, AUTHORITY))
        {
            Log.d("testing1","testttt");
        }
        if(ContentResolver.isSyncPending(mAccount, AUTHORITY))
        {
            Log.d("testing2","testttt2");
        }

        List<SyncInfo> myList = ContentResolver.getCurrentSyncs();

    }

}
类身份验证服务

    public class AuthenticatorService extends Service {

    // Instance field that stores the authenticator object
    private Authenticator mAuthenticator;
    @Override
    public void onCreate() {
        // Create a new authenticator object
        mAuthenticator = new Authenticator(this);
    }
    /*
     * When the system binds to this Service to make the RPC call
     * return the authenticator's IBinder.
     */
    @Override
    public IBinder onBind(Intent intent) {
        return mAuthenticator.getIBinder();
    }
}
类同步服务

    public class SyncService extends Service {
    // Storage for an instance of the sync adapter
    private static SyncAdapter sSyncAdapter = null;
    // Object to use as a thread-safe lock
    private static final Object sSyncAdapterLock = new Object();
    /*
     * Instantiate the sync adapter object.
     */
    @Override
    public void onCreate() {
        /*
         * Create the sync adapter as a singleton.
         * Set the sync adapter as syncable
         * Disallow parallel syncs
         */
        synchronized (sSyncAdapterLock) {
            if (sSyncAdapter == null) {
                sSyncAdapter = new SyncAdapter(getApplicationContext(), true);
            }
        }
    }
    /**
     * Return an object that allows the system to invoke
     * the sync adapter.
     *
     */
    @Override
    public IBinder onBind(Intent intent) {
        /*
         * Get the object that allows external processes
         * to call onPerformSync(). The object is created
         * in the base class code when the SyncAdapter
         * constructors call super()
         */
        return sSyncAdapter.getSyncAdapterBinder();
    }
}
类存根提供程序

public class StubProvider extends ContentProvider {
    /*
     * Always return true, indicating that the
     * provider loaded correctly.
     */
    @Override
    public boolean onCreate() {
        return true;
    }
    /*
     * Return no type for MIME type
     */
    @Override
    public String getType(Uri uri) {
        return null;
    }
    /*
     * query() always returns no results
     *
     */
    @Override
    public Cursor query(
            Uri uri,
            String[] projection,
            String selection,
            String[] selectionArgs,
            String sortOrder) {
        return null;
    }
    /*
     * insert() always returns null (no URI)
     */
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        return null;
    }
    /*
     * delete() always returns "no rows affected" (0)
     */
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        return 0;
    }
    /*
     * update() always returns "no rows affected" (0)
     */
    public int update(
            Uri uri,
            ContentValues values,
            String selection,
            String[] selectionArgs) {
        return 0;
    }
}
类同步适配器

public class SyncAdapter extends AbstractThreadedSyncAdapter {
    private final AccountManager mAccountManager;

    public SyncAdapter(Context context, boolean autoInitialize) {
        super(context, autoInitialize);
        mAccountManager = AccountManager.get(context);
    }

    @Override
    public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
        Log.d("udinic", "onPerformSync for account[" + account.name + "]");
        try {
            // TODO Updating local tv shows

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

您是否在同步适配器的元数据xml上指定了帐户类型

您的xml描述器应该是这样的

<sync-adapter
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:contentAuthority="com.android.contacts"
    android:accountType="com.syncadaptertest"
    android:userVisible="true"
    android:supportsUploading="false"
    android:allowParallelSyncs="false"
    android:isAlwaysSyncable="true"/>

最重要的是,它应该在AndroidManifest文件中声明

<service
        android:name=".sync.ContactSyncService"
        android:exported="true">
        <intent-filter>
            <action android:name="android.content.SyncAdapter" />
        </intent-filter>
        <meta-data
            android:name="android.content.SyncAdapter"
            android:resource="@xml/sync_contact" />
    </service>

您确定它不工作吗

请记住,同步适配器是在绑定服务上运行的,该服务不在同一进程中,因此onPerformSync()中的Log.d()不会显示在应用程序主进程下的LogCat中,而是显示在同步适配器正在使用的进程中


尝试删除LogCat中的过滤器:选择“无过滤器”,而不是“仅显示选定的应用程序”

从android清单中删除android:process=“:sync”

您提供了很多代码,但到底是什么不起作用?请描述您试图实现的目标、预期结果以及实际结果。如果调用了
onPerformSync
,那么您的问题是什么?愚蠢的我,我刚刚编辑了….'onPerformSync’没有接到电话。你解决问题了吗?我也有同样的问题检查这个答案这真的解决了我两天来的头痛。谢谢:)同样的事情也发生在我身上。非常感谢:哦,经过数小时的调试,这是一条黄金线索。选择“无过滤器”后,我看到了日志,意识到我没有实现动态权限处理。非常感谢你!谢谢@Raphael我已经花了将近一个小时来研究这件事了!感谢您在高度优先的情况下指出,并给出正确的解释。大部分地方我都读过,但都被忽略了。
<service
        android:name=".sync.ContactSyncService"
        android:exported="true">
        <intent-filter>
            <action android:name="android.content.SyncAdapter" />
        </intent-filter>
        <meta-data
            android:name="android.content.SyncAdapter"
            android:resource="@xml/sync_contact" />
    </service>