Android(判断wifi是否开启,手机屏幕状态,sdcard是否被拔出,设置全屏)

来源:互联网 发布:调查报告数据统计样本 编辑:程序博客网 时间:2024/05/22 17:01
工作中遇到的问题要注意总结,我在工作中遇到了问题,现在抽空简单整理一下;

 

       第一个问题判断手机当前上网用的是sim卡还是wifi,我写了一个封装的方法,以后可以拿来用:

[java] view plaincopyprint?
  1. /** 
  2.  * check the internet is 
  3.  * mobile or wifi 
  4.  * add by wangxianming  
  5.  * in 2012-03-22 
  6.  */  
  7. private boolean checkWifi() {  
  8.     boolean isWifiConnect = true;  
  9.     ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);  
[java] view plaincopyprint?
  1. //check the networkInfos numbers   
  2. NetworkInfo[] networkInfos = cm.getAllNetworkInfo();  
  3. for (int i = 0; i<networkInfos.length; i++) {  
  4.     if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {  
  5.        if(networkInfos[i].getType() == cm.TYPE_MOBILE) {  
  6.            isWifiConnect = false;  
  7.        }  
  8.        if(networkInfos[i].getType() == cm.TYPE_WIFI) {  
  9.            isWifiConnect = true;  
  10.        }  
  11.     }  
  12. }  
  13. return isWifiConnect;  

 

 

        第二个例子:判断当前的手机屏幕是否开启了旋转屏幕这个选项:

[java] view plaincopyprint?
  1.         /** 
  2.      * ACCELEROMETER_ROTATION---->explain: 
  3.      *  
  4.      * Control whether the accelerometer will be  
  5.      * used to change screen orientation.  
  6.      * If 0, it will not be used unless explicitly  
  7.      * requested by the application;  
  8.      * if 1, it will be used by default  
  9.      * unless explicitly disabled by the application.  
  10.      * Constant Value: "accelerometer_rotation"  
  11.      */  
  12.     systemGravity = Settings.System.getInt(this  
  13. .getContentResolver(),  
  14. Settings.System.ACCELEROMETER_ROTATION);//1 is open;0 is close;  

 

 

         第三个是在代码中注册监听内存卡状态的广播:     

[java] view plaincopyprint?
  1. IntentFilter intentFilter=new IntentFilter);  
  2. intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);  
  3. intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);  
  4. intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);  
  5. intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);  
  6. registerReceiver(sdcardListener,intentFilter);  

          有registerReceiver()注册广播,就有unregisterReceiver()方法,他们是成对出现的。

          如果在onCreate()方法中注册广播,就在onDestroy()方法中释放。

          如果在onResume()方法中注册广播,就在onPause()方法中释放。

 

          在代码中写个内部类的广播:

[java] view plaincopyprint?
  1. <span style="font-size:16px;color:#000000;">private final BroadcastReceiver sdcardListener=new BroadcastReceiver() {  
  2.           
  3.         public void onReceive(Context context, Intent intent) {  
  4.             Toast.makeText(SummaryAppMainActivityActivity.this, R.string.sd_removed, 2000).show();  
  5.         }  
  6.     };</span>  

 


         第四个是全屏的设置:写一个简单的方法中;

[java] view plaincopyprint?
  1.  <span style="font-size:16px;color:#000000;"//set the activity is fullScreen  
  2.     private void setFullScreen() {  
  3.         misFullscreen = !misFullscreen;  
  4.         if (misFullscreen) {  
  5.             getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
  6.                                  WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  7.         } else {  
  8.             getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  9.         }  
  10.     }</span>  
  11. <span style="color:#ff6600;"><strong><span style="font-size:18px;">今天先整理这么少吧,抽空把知识串联一下!呵呵,睡觉了,下次见!  
  12. 今天参加移动语音开发者大会,见到了柳传志和李开复雷军没有到场,有点遗憾。呵呵,有点收获,听了他们现场的访谈!  
  13. </span>  
  14. </strong></span>  

判断网络是否可用

在Android手机中判断是否联网可以通过 ConnectivityManager 类的isAvailable()方法判断,首先获取网络通讯类的实例

 

ConnectivityManager cwjManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
  NetworkInfo info = cwjManager.getActiveNetworkInfo();
  if (info != null && info.isAvailable()){

       //do something
  }

 

来返回是否有效,如果为True则表示当前Android手机已经联网,可能是WiFi或GPRS、HSDPA等等,具体的可以通过ConnectivityManager 类的getActiveNetworkInfo() 方法判断详细的接入方式,需要注意的是有关调用需要加入网络访问权限

 

原创粉丝点击