我开发了一个应用程序,它有一个注册表单。 如果候选移动设备连接到网络,则注册表单中的数据将上传到WebService 。 否则,数据将存储在本地MySQL数据库中。
我现在想要的是,当移动连接到网络时,本地存储的数据应该上传到WebService,即使他没有打开该应用程序。 我期待功能与WHATSAPP相同。
编辑解决
最后我找到了解决这个问题的方法。非常感谢大家的参与。
package com.example.detapp; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; import android.widget.Toast; public class InternetConnector_Receiver extends BroadcastReceiver { boolean isVisible; public static int flag1=0; public InternetConnector_Receiver() { } @Override public void onReceive(Context context, Intent intent) { try { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager .getActiveNetworkInfo(); // Check internet connection and accrding to state change the // text of activity by calling method if ((networkInfo.isConnected()==true)) { Log.i("*************DEEPA* insideif", "*********network : " +networkInfo.isConnected() ); flag1=1; Toast.makeText(context, "*INTERNET CONNECTED*", Toast.LENGTH_SHORT).show(); new MainActivity().Synchronization(flag1); } else { flag1=0; Toast.makeText(context, "*NO INTERNET*", Toast.LENGTH_SHORT).show(); new MainActivity().Synchronization(flag1); } } catch (Exception e) { e.printStackTrace(); } } }`
添加了此广播接收器类InternetConnector_Receiver.java。即使您不在应用程序中,这也将起作用。
在MainActivity.java中,我添加了以下代码
protected void Synchronization(int flag) { if(flag==1) { //call webservice or sync Adapters here to synch data } }再次感谢大家
I have developed an application, which has a registration form. The data from registration form will be uploaded to WebService if candidates mobile is connected to the network. Else that data will be stored in local MySQL database.
What I want now is, when mobile is connected to a network, then the locally stored data should upload to WebService, even though he don't open that application. I am expecting functionality same as of WHATSAPP.
Edited Solved
Finally i found solution to this problem.Thank you so much everyone for participation.
package com.example.detapp; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.util.Log; import android.widget.Toast; public class InternetConnector_Receiver extends BroadcastReceiver { boolean isVisible; public static int flag1=0; public InternetConnector_Receiver() { } @Override public void onReceive(Context context, Intent intent) { try { ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connectivityManager .getActiveNetworkInfo(); // Check internet connection and accrding to state change the // text of activity by calling method if ((networkInfo.isConnected()==true)) { Log.i("*************DEEPA* insideif", "*********network : " +networkInfo.isConnected() ); flag1=1; Toast.makeText(context, "*INTERNET CONNECTED*", Toast.LENGTH_SHORT).show(); new MainActivity().Synchronization(flag1); } else { flag1=0; Toast.makeText(context, "*NO INTERNET*", Toast.LENGTH_SHORT).show(); new MainActivity().Synchronization(flag1); } } catch (Exception e) { e.printStackTrace(); } } }`
Added this broadcast receiver class InternetConnector_Receiver.java.This will work even when you are off to application.
And in MainActivity.java i added following code
protected void Synchronization(int flag) { if(flag==1) { //call webservice or sync Adapters here to synch data } }Thank you all once again
最满意答案
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class CustomBroadCastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(isNetworkAvail(context)){ //TODO: } } public boolean isNetworkAvail(Context mContext) { try { ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) for (int i = 0; i < info.length; i++) if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } return false; } catch (Exception e) { e.printStackTrace(); return false; } } } import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.net.ConnectivityManager; import android.net.NetworkInfo; public class CustomBroadCastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if(isNetworkAvail(context)){ //TODO: } } public boolean isNetworkAvail(Context mContext) { try { ConnectivityManager connectivity = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE); if (connectivity != null) { NetworkInfo[] info = connectivity.getAllNetworkInfo(); if (info != null) for (int i = 0; i < info.length; i++) if (info[i].getState() == NetworkInfo.State.CONNECTED) { return true; } } return false; } catch (Exception e) { e.printStackTrace(); return false; } } }更多推荐
发布评论