API挂钩的连接更改

编程入门 行业动态 更新时间:2024-10-25 04:19:34
本文介绍了API挂钩的连接更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在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挂钩的连接更改

本文发布于:2023-10-26 09:50:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529803.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:API

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!