如何在用户设备上线时发送我的数据

huangapple go评论82阅读模式
英文:

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(&quot;InternetLog&quot;, &quot;Network Available Do operations&quot;,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

    &lt;receiver android:name=&quot;.NetworkChangeReceiver&quot;&gt;
    &lt;intent-filter&gt;
    &lt;action android:name=&quot;android.net.conn.CONNECTIVITY_CHANGE&quot; /&gt;.
    &lt;/intent-filter&gt;
    &lt;/receiver&gt;

  • 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 &amp;&amp; activeNetworkInfo.isConnected();
}

You will also need in your Manifest

&lt;uses-permission android:name=&quot;android.permission.ACCESS_NETWORK_STATE&quot; /&gt;

huangapple
  • 本文由 发表于 2020年4月8日 04:34:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/61088964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定