我在android手机上有一个打印机检查应用程序,它的基本检查表格是 检查员即使没有互联网连接也可以进行打印机检查,
I have a printer inspection app on android phone, its basic inspection form Inspector can work on a printer inspection even if he has no internet connection,
电话回到接收/互联网状态后,我想提交检查.
Once the phone is back with reception/internet, I would like to submit the inspection.
我当时正在考虑使用android服务来设计应用程序 因此它将使用sqlite保存检查详细信息,然后在有互联网连接时重新提交检查.
I was thinking to design the app using an android service so it will save the inspection details using sqlite, then when there is internet connection to resubmit the inspection .
但这需要服务才能定期检查互联网.并且会消耗大量的电池汁.
But this require service to periodically check for internet. and will consume significant battery juice.
我可以注册我的应用程序的钩子,以通过互联网连接通知该应用程序或服务吗?
Is there a hook I can register for my app to notify the app or the service on internet connection?
推荐答案对 Wi-fi 和移动互联网进行简单检查,如下所示... 在 Manifest.xml 中:
Simple check for both Wi-fi and Mobile internet as follows... in Manifest.xml :
<receiver android:name=".yourapp.ConnectivityChangeReceiver" > <intent-filter> <action android:name="android.conn.CONNECTIVITY_CHANGE" /> </intent-filter> </receiver>
制作一个新的 BroadcastReceiver :
public class ConnectivityChangeReceiver extends BroadcastReceiver { @Override public void onReceive(final Context context, final Intent intent) { if(checkInternet(context)) { Toast.makeText(context, "Network Available Do operations",Toast.LENGTH_LONG).show(); } } boolean checkInternet(Context context) { ServiceManager serviceManager = new ServiceManager(context); if (serviceManager.isNetworkAvailable()) { return true; } else { return false; } } }最后是 ServiceManager 类:
and finally ServiceManager class :
public class ServiceManager { Context context; public ServiceManager(Context base) { context = base; } public boolean isNetworkAvailable() { ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = cm.getActiveNetworkInfo(); return networkInfo != null && networkInfo.isConnected(); } }**不要忘记在清单文件中添加使用Internet的权限:
** Don't forget to add permission to use the Internet in your manifest file :
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" />还可以在vogella AndroidServices ...
Also check out this super cool article on vogella AndroidServices ...
更多推荐
API挂钩的连接更改
发布评论