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

来源:互联网 发布:手机网络不卡玩游戏卡 编辑:程序博客网 时间:2024/05/22 23:57

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

[java] view plain copy
 print?
  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 plain copy
 print?
  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;  

 注:判断网络和WIFI是否连接

Java代码  收藏代码
  1. public static boolean checkNetworkConnection(Context context)  
  2.    {  
  3.        final ConnectivityManager connMgr = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);  
  4.   
  5.        final android.net.NetworkInfo wifi =connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);  
  6.        final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);  
  7.   
  8.        if(wifi.isAvailable()||mobile.isAvailable())  
  9.            return true;  
  10.        else  
  11.            return false;  
  12.    }  

 

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

[java] view plain copy
 print?
  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 plain copy
 print?
  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 plain copy
 print?
  1. <span style="color:#000000;FONT-SIZE: 16px">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 plain copy
 print?
  1.  <span style="color:#000000;FONT-SIZE: 16px"//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.     }
阅读全文
1 0
原创粉丝点击