Android判断网络是否可用and Activity整体分析

Android的一个整体结构

  

 2通过对网络是否连接的案例来对Android的权限进行了解

   案例的描述:当点击一个按钮的时候将在页面中显示当前网络是否可用。这里我是使用Eclipse进行编写的。

   1.首先在项目创建好后,在activity_main.xml 布局中添加一个按键,并且添加一个TextView组件;代码如下:

      

    <!--Button里面的属性可以根据自己的实际需求进行修改-->

    

     <TextView
       android:id="@+id/textView2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignLeft="@+id/tableLayout1"
       android:layout_alignParentTop="true"
       android:layout_marginTop="38dp"
       android:text="TextView" />

    <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/tableLayout1"
      android:layout_alignParentTop="true"
      android:layout_marginLeft="54dp"
      android:layout_marginTop="96dp"
      android:text="@string/button1" />

  

2.在src-->com.example.test2下创建一个class 这个类implements OnClickListener接口代码如下:
    package com.example.test2;

    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;

  import com.example.test2.R;

  public class ButtonNetInfo implements OnClickListener {

    private Context context;//声明一个上下文对象(Activity)
    private TextView tv;//声明一个组件
    //创建一个构造器
    public ButtonNetInfo(Context context, int textView1) {
      this.context = context;
      this.tv = (TextView) ((Activity)context).findViewById(textView1);
    }
    //实现OnClickLIstener接口中的方法。
    @Override
    public void onClick(View arg0) {
      Activity c = (Activity) context;

      String text;//在tv组件中最后要显示的内容
      //这里是一个判断网络是否能用的代码。
      NetworkInfo networkInfo =null;
      try {
        ConnectivityManager connectivityManager= (ConnectivityManager)                      c.getSystemService(Context.CONNECTIVITY_SERVICE);
        networkInfo = connectivityManager.getActiveNetworkInfo();
      } catch(Exception e){
         e.printStackTrace();
          Toast toast = Toast.makeText(context,

                           "该软件未赋予权限,请赋予权限",                                        Toast.LENGTH_LONG);
          toast.show();
      }

      //在写这一步之前的话还要在res--->values--->string配置相对应的值;
      //如果networkInfo不为空的话则text=res--->values--->string--->name="text_View1"的值
      if(networkInfo !=null){
      text =c.getResources().getString(R.string.text_View1);
      } else {
      //如果networkInfo为空的话则text=res--->values--->string--->name="text_View0"的值
        text =c.getResources().getString(R.string.text_View0);
      }
        //最终将text在组件tv中显示。
        tv.setText(text);
      }

    }
3.在MainActivity中加入以下代码:
    private Button btn1;
    private EditText etxt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      // getSystemService(name);
      btn1 = (Button) findViewById(R.id.button1);

      btn1.setOnClickListener(new ButtonNetInfo(this,R.id.textView1));
    }
4.最重要的一步:赋予权限;
  在AndroidManifest.xml加上以下代码:
    <!--如果没有加上这一步的话,程序是运行不下去的。-->
    <uses-permission
      android:name="android.permission.ACCESS_NETWORK_STATE"
    />
    <uses-permission
      android:name="android.permission.INTERNET"
    />

完成以下步骤就能够执行。



    

posted on 2016-11-21 20:17  不尽昼夜  阅读(156)  评论(0)    收藏  举报

导航