免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
【收藏篇】Android 開發(fā)中常用的10種工具類

導(dǎo)語

 Android開發(fā)中,收集一些常用的代碼工具類是非常重要的。現(xiàn)在Android開發(fā)技術(shù)已經(jīng)很成熟了,很多代碼大牛已經(jīng)寫出了很多框架和工具類,我們現(xiàn)在應(yīng)該要站在巨人的肩膀上做開發(fā)了。今天我把平時開發(fā)中收集最常用的 10 個工具類,分享給大家。以后開發(fā)中合理利用,對于在平時開發(fā)中的效率是非常有幫助的 。

1安裝應(yīng)用APK

// 安裝應(yīng)用APK private void installApk(String apkFilePath) {        File apkfile = new File(apkFilePath);        
       if (!apkfile.exists()) {            
           return;        }        Intent i = new Intent(Intent.ACTION_VIEW);        i.setDataAndType(Uri.parse("file://" + apkfile.toString()), "application/vnd.android.package-archive");        startActivity(i);    }}

2MD5算法

public final static String MD5(String s) {        
           char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};        
           try {            
           byte[] btInput = s.getBytes();          // 獲得MD5摘要算法的 MessageDigest 對象            MessageDigest mdInst = MessageDigest.getInstance("MD5");  // 使用指定的字節(jié)更新摘要            mdInst.update(btInput);                 // 獲得密文            byte[] md = mdInst.digest();            // 把密文轉(zhuǎn)換成十六進制的字符串形式            int j = md.length;            
           char str[] = new char[j * 2];            
           int k = 0;            //把字節(jié)轉(zhuǎn)換成對應(yīng)的字符串            for (int i = 0; i < j; i++) {                
               byte byte0 = md[i];                str[k++] = hexDigits[byte0 >>> 4 & 0xf];                str[k++] = hexDigits[byte0 & 0xf];            }          
                return new String(str);        } catch (Exception e) {            e.printStackTrace();            
                return null;        }    }

3判斷現(xiàn)在是否有網(wǎng)絡(luò)

private boolean NetWorkStatus() {        ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);        cwjManager.getActiveNetworkInfo();        
       boolean netSataus = true;        
       if (cwjManager.getActiveNetworkInfo() != null) {        netSataus = cwjManager.getActiveNetworkInfo().isAvailable();        Toast.makeText(this, "網(wǎng)絡(luò)已經(jīng)打開", Toast.LENGTH_LONG).show();        }
   else {        Builder b = new AlertDialog.Builder(this).setTitle("沒有可用的網(wǎng)絡(luò)")                                        .setMessage("是否對網(wǎng)絡(luò)進行設(shè)置?");        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;        }

4獲取設(shè)備屏幕的寬度高度密度

public  class   DisplayUtil {     /**     * 得到設(shè)備屏幕的寬度     */    public static int getScreenWidth(Context context) {        
       return context.getResources().getDisplayMetrics().widthPixels;    }    /**     * 得到設(shè)備屏幕的高度     */    public static int getScreenHeight(Context context) {        
       return context.getResources().getDisplayMetrics().heightPixels;    }    /**     * 得到設(shè)備的密度     */    public static float getScreenDensity(Context context) {        
       return context.getResources().getDisplayMetrics().density;    }    }

5dp、sp 轉(zhuǎn)換為 px 的工具類

public  class   DisplayUtil {              /**     * 將px值轉(zhuǎn)換為dip或dp值,保證尺寸大小不變     */    public static int px2dip(Context context, float pxValue) {        final float scale = context.getResources().getDisplayMetrics().density;        return (int) (pxValue / scale + 0.5f);    }    /**     * 將dip或dp值轉(zhuǎn)換為px值,保證尺寸大小不變     *     * @param dipValue     * @param scale     *            (DisplayMetrics類中屬性density)     * @return     */    public static int dip2px(Context context, float dipValue) {        
       final float scale = context.getResources().getDisplayMetrics().density;        
       return (int) (dipValue * scale + 0.5f);    }    /**     * 將px值轉(zhuǎn)換為sp值,保證文字大小不變     *     * @param pxValue     * @param fontScale     *            (DisplayMetrics類中屬性scaledDensity)     * @return     */    public static int px2sp(Context context, float pxValue) {        
       final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;        
       return (int) (pxValue / fontScale + 0.5f);    }    /**     * 將sp值轉(zhuǎn)換為px值,保證文字大小不變     *     * @param spValue     * @param fontScale     *            (DisplayMetrics類中屬性scaledDensity)     * @return     */    public static int sp2px(Context context, float spValue) {        
       final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;        
       return (int) (spValue * fontScale + 0.5f);    }    }

6drawable轉(zhuǎn)bitmap的工具類

/** * drawable轉(zhuǎn)bitmap * * @param drawable * @return */private Bitmap drawableToBitamp(Drawable drawable) {    
       if (null == drawable) {        
       return null;        }if (drawable instanceof BitmapDrawable) {        BitmapDrawable bd = (BitmapDrawable) drawable;        
       return bd.getBitmap();    }    
       int w = drawable.getIntrinsicWidth();    
       int h = drawable.getIntrinsicHeight();        Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(bitmap);        drawable.setBounds(0, 0, w, h);        drawable.draw(canvas);    
       return bitmap;}

7文章發(fā)布模仿朋友圈時間顯示

   public static String ChangeTime(Date time) {        String ftime = "";        Calendar cal = Calendar.getInstance();        // 判斷是否是同一天        String curDate = dateFormater2.get().format(cal.getTime());        String paramDate = dateFormater2.get().format(time);        
   if (curDate.equals(paramDate)) {            
   int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);            
   if (hour == 0)                ftime = Math.max(                        (cal.getTimeInMillis() - time.getTime()) / 60000, 1)                        + "分鐘前";            
           else                ftime = hour + "小時前";            
           return ftime;        }        long lt = time.getTime() / 86400000;        
           long ct = cal.getTimeInMillis() / 86400000;        
           int days = (int) (ct - lt);        
           if (days == 0) {            
           int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);            
           if (hour == 0)            ftime = Math.max( (cal.getTimeInMillis() - time.getTime()) / 60000, 1)                        + "分鐘前";                       else                ftime = hour + "小時前";        } else if (days == 1) {            ftime = "昨天";        } else if (days == 2) {            ftime = "前天 ";        } else if (days > 2 && days < 31) {            ftime = days + "天前";        } else if (days >= 31 && days <= 2 * 31) {            ftime = "一個月前";        } else if (days > 2 * 31 && days <= 3 * 31) {            ftime = "2個月前";        } else if (days > 3 * 31 && days <= 4 * 31) {            ftime = "3個月前";        } else {            ftime = dateFormater2.get().format(time);        }        return ftime;    }    public static String friendly_time(String sdate) {        Date time = null;        
           if (TimeZoneUtil.isInEasternEightZones())            time = toDate(sdate);        else            time = TimeZoneUtil.transformTime(toDate(sdate),                    TimeZone.getTimeZone("GMT+08"), TimeZone.getDefault());        
           if (time == null) {            
           return "Unknown";        }        String ftime = "";        Calendar cal = Calendar.getInstance();        // 判斷是否是同一天        String curDate = dateFormater2.get().format(cal.getTime());        String paramDate = dateFormater2.get().format(time);        
           if (curDate.equals(paramDate)) {            
           int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);            
           if (hour == 0)                ftime = Math.max(                        (cal.getTimeInMillis() - time.getTime()) / 60000, 1)                        + "分鐘前";                       else                ftime = hour + "小時前";            
            return ftime;        }        long lt = time.getTime() / 86400000;        
            long ct = cal.getTimeInMillis() / 86400000;        
            int days = (int) (ct - lt);        
            if (days == 0) {            
            int hour = (int) ((cal.getTimeInMillis() - time.getTime()) / 3600000);            
            if (hour == 0)                ftime = Math.max(                        (cal.getTimeInMillis() - time.getTime()) / 60000, 1)                        + "分鐘前";          
             else                ftime = hour + "小時前";        } else if (days == 1) {            ftime = "昨天";        } else if (days == 2) {            ftime = "前天 ";        } else if (days > 2 && days < 31) {            ftime = days + "天前";        } else if (days >= 31 && days <= 2 * 31) {            ftime = "一個月前";        } else if (days > 2 * 31 && days <= 3 * 31) {            ftime = "2個月前";        } else if (days > 3 * 31 && days <= 4 * 31) {            ftime = "3個月前";        } else {            ftime = dateFormater2.get().format(time);        }        return ftime;    }

8時間格式轉(zhuǎn)換工具類

public static String ConverToString(Date date) {        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");        
       return df.format(date);    }    
       public static String ConverToString2(Date date){        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");        
       return df.format(date);    }    
       public static String ConverToString3(Date date){        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        
       return df.format(date);    }

9驗證碼倒計時工具類

public class TimeCount extends CountDownTimer {    
   private Button button;    /**     * 到計時     * @param millisInFuture  到計時多久,毫秒     * @param countDownInterval 周期     * @param button   按鈕     */    public TimeCount(long millisInFuture, long countDownInterval,Button button) {        
   super(millisInFuture, countDownInterval);        
   this.button =button;    }    
   public TimeCount(long millisInFuture, long countDownInterval) {        
   super(millisInFuture, countDownInterval);    }    
   @Override    public void onFinish() {// 計時完畢        button.setText("獲取驗證碼");        button.setClickable(true);        button.setBackground(MyApplication.getContext().getResources().getDrawable(R.drawable.radius14));    }  
    @Override    public void onTick(long millisUntilFinished) {// 計時過程        button.setClickable(false);//防止重復(fù)點擊        button.setText(millisUntilFinished / 1000 + "s后重試");        button.setBackground(MyApplication.getContext().getResources().getDrawable(R.drawable.radius_gray));    }}

10屏幕截圖工具類

public class ScreenShot {    
public static void shoot(Activity a, File filePath) {        
   if (filePath == null) {            
       return;        }        
   if (!filePath.getParentFile().exists()) {            filePath.getParentFile().mkdirs();        }        FileOutputStream fos = null;        
   try {            fos = new FileOutputStream(filePath);            
   if (null != fos) {                takeScreenShot(a).compress(Bitmap.CompressFormat.PNG, 100, fos);            }        } catch (FileNotFoundException e) {            e.printStackTrace();        } finally {            
   if (fos != null) {                
   try {                    fos.flush();                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    private static Bitmap takeScreenShot(Activity activity) {        View view = activity.getWindow().getDecorView();        view.setDrawingCacheEnabled(true);        view.buildDrawingCache();        Bitmap bitmap = view.getDrawingCache();        Rect frame = new Rect();        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);        
       int statusBarHeight = frame.top;        
       int width = activity.getWindowManager().getDefaultDisplay().getWidth();        
       int height = activity.getWindowManager().getDefaultDisplay()                .getHeight();        //去掉標題欄        Bitmap b = Bitmap.createBitmap(bitmap, 0, statusBarHeight, width,                height - statusBarHeight);        view.destroyDrawingCache();        return b;    }}
  總結(jié)
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Android開發(fā)網(wǎng)上的一些重要知識點
android游戲開發(fā)入門: 貪吃蛇 源代碼分析
Android放大鏡實現(xiàn)的兩種方式
Android內(nèi)存優(yōu)化
Android內(nèi)存泄漏簡介
Android控件Gallery3D效果
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服