Android(判斷wifi是否開啟,手機螢幕狀態,sdcard是否被拔出,設定全屏)

來源:互聯網
上載者:User

       第一個問題判斷手機當前上網用的是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. 今天參加移動語音開發人員大會,見到了柳傳志和李開複雷軍沒有到場,有點遺憾。呵呵,有點收穫,聽了他們現場的訪談! 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.