1、android 怎么检测网络是否可用
实现步骤:
1、获取ConnectivityManager对象
Context context = activity.getApplicationContext();
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
2、获取NetworkInfo对象
// 获取NetworkInfo对象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
3、判断当前网络状态是否为连接状态
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
4、在AndroidManifest.xml中添加访问当前网络状态权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
代码如下:
public class ClassTestDemoActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (isNetworkAvailable(ClassTestDemoActivity.this))
{
Toast.makeText(getApplicationContext(), "当前有可用网络!", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "当前没有可用网络!", Toast.LENGTH_LONG).show();
}
}
/**
* 检查当前网络是否可用
*
* @param context
* @return
*/
public boolean isNetworkAvailable(Activity activity)
{
Context context = activity.getApplicationContext();
// 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager == null)
{
return false;
}
else
{
// 获取NetworkInfo对象
NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
if (networkInfo != null && networkInfo.length > 0)
{
for (int i = 0; i < networkInfo.length; i++)
{
System.out.println(i + "===状态===" + networkInfo[i].getState());
System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
// 判断当前网络状态是否为连接状态
if (networkInfo[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
}
}
return false;
}
}
2、android判断service是否运行
在开启一个服务之前应该判断该服务知否已经在运行
//本方法判断自己些的一个Service-->com.android.controlAddFunctions.PhoneService是否已经运行
public static boolean isWorked() {
ActivityManager myManager=(ActivityManager)context.getSystemService(Co ntext.ACTIVITY_SERVICE);
ArrayList<RunningServiceInfo> runningService = (ArrayList<RunningServi ceInfo>) myManager.getRunningServices(30);
for(int i = 0 ; i<runningService.size();i++)
{
if(runningService.get(i).service.getClassName().toString().equals("c om.android.controlAddFunctions.PhoneService"))
{
return true;
}
}
return false;
}
3、Android:如何检查服务器是否可用
1
4、android判断Tomcat是否可用
直接用电脑浏览器访问http://127.0.0.1:8080就行了,当然如果你自己设置了端口号,就把8080改成自己的端口号就行!如果出来tomcat页面,说明tomcat已经开启了。
5、android怎么判断网络是否可用
/**
* 获取当前网络类型
*
* @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
*/
public static int getNetworkType() {
int netType = 0;
ConnectivityManager connectivityManager = (ConnectivityManager) AppContext
.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_MOBILE) {
String extraInfo = networkInfo.getExtraInfo();
if (!StringUtil.isEmpty(extraInfo)) {
if (extraInfo.toLowerCase().equals("cmnet")) {
netType = NETTYPE_CMNET;
} else {
netType = NETTYPE_CMWAP;
}
}
} else if (nType == ConnectivityManager.TYPE_WIFI) {
netType = NETTYPE_WIFI;
}
return netType;
}
6、android 判断service是否运行
获取正在运行的所有服务,然后进行判断,如果是自己的service,一般都是提前设置一个标识,直接判断标识位即可;
获取正在运行的所有服务:
PackageManager pm = contex.getPackageManager();
ActivityManager am = (ActivityManager) contex.getSystemService(ACTIVITY_SERVICE);
// 得到系统安装的所有程序包的PackageInfo对象
List<PackageInfo> packs = pm.getInstalledPackages(0);
List<ActivityManager.RunningServiceInfo> listSer= am.getRunningServices(30);
// 获取正在运行的服务
for (PackageInfo p : packs) {
HashMap<String, Object> map2 = new HashMap<String, Object>();
for (ActivityManager.RunningServiceInfo ra : listSer) {
if (ra.process.equals(p.applicationInfo.packageName)) {
// 获得该进程的内存
int[] myMempid = new int[] { ra.pid };
Debug.MemoryInfo[] memoryInfo = am
.getProcessMemoryInfo(myMempid);
int memSize = memoryInfo[0].dalvikPrivateDirty;
map2.put("size", "内存为:" + memSize + "kb");
map2.put("icon", p.applicationInfo.loadIcon(pm));// 图标
map2.put("appName", p.applicationInfo.loadLabel(pm));// 应用程序名称
map2.put("packageName", "所在包名:"
+ p.applicationInfo.packageName);// 应用程序包名
System.out.println("pi.applicationInfo.packageName===="+p.applicationInfo.packageName);
items.add(map2);
}
}
}
7、android项目判断是否有可用网络
从程序开发的角度来开发该功能如下代码及代码中的注释即可理解:ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); // 检查网络连接,如果无网络可用,就不需要进行连网操作等 NetworkInfo info = manager.getActiveNetworkInfo(); if (info == null || !manager.getBackgroundDataSetting()) { return "无网络链接"; } return "网络畅通";