在后台线程中检查 android 牛轧糖的互联网连接
Check internet connectivity on android nougat in background thread
我正在开发针对 android 牛轧糖的 android 应用程序。如果连接到互联网,它应该在后台线程中对本地数据库进行远程 sql 同步。我尝试使用广播接收器来检测连接,但它没有在 android N 上触发。我如何在后台执行此操作(检查互联网可用性)?一个例子会有所帮助
我试过了
Android 清单
<receiver android:name=".NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
NetworkChangeReciever.java
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Toast.makeText(context,"Internet state changed",Toast.LENGTH_LONG).show();
}
private void registerInternetReceiver()
{
if (this.internetReceiver != null) return;
this.internetReceiver = new BroadcastReceiver()
{
@Override
public void onReceive (Context context, Intent intent)
{
if (isInternetAvailable()) {
Log.i ("Tag", "internet status online");
// write ur logic here
}
else
Log.i ("Tag", "internet status offline");
}
};
IntentFilter filter = new IntentFilter();
filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver (internetReceiver, filter);
}
private boolean isInternetAvailable()
{
try
{
return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return false;
}
// 使用上面的代码
牛轧糖应使用连接操作
IntentFilter intentFilter = new IntentFilter();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
//For Nougat
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
} else
{
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_CHANGE));
}
registerReceiver(new NetworkConnectionReceiver(), intentFilter);
我的问题得到了答案。
带动作的广播接收器
在清单中注册的 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
不会在 android N 中触发。但是可以通过编程方式注册广播并使其工作。但是当应用程序被杀死时它不会起作用。
然后在我的情况下,我有两个选择(这就是我的想法),使用
安排同步操作
GCM 网络管理员
或
作业调度程序
作业调度程序的问题在于它只能在 API 级别 21 以上工作,但不需要 Google 播放服务即可工作。另一方面,GCM 网络管理器具有向后兼容性,但需要 google 向 运行 播放服务。
然后我访问了Intelligent Job Scheduling, and they said, go with Firebase Job dispatcher,
Firebase JobDispatcher supports the use of Google Play services as an
implementation for dispatching (running) jobs, but the library also
allows you to define and use other implementations: For example, you
might decide to use JobScheduler or write your own, custom code.
Because of this versatility, we recommend that you use this Firebase
JobDispatcher if your app targets a version of Android lower than 5.0
(API level 21).
我采纳了这个建议,
这就是我所做的,
创建了执行同步作业的服务 (SyncService.java),
将该服务添加到清单
<service
android:exported="false"
android:name=".SyncService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
并且在我的 activity 中,创建并执行了一个要同步的作业
Job syncJob=jobDispatcher.newJobBuilder()
.setService(SyncService.class)
.setRecurring(true)
.setLifetime(Lifetime.FOREVER)
.setRecurring(true)
.setLifetime(Lifetime.FOREVER)
.setTrigger(Trigger.executionWindow(3,600))
.setConstraints(Constraint.ON_ANY_NETWORK) //does job when network is available
.setReplaceCurrent(true)
.setTag("syncService")
.build();
jobDispatcher.mustSchedule(syncJob);
这在后台工作。
我正在开发针对 android 牛轧糖的 android 应用程序。如果连接到互联网,它应该在后台线程中对本地数据库进行远程 sql 同步。我尝试使用广播接收器来检测连接,但它没有在 android N 上触发。我如何在后台执行此操作(检查互联网可用性)?一个例子会有所帮助
我试过了
Android 清单
<receiver android:name=".NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
NetworkChangeReciever.java
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
Toast.makeText(context,"Internet state changed",Toast.LENGTH_LONG).show();
}
private void registerInternetReceiver()
{
if (this.internetReceiver != null) return;
this.internetReceiver = new BroadcastReceiver()
{
@Override
public void onReceive (Context context, Intent intent)
{
if (isInternetAvailable()) {
Log.i ("Tag", "internet status online");
// write ur logic here
}
else
Log.i ("Tag", "internet status offline");
}
};
IntentFilter filter = new IntentFilter();
filter.addAction (ConnectivityManager.CONNECTIVITY_ACTION);
registerReceiver (internetReceiver, filter);
}
private boolean isInternetAvailable()
{
try
{
return (Runtime.getRuntime().exec ("ping -c 1 google.com").waitFor() == 0);
}
catch (Exception ex)
{
ex.printStackTrace();
}
return false;
}
// 使用上面的代码
牛轧糖应使用连接操作
IntentFilter intentFilter = new IntentFilter();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
//For Nougat
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
} else
{
intentFilter.addAction(ConnectivityManager.CONNECTIVITY_CHANGE));
}
registerReceiver(new NetworkConnectionReceiver(), intentFilter);
我的问题得到了答案。
带动作的广播接收器
在清单中注册的 <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
不会在 android N 中触发。但是可以通过编程方式注册广播并使其工作。但是当应用程序被杀死时它不会起作用。
然后在我的情况下,我有两个选择(这就是我的想法),使用
安排同步操作GCM 网络管理员
或
作业调度程序
作业调度程序的问题在于它只能在 API 级别 21 以上工作,但不需要 Google 播放服务即可工作。另一方面,GCM 网络管理器具有向后兼容性,但需要 google 向 运行 播放服务。
然后我访问了Intelligent Job Scheduling, and they said, go with Firebase Job dispatcher,
Firebase JobDispatcher supports the use of Google Play services as an implementation for dispatching (running) jobs, but the library also allows you to define and use other implementations: For example, you might decide to use JobScheduler or write your own, custom code. Because of this versatility, we recommend that you use this Firebase JobDispatcher if your app targets a version of Android lower than 5.0 (API level 21).
我采纳了这个建议, 这就是我所做的,
创建了执行同步作业的服务 (SyncService.java),
将该服务添加到清单
<service
android:exported="false"
android:name=".SyncService">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>
并且在我的 activity 中,创建并执行了一个要同步的作业
Job syncJob=jobDispatcher.newJobBuilder()
.setService(SyncService.class)
.setRecurring(true)
.setLifetime(Lifetime.FOREVER)
.setRecurring(true)
.setLifetime(Lifetime.FOREVER)
.setTrigger(Trigger.executionWindow(3,600))
.setConstraints(Constraint.ON_ANY_NETWORK) //does job when network is available
.setReplaceCurrent(true)
.setTag("syncService")
.build();
jobDispatcher.mustSchedule(syncJob);
这在后台工作。