常用的方法、知識(一)

1、獲取當前時間(包括年、月、日、時、分、秒):

SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日  HH:mm:ss");
Date curDate = new Date(System.currentTimeMillis());
String str = formatter.format(curDate);

2、獲取本地版本號:

public int getlocalVersion(){
int localversion = 0;
try {
        PackageInfo info = getPackageManager().getPackageInfo(getPackageName(), 0);
//如果是要獲取versionName,就直接把versionCode換成versionName
localversion = info.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
    }
return localversion;
}

3、判斷是否有網絡連接:

//基類
public static boolean isNetworkAvailable(Context context) {
    ConnectivityManager connectivity = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
        NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
// 當前網絡是連接的
if (info.getState() == NetworkInfo.State.CONNECTED) {
// 當前所連接的網絡可用
return true;
            }
        }
    }
return false;
}
//調用
private void isnettest() {
boolean isnet = BaseActivity.isNetworkAvailable(Home.this);
if (isnet == false) {
        Toast.makeText(Home.this, "請檢查你的網絡連接", Toast.LENGTH_SHORT).show();
    } else {
        Thread weatherThread = new Thread(new WeatherThread());
        weatherThread.start();
    }
}

4、截屏、調用系統分享圖片、文字

ScreenShot screenShot = new ScreenShot();
Bmp = screenShot.GetandSaveCurrentImage(Report.this);
String str = getSDCardPath()+"/PrintScreenDemo/ScreenImage/Screen_0.png";
Intent shareIntent = new Intent(Intent.ACTION_SEND);
File file = new File(str);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent, "title"));
private String getSDCardPath(){
    File sdcardDir = null;
    //判斷SDCard是否存在
    boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
    if(sdcardExist){
        sdcardDir = Environment.getExternalStorageDirectory();
    }
    return sdcardDir.toString();
}

5、正則判斷,是否爲手機登錄:

//方法
public static boolean isMobileNO(String mobiles) {
/*
移動:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188
聯通:130、131、132、152、155、156、185、186
電信:133、153、180、189、(1349衛通)
總結起來就是第一位必定爲1,第二位必定爲3或5或8,其他位置的可以爲0-9
*/
String telRegex = "[1][358]\\d{9}";//"[1]"代表第1位爲數字1,"[358]"代表第二位可以爲3、5、8中的一個,"\\d{9}"代表後面是可以是0~9的數字,有9位。
if (TextUtils.isEmpty(mobiles)) return false;
else return mobiles.matches(telRegex);
}
//調用
if(!isMobileNO(username)){
Toast.makeText(SetUser.this, "請用手機號碼註冊", Toast.LENGTH_LONG).show();
}

6、切換Activity的時候,加載動畫:

case R.id.login_base_rl_back:
    startActivity( new Intent(this, Forget.class));
    finish();
    overridePendingTransition(R.anim.push_right_in,
            R.anim.push_right_out);
break;

給整個APP修改切換動畫,在style裏面寫,然後在清單裏面修改主題:

<!-- Base application theme. -->
<style name="AppTheme1" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->

    <!-- 設置沒有標題 -->
<item name="android:windowNoTitle">true</item>
<!-- 設置activity切換動畫 -->
<item name="android:windowAnimationStyle">@style/activityAnimation</item>
</style>
<!-- animation 樣式 -->
<style name="activityAnimation" parent="@android:style/Animation">
    <item name="android:activityOpenEnterAnimation">@anim/slide_right_in</item>
    <item name="android:activityOpenExitAnimation">@anim/slide_left_out</item>
    <item name="android:activityCloseEnterAnimation">@anim/slide_left_in</item>
    <item name="android:activityCloseExitAnimation">@anim/slide_right_out</item>
</style>

7、定時任務,半個小時請求一次服務器,返回數據:

handler.postDelayed(runnable, TIME); //每隔半小時執行
Runnable runnable = new Runnable() {
@Override
public void run() {
// handler自帶方法實現定時器
try {
handler.postDelayed(this, TIME);
            Thread weatherThread = new Thread(new WeatherThread());
            weatherThread.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};
發佈了61 篇原創文章 · 獲贊 72 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章