Android 使用JobScheduler而不是BroadcastReceiver和Service
我正在开发一个应用程序,当设备连接到互联网时,它会在后台将我所有的数据库记录顺序上传到服务器 为此,我编写了一个Android 使用JobScheduler而不是BroadcastReceiver和Service,android,service,broadcastreceiver,broadcast,android-jobscheduler,Android,Service,Broadcastreceiver,Broadcast,Android Jobscheduler,我正在开发一个应用程序,当设备连接到互联网时,它会在后台将我所有的数据库记录顺序上传到服务器 为此,我编写了一个BroadcastReceiver,它将侦听网络连接。当这个接收器被触发时,我启动后台服务来上传记录 这是我的密码 public class NetworkChangeReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Int
BroadcastReceiver
,它将侦听网络连接。当这个接收器被触发时,我启动后台服务来上传记录
这是我的密码
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
AppUtils.checkInternetConnection(context));
//If the device has the internet connection and if there are any pending records to upload to server then start the service for uploading the records.
if (AppUtils.checkInternetConnection(context)) {
if (Database.getInstance().getTotalRecordsCount() > 0) {
context.startService(new Intent(context, SurveyUploadService.class));
}
} else {
context.stopService(new Intent(context, SurveyUploadService.class));
}
}
}
现在我的怀疑是
1。我可以用同样的方法吗?2。更好的方法是什么(我的还是使用JobScheduler的)以及为什么?我不知道您对BroadcastReceiver使用的是哪种操作,但我猜是连接更改操作。如果您使用它,请从侧面阅读以下关于: 为了缓解这些问题,Android 7.0应用了以下优化:
- 以Android 7.0为目标的应用程序不会收到连接性行动广播,即使它们有清单条目来请求通知这些事件。如果正在运行的应用程序请求使用BroadcastReceiver发出通知,它们仍然可以侦听其主线程上的连接更改
- 应用程序无法发送或接收ACTION_NEW_图片或ACTION_NEW_视频广播。这种优化会影响所有应用程序,而不仅仅是那些针对安卓7.0的应用程序
下面是一个。连接更改和连接操作之间的区别是什么?如果您查看此处,您将看到连接操作是值为“android.net.conn.CONNECTIVITY\u CHANGE”的常量的名称’。那太好了,谢谢。这是否意味着从目标>=N在后台检查连接更改是不可能的?是的,如果您使用作为隐式广播接收器,即作为清单的一部分。建议使用作业计划程序。您是否尝试实施作业服务?