- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个 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/
我在 AbstractThreadedSyncAdapter 的 onPerformSync 方法中进行网络操作。我想知道当我的应用程序在前台、后台和死机中运行时,这些操作是否默认在单独的线程上运行。
我的应用程序有一个按照 Android developer site 显示的结构创建的同步适配器. onPerformSync() 方法从互联网获取数据并使用批量插入将其插入数据库: Vector c
我为我的应用构建了一个 SyncAdapter,以便我可以使用 Google Cloud Messaging 触发数据库与服务器的同步。我实际上使用 Volley 进行网络调用并同步数据,但从我读到的
我正在使用 AbstractThreadedSyncAdapter 将我的 Android 手机中的一些数据与服务器同步。当用户使用手机的应用程序登录时,我会执行第一次同步,以便用户在开始使用我的
我有一个 SyncAdapter 类,它连接到 MQTT 代理并为服务器发布负载以接收负载。但是,似乎即使调用了 onPerformSync() 方法,也无法访问互联网。我认为使用 SyncAdapt
除了一件事,我的 syncadapter 运行良好。用户安装应用程序后,我的应用程序会同步两次。稍后,如果我在“设置”中手动同步它,它只会按预期同步一次。这只是发生这种情况的应用程序的第一次运行。 这
我关注这个标题Why does ContentResolver.requestSync not trigger a sync?但同步方法不会出现在设置屏幕的帐户和同步中。我认为 onPerformSy
正在尝试为我的内容提供者设置一个同步适配器/服务。内容提供者工作正常,能够毫无问题地存储数据。 我有一个向我的内容解析器注册的内容观察器,并且在数据更改后调用 onChange 方法。 但是,在我调用
我正在尝试实现同步适配器,我已经关注了This guide. 但是 onPerformSync() 在 ContentResolver.requestSync() 调用之后没有被触发。 我也尝试了一些
我正在创建一个 Android 应用程序,它向用户提供一个按钮来刷新数据库,以便本地数据和服务器上的数据保持同步。 我已经创建了帐户验证器、内容提供器并通过服务绑定(bind)了它们。 Content
我已经使用 Android 设置了一个 SyncAdapter sample project 、Steele 的两部分教程(1 和 2)和 Google I/O 2010 关于 REST 应用程序的演
当我将联系人添加到我的电话簿时,同步适配器的 onPerformSync() 方法被触发。但我无法弄清楚如何在该方法中检索新添加的联系方式。请帮助我提供一些指导和代码 fragment 。感谢您的帮助
我正在通过 SyncAdapter onPerformSync 发送几个改造调用,并且我试图通过通过 try/catch sleep 语句发送出去来调节 http 调用。但是,这会阻塞 UI,并且只有
你们能不能帮我一下。我一直在尝试在 onPerformSyn 中运行 yelp api。我怎么可能这样做。?我收到以下错误。 “没有空的构造函数”我正在使用这个 post尝试创建构造函数,但它对我不起
我使用 SyncAdapter 将我的数据与服务器同步。一切正常,但我很难向 SyncAdapter 发送额外内容。 在我的 ContentObserver 中,我使用以下代码向 SyncAdapte
我收到了大量来自用户的错误报告,这些错误报告来自强制关闭对话框,内容如下: java.lang.ExceptionInInitializerError at com.MyRequest.execute
我正在尝试使用 AbstractThreadedSyncAdapter 将我的本地数据库与我的服务器同步。我已经按照找到的指南进行操作,但无法正常工作。适用于 Android 2.2。 我的身份验证器
我是一名优秀的程序员,十分优秀!