gpt4 book ai didi

java - SyncAdapter- onPerformSync 无法访问互联网

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:01:08 24 4
gpt4 key购买 nike

我有一个 SyncAdapter 类,它连接到 MQTT 代理并为服务器发布负载以接收负载。但是,似乎即使调用了 onPerformSync() 方法,也无法访问互联网。我认为使用 SyncAdapter 可以保证访问互联网?

这是 SyncAdapter 类

public class SyncAdapter extends AbstractThreadedSyncAdapter {
private static final String TAG = SyncAdapter.class.getSimpleName();
private MqttHelper mqttHelper;

public SyncAdapter(Context context, boolean autoInitialize) {
super(context, autoInitialize);
mqttHelper = new MqttHelper(getContext());
}

public SyncAdapter(Context context, boolean autoInitialize, boolean allowParallelSyncs) {
super(context, autoInitialize, allowParallelSyncs);
}
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager
= (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

@Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult) {
Log.wtf(TAG, "onPerformSync: ");
Log.wtf(TAG, "SYNC_EXTRAS_MANUAL: " + extras.getBoolean(ContentResolver.SYNC_EXTRAS_MANUAL));
Log.wtf(TAG, "SYNC_EXTRAS_EXPEDITED: " + extras.getBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED));

Log.wtf(TAG, "internte: " + isNetworkAvailable());


mqttHelper.connect(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.wtf(TAG, "onSuccess: ");
mqttHelper.pub("hello/android", "Finally working via sync adapter praise the lord!!!!");
// TODO: Get Checkpoints from Realm
// TODO: publish at once
// TODO: Disconnect
mqttHelper.disconnect(new IMqttActionListener() {
@Override
public void onSuccess(IMqttToken asyncActionToken) {
Log.wtf(TAG, "onSuccess: disconnect");
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.wtf(TAG, "onFailure: disocnnect");
}
});
}

@Override
public void onFailure(IMqttToken asyncActionToken, Throwable exception) {
Log.wtf(TAG, "onFailure: ", exception);
}
});

}

@Override
public void onSyncCanceled() {
super.onSyncCanceled();
Log.wtf(TAG, "sync canceled");
}
}

还有一段关于 MqttService 和 SyncAdapter 的 Android Manifest:

<application
...
<receiver android:name=".LocationPollingReceiver" />
<service android:name="org.eclipse.paho.android.service.MqttService"
android:process=":sync"/>
<service
android:name=".LocationPollingService"
android:exported="false"/>
<service
android:name=".sync.AuthenticatorService">
<intent-filter>
<action android:name="android.accounts.AccountAuthenticator"/>
</intent-filter>
<meta-data
android:name="android.accounts.AccountAuthenticator"
android:resource="@xml/authenticator" />
</service>
<provider
android:name=".sync.StubProvider"
android:authorities="proj.com.fyp.provider"
android:exported="false"
android:syncable="true"/>
<service
android:name=".sync.SyncService"
android:exported="true"
android:process=":sync">
<intent-filter>
<action android:name="android.content.SyncAdapter"/>
</intent-filter>
<meta-data android:name="android.content.SyncAdapter"
android:resource="@xml/syncadapter" />
</service>
</application>

这与手动调用同步有什么关系吗?就像我在下面所做的那样?

Account mAccount = MainActivity.CreateSyncAccount(context);
Bundle settingsBundle = new Bundle();
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true);
//settingsBundle.putBoolean(ContentResolver.SYNC_EXTRAS_FORCE, true);
ContentResolver.requestSync(mAccount, AUTHORITY, settingsBundle);

即使通过 Settings->Account->Sync now 进行同步也会产生相同的结果。

最佳答案

让我解释一下。

onPerformSync() 是一个回调,您无法控制如何/何时调用它?这些类型的回调通常是异步任务,可以随时从外部(可以是远程)对象触发。这就是为什么我们通常将这些类型的回调放在我们的 MainThread(UI 线程)中,因为 MainThread 不能在整个应用程序中被杀死。 [注意:如果您在不同的进程中执行了服务,那么您也可以从该服务运行 onPerformSync()]。我这样说的目的是确保整个应用程序保持运行,这些回调可以随时执行。

我真的没有看到 onNetworkAvailable() 方法在这里的任何用途。如果你想从你这边做一些网络操作,你可以使用这个 onNetworkAvailable() 。

关于java - SyncAdapter- onPerformSync 无法访问互联网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44086816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com