android 自己總結的小工具類

1 dp 與 px 的相互轉換

  public class DensityUtil {  
  
     
    public static int dip2px(Context context, float dpValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (dpValue * scale + 0.5f);  
    }  
  
     
    public static int px2dip(Context context, float pxValue) {  
        final float scale = context.getResources().getDisplayMetrics().density;  
        return (int) (pxValue / scale + 0.5f);  
    }  
}  


2 -------- 判斷網絡狀態-----------------------------

    android:name="android.permission.ACCESS_NETWORK_STATE" />  
  
 private boolean getNetWorkStatus() {  
  
   boolean netSataus = false;  
   ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);  
  
   cwjManager.getActiveNetworkInfo();  
  
   if (cwjManager.getActiveNetworkInfo() != null) {  
   netSataus = cwjManager.getActiveNetworkInfo().isAvailable();  
   }  
  
   if (!netSataus) {  
   Builder b = new AlertDialog.Builder(this).setTitle("沒有可用的網絡")  
   .setMessage("是否對網絡進行設置?");  
   b.setPositiveButton("是", new DialogInterface.OnClickListener() {  
   public void onClick(DialogInterface dialog, int whichButton) {  
   Intent mIntent = new Intent("/");  
   ComponentName comp = new ComponentName(  
   "com.android.settings",  
   "com.android.settings.WirelessSettings");  
   mIntent.setComponent(comp);  
   mIntent.setAction("android.intent.action.VIEW");  
   startActivityForResult(mIntent,0);   
   }  
   }).setNeutralButton("否", new DialogInterface.OnClickListener() {  
   public void onClick(DialogInterface dialog, int whichButton) {  
   dialog.cancel();  
   }  
   }).show();  
   }  
  
   return netSataus;  
   }  



3 ----------調節屏幕亮度-------

public void setBrightness(int level) { 
ContentResolver cr = getContentResolver(); 
Settings.System.putInt(cr, "screen_brightness", level); 
Window window = getWindow(); 
LayoutParams attributes = window.getAttributes(); 
float flevel = level; 
attributes.screenBrightness = flevel / 255; 
getWindow().setAttributes(attributes); 



4--------檢查手機是否root ---------------------

 public static boolean checkRootingState(Context context)
    {
        
        final String ROOT_PATH = Environment.getExternalStorageDirectory() + "";
        final String ROOTING_PATH_1 = "/system/bin/su";
        final String ROOTING_PATH_2 = "/system/xbin/su";
        final String ROOTING_PATH_3 = "/system/app/SuperUser.apk";
        final String ROOTING_PATH_4 = context.getFilesDir().getPath() + "data/com.noshufou.android.su";
        
        // System.out.println("1111111111111111111   " + ROOTING_PATH_4);
        
        String[] rootFilesPath = new String[] { ROOT_PATH + ROOTING_PATH_1, ROOT_PATH + ROOTING_PATH_2,
                ROOT_PATH + ROOTING_PATH_3, ROOT_PATH + ROOTING_PATH_4 };
        
        boolean isRootingFlag = false;
        
        try
        {
            Runtime.getRuntime().exec("su");
            isRootingFlag = true;
        } catch (Exception e)
        {
            isRootingFlag = false;
        }
        
        if (!isRootingFlag)
        {
            isRootingFlag = checkRootingFiles(createFiles(rootFilesPath));
        }
        
        return isRootingFlag;
        
    }


5 --------------殺死當前進程--------------------------
  public static void killProc()
    {
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }


6 ----------防止快速點擊按鈕----------------
   public static boolean isFastDoubleClick()
    {
        long time = System.currentTimeMillis();
        long timeX = time - lastClickTime;
        if (0 < timeX && timeX < 500)
        {
            return true;
        }
        lastClickTime = time;
        return false;
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章