Android常用功能代碼塊,android代碼

來源:互聯網
上載者:User

Android常用功能代碼塊,android代碼

1、設定activity無標題,全屏

// 設定為無標題列 requestWindowFeature(Window.FEATURE_NO_TITLE); // 設定為全螢幕模式 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

2、獲得螢幕高度和寬度

//擷取螢幕的高度和寬度用到WindowManager這個類WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); int width = wm.getDefaultDisplay().getWidth();int height = wm.getDefaultDisplay().getHeight();

3、擷取手機各種資訊

TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);String imei = tm.getDeviceId();//行動裝置國際辨識碼String imsi = tm.getSubscriberId();//國際移動使用者識別碼String tel = tm.getLine1Number();//電話號碼        String model =  android.os.Build.MODEL;//手機型號String sdk = android.os.Build.VERSION.SDK;//SDK版本    String release = android.os.Build.VERSION.RELEASE;//系統版本//根據IMSI號碼識別移動供應商public String getProvidersName(String IMSI) {    String ProvidersName = null;    // IMSI號前面3位460是國家,緊接著後面2位00 02是中國移動,01是中國聯通,03是中國電信。    if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {        ProvidersName = "中國移動";    } else if (IMSI.startsWith("46001")) {        ProvidersName = "中國聯通";    } else if (IMSI.startsWith("46003")) {        ProvidersName = "中國電信";    }    return ProvidersName;}

4、使用Toast輸出一個字串

public void showToast(String text){    Toast.makeText(this, text, Toast.LENGTH_SHORT).show();}

5、把一個字串寫進檔案

//把一個字串寫進檔案public void writeFile(String str,String path){    File file;    FileOutputStream out;    try{        //建立檔案        file = new File(path);        file.createNewFile();        //開啟檔案file的輸出資料流        out = new FileOutputStream(file);        //將字串轉換成byte數組寫入檔案        out.write(str.getBytes());        out.close();    }catch(IOException e){            }}

6、把檔案內容讀出到字串

//把檔案內容讀出到字串public String getFileInfo(String path){    File file;    String str = "";    FileInputStream in;    try{        //開啟檔案的inputStream        file  new File(path);        in = new FileInputStream(file);        //將檔案內容讀入byte數組        int length = (int)file.length();        byte [] temp = new byte[length];        in.read(temp,0,length);        str = EncodingUtils.getString(temp, "utf-8");        in.close();    }catch(IOException e){            }    return str;}

7、程式的安裝,卸載,更新

//調出系統安裝應用String fileName = Environment.getExternalStorageDirectory() + apkName;Uri uri = Uri.fromFile(new File(fileName));Intent intent = new Intent(Intent.ACTION_VIEW);intent.setDataAndType(uri, "application/vnd.android.package-archive");this.startActivity(intent);//調出系統卸載應用Uri packageURI = Uri.parse("package: your.app.id");Intent intent = new Intent(Intent.ACTION_DELETE,packageURI);startActivity(intent);

8、實現點擊兩次返回鍵退出

//第一步,定義一個變數,用於標識是否退出boolean isExit;//第二步,重寫Activity中onKeyDown方法@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {    if (keyCode == KeyEvent.KEYCODE_BACK) {        exit();        return false;    } else {        return super.onKeyDown(keyCode, event);    }}//第三步,寫一個退出方法public void exit(){    if (!isExit) {        isExit = true;        Toast.makeText(getApplicationContext(), "再按一次退出程式", Toast.LENGTH_SHORT).show();        mHandler.sendEmptyMessageDelayed(0, 2000);    } else {        Intent intent = new Intent(Intent.ACTION_MAIN);        intent.addCategory(Intent.CATEGORY_HOME);        startActivity(intent);        System.exit(0);    }}//第四步,根據exit()方法中的的訊息,寫一個HandlerHandler mHandler = new Handler() {    @Override    public void handleMessage(Message msg) {        // TODO Auto-generated method stub        super.handleMessage(msg);        isExit = false;    } };

 


android代碼實現分享功可以

Intent.ACTION_SEND,直接調用這個Intent就會彈出分享選擇對話方塊。
 
Android的一段常用動畫效果代碼(怎讓點擊的圖片控制項加速飛入到指定位置)

首相要new一個這個圖片image對象
然後用TranslateAnimation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta)函數,將現在的的座標和要移到的座標寫進去,然後用animation.setFillAfter(true);讓圖片停留在那。最後image.startAnimation(animation )就可以了,我是看見QQ 2011以前用過這個移動的動畫做了一下,就是這麼實現的
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.