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方法 @Override public 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()方法中的的消息,寫一個Handler Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub super.handleMessage(msg); isExit = false; } };