Android——检查网络是否已经链接
新建一个项目testNet
添加一个button
layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.testnet.MainActivity" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/testNetwrkBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:text="@string/btn_testNet" />
</RelativeLayout>
新建一个外部类,实现接口OnClickListener
Btn1Listener.java:
package com.example.testnet; import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast; public class Btn1Listener implements OnClickListener {
private Context context;
private TextView tv1;
private ConnectivityManager cm;
public Btn1Listener(Context context) {
this.context = context;
} @Override
public void onClick(View v) {
//把上下文对象转型为Activity
Activity c = ((Activity) context);
//获取文本控件
tv1 = (TextView) c.findViewById(R.id.textView1);
// tv1.setBackgroundColor(0);
try{
//获取网络。。。。
cm = (ConnectivityManager) c.getSystemService(c.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if(info == null){
//提示没有网络
tv1.setText(R.string.text_Net1);
tv1.setBackgroundResource(R.color.red);
}else{
tv1.setText(R.string.text_Net0);
tv1.setBackgroundResource(R.color.green);
//不需要提示,继续执行相关的代码
}
}catch(RuntimeException e){
//用户没授权的时候给出提示(由于不知道怎么提示用户授权,只能用Toast了,怪我小白)
Toast.makeText(c, "没有权限,请授权", Toast.LENGTH_SHORT).show();
}
} }
然后在Activity类给button绑定点击事件
package com.example.testnet; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button; public class MainActivity extends Activity {
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.testNetwrkBtn);
btn1.setOnClickListener(new Btn1Listener(this));
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
注意:检查网络链接需要在AndroidManifest.xml中授予相关权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testnet"
android:versionCode="1"
android:versionName="1.0" >
<!-- 授予局域网权限 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- 授予互联网权限 -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="20" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application> </manifest>
然后就可以运行了:
网络已连接的运行结果:
网络未连接的运行结果:
最后是用户没有授权的结果:
由于我的是模拟器,实在找不到怎么取消他的权限,所以我把AndroidManifest.xml的授权注释掉了→_→:
Android——检查网络是否已经链接的更多相关文章
- android检查网络连接状态的变化,无网络时跳转到设置界面
在AndroidManifest.xml中加一个声明<receiver android:name="NetCheckReceiver"> <intent-filt ...
- android 检查网络连接状态实现步骤
获取网络信息需要在AndroidManifest.xml文件中加入相应的权限. <uses-permission android:name="android.permission.AC ...
- android 检查网络是否可用,如果不可用弹出设置,让用户改变
/** * 校验网络,如果没有网络,返回true * * @return boolean */ @Override public boolean hasInternetConnected() { Co ...
- android中 检查网络连接状态的变化,无网络时跳转到设置界面
1:在AndroidManifest.xml中加一个声明 <receiver android:name="NetCheckReceiver"> <inten ...
- 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)
1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- Android 检查手机网络是否可用
添加网络状态权限 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 代 ...
- Android中实现进入App之后检查网络状态
1,注册广播,一般使用静动态注册,即当程序退出的时候广播接受者就收不到消息使用方法context.registerReceiver()方法在MainActivity中的OnStart()方法中执行注册 ...
- Android okHttp网络请求之Get/Post请求
前言: 之前项目中一直使用的Xutils开源框架,从xutils 2.1.5版本使用到最近的xutils 3.0,使用起来也是蛮方便的,只不过最近想着完善一下app中使用的开源框架,由于Xutils里 ...
- Android检测网络是否正常代码!
在Android开发中,如果该应用程序需要连接网络请求,那么最好我们先做一个检测网络是否在线的判断,否则程序容易出现卡死或FC等Bug,应该判断如果手机离线则弹出提示让用户检查网络,如果正常则继续执行 ...
随机推荐
- K3 WISE开发手册
1.VB插件工程的命名.命名空间和生成的DLL命名要一致,否则导致注册不成功!
- SNF开发平台WinForm之十四-站内发送系统信息-SNF快速开发平台3.3-Spring.Net.Framework
1运行效果: 2开发实现: .组装站内信息发送实体对象. SNFService SNFService = new SNFService(); if (this.ucUser.SelectedIds ! ...
- Web程序员开发App系列 - 申请苹果开发者账号
Web程序员开发App系列 Web程序员开发App系列 - 认识HBuilder Web程序员开发App系列 - 申请苹果开发者账号 Web程序员开发App系列 - 调试Android和iOS手机代码 ...
- HMM 自学教程(四)隐马尔科夫模型
本系列文章摘自 52nlp(我爱自然语言处理: http://www.52nlp.cn/),原文链接在 HMM 学习最佳范例,这是针对 国外网站上一个 HMM 教程 的翻译,作者功底很深,翻译得很精彩 ...
- [mysql]MySQL忘记密码
1.修改MySQL的登录设置: vi /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 保存并且退出vi. 2.重新启动mysqld # /etc/init ...
- EPANET能做什么,不能做什么
What Epanet cand and cannot do Good news!Epanet can do most of the calculations you may need for you ...
- Action接收页面传来的参数方法
接收页面传来的参数方法 1.第一种:在action中设置相应的变量 在相应的action中设置与将要传进来的参数名相同的变量 eg: 页面传给后台两个参数 name=chance & age ...
- SAPI训练文件存储位置
查看注册表HKEY_CURRENT_USER\Software\Microsoft\Speech\RecoProfiles 说明查看http://msdn.microsoft.com/en-us/li ...
- C#语法糖之 cache操作类 asp.net
因为考虑到我下面我将写session cookies 等 操作类 ,与cache具有共性. 所以都统一继承了IHttpStorageObject abstract class 来保函数风格的统一 , ...
- 迭代接口的IEnumerator
我们经常在工作中用到对List,Dictionary对象的Foreach遍历,取出每一项. 其实这个接口很简单,只有一个属性2个方法. [ComVisible(true), Guid("49 ...