英文:
How to send my data when the user device come online
问题
我有一个安卓应用,其中包含用户步数的某些数据,我希望在用户设备联机时将用户的步数数据发送到服务器,即使用户没有在我的应用程序中。如何最好地解决这个问题?
英文:
I have an android application that has some data from user steps count and I want to send the user steps counts to sever when the user device comes online even if the user is not in my application.
What is the best way to tackle that??
答案1
得分: 1
在高层次上,根据我的观点,解决方案应该如下所示:
- 创建一个接收器以获取连接状态变化的广播。代码如下所示:
public class NetworkChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(final Context context, final Intent intent) {
if (checkInternet(context)) {
Log.d("InternetLog", "Network Available Do operations");
// 在此执行后台任务
}
}
boolean checkInternet(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
}
- 确保在清单文件中注册它:
<receiver android:name=".NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>
- 完成以上步骤后,你的应用程序将能够在连接状态变化时被唤醒。
- 我建议使用一些后台任务将数据发送到服务器,你可以查阅服务组件或者工作管理器。
- 根据你选择的方式,在后台连接逻辑内实现,并在接收器中启动后台任务,将注释替换为相应的代码。
更新
作为替代方案,你可以使用以下方法来替代ServiceManager
:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
在你的清单文件中,还需要添加以下权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
英文:
On high-level the solution should look like next at my point f view:
-
Create receiver to get connectivity changing broadcasts. This should look like this
public class NetworkChangeReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { if(checkInternet(context)) { Log.d("InternetLog", "Network Available Do operations",Toast.LENGTH_LONG); //Do your background task here } } boolean checkInternet(Context context) { ServiceManager serviceManager = new ServiceManager(context); if (serviceManager.isNetworkAvailable()) { return true; } else { return false; } }
}
-
Be sure to register it in manifest also
<receiver android:name=".NetworkChangeReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />.
</intent-filter>
</receiver> -
After this is done, your application is ready to awake on connectivity change.
-
I recommend to use some background task to use data to the server, you should take a look at services component or work manager
-
Depending on what way you will choose just implement inside backend connecting logic and start background task in your receiver, just replace the comment with propper code
Update
As alternative, instead of ServiceManager
you can use this method
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
You will also need in your Manifest
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论