Android(安卓)判断网络是否连接

第6天

1.连接手机的问题之 —Launch canceled
当用手机连接到eclipse时,会报这个错误

[2016-11-21 19:10:11 - Network] Launch canceled!

这个时候就算是Reset adb 也是没有用的,这时看下你的手机的版本是不是低于你程序所用的版本,如果是的话,你只需要在AndroidManifest.xml中把程序所用的版本调到和手机一致或者低于手机即可。
这里写图片描述

2.权限(permissions)
1.有许多的程序要正常运行都需要获得各种各样的权限,如果不获得相关权限的话,程序会报权限不足的错误比如:拨打电话,连网许可,拍照权限等等 很多权限。下面就是一个权限大全链接,大家可以看一下。

权限大全链接

那么我们如何添加需要的权限呢?
仍然是在AndroidManifest.xml 页面中,选择permissions视图。
就可以添加相关权限啦。
这里写图片描述

接下来就是判断网络是否连接的例子**

(1)Network.java

package com.example.network;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
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;

@SuppressLint("ShowToast") public class NetWork implements OnClickListener{

    private Context context;
    private TextView v;
    private ConnectivityManager cm;

    public NetWork(Context context){
        this.context = context;
    }

    @Override
    public void onClick(View view) {

        Activity c = (Activity)context;
        String netstatus;
        int color;

        try{

            v = (TextView)c.findViewById(R.id.textView1);
            //判断是否连接网络
            cm = (ConnectivityManager) c.getSystemService(c.CONNECTIVITY_SERVICE);
            NetworkInfo info = cm.getActiveNetworkInfo();

            if(info == null){

                color = c.getResources().getColor(R.color.red);
                netstatus = c.getResources().getString(R.string.status1);

            }else{
                color = c.getResources().getColor(R.color.green);
                netstatus = c.getResources().getString(R.string.status2);
            }
            v.setBackgroundColor(color);
            v.setText(netstatus);

        }catch(Exception e){
            e.printStackTrace();
        }   
            /*判断是否有连接网络的权限*/
        PackageManager pm = c.getPackageManager();
        boolean permission = (PackageManager.PERMISSION_GRANTED ==   
                pm.checkPermission("android.permission.ACCESS_NETWORK_STATE", "packageName"));  
        if (permission) {  
            Toast.makeText(c, "有这个权限", 0).show();  
        }else {  
            Toast.makeText(c, "木有这个权限", 0).show();  
        }       
    }   
}

(2)MainActivity.java

package com.example.network;

import android.app.Activity;
import android.os.Bundle;
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.button1);
        btn1.setOnClickListener(new NetWork(this));   

    }
}

(3)activity_main.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.network.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="36dp"
        android:layout_marginTop="29dp"
        android:layout_toRightOf="@+id/textView1"
        android:text="@string/button1" />
</RelativeLayout>

(4)strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Network</string>
    <string name="hello_world">网络状态</string>
    <string name="action_settings">Settings</string>
    <string name="status1">网络连接失败</string>
    <string name="status2">网络连接正常</string>

    <string name="button1">检测</string>
</resources>

例子思路:当点击一个button,判断手机 是否有网络,如果是有网络,则TextView 显示网络连接正常,然后TextView 背景颜色为绿色,反之TextView 显示网络连接正失败然后TextView 背景颜色为红色。只是简单的测试例子

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值