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

       工作中遇到的問題要注意總結,我在工作中遇到了問題,現在抽空簡單整理一下;

 

       第一個問題判斷手機當前上網用的是sim卡還是wifi,我寫了一個封裝的方法,以後可以拿來用:

    /**
     * check the internet is
     * mobile or wifi
     * add by wangxianming 
     * in 2012-03-22
     */
    private boolean checkWifi() {
        boolean isWifiConnect = true;
        ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        //check the networkInfos numbers
        NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
        for (int i = 0; i<networkInfos.length; i++) {
            if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
               if(networkInfos[i].getType() == cm.TYPE_MOBILE) {
                   isWifiConnect = false;
               }
               if(networkInfos[i].getType() == cm.TYPE_WIFI) {
                   isWifiConnect = true;
               }
            }
        }
        return isWifiConnect;
    }

 

 

        第二個例子:判斷當前的手機屏幕是否開啓了旋轉屏幕這個選項:

             /**
        	 * ACCELEROMETER_ROTATION---->explain:
        	 * 
        	 * Control whether the accelerometer will be 
        	 * used to change screen orientation. 
        	 * If 0, it will not be used unless explicitly 
        	 * requested by the application; 
        	 * if 1, it will be used by default 
        	 * unless explicitly disabled by the application. 
        	 * Constant Value: "accelerometer_rotation" 
        	 */
        	systemGravity = Settings.System.getInt(this
					.getContentResolver(),
				 Settings.System.ACCELEROMETER_ROTATION);//1 is open;0 is close;

 

 

         第三個是在代碼中註冊監聽內存卡狀態的廣播:     

        IntentFilter intentFilter=new IntentFilter);
        intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
        intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
        intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
        registerReceiver(sdcardListener,intentFilter);

          有registerReceiver()註冊廣播,就有unregisterReceiver()方法,他們是成對出現的。

          如果在onCreate()方法中註冊廣播,就在onDestroy()方法中釋放。

          如果在onResume()方法中註冊廣播,就在onPause()方法中釋放。

 

          在代碼中寫個內部類的廣播:

private final BroadcastReceiver sdcardListener=new BroadcastReceiver() {
		
		public void onReceive(Context context, Intent intent) {
			Toast.makeText(SummaryAppMainActivityActivity.this, R.string.sd_removed, 2000).show();
		}
    };

 


         第四個是全屏的設置:寫一個簡單的方法中;

  //set the activity is fullScreen
    private void setFullScreen() {
		misFullscreen = !misFullscreen;
		if (misFullscreen) {
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
					             WindowManager.LayoutParams.FLAG_FULLSCREEN);
		} else {
			getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		}
	}
今天先整理這麼少吧,抽空把知識串聯一下!呵呵,睡覺了,下次見!
今天參加移動語音開發者大會,見到了柳傳志和李開復雷軍沒有到場,有點遺憾。呵呵,有點收穫,聽了他們現場的訪談!

發佈了88 篇原創文章 · 獲贊 25 · 訪問量 124萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章