零碎知識點

轉載:請標註:http://bbs.itheima.com/thread-46102-1-1.html

一、  獲取系統版本號:
  1. PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0); 

  2. int versionCode=nfo.versionCode 

  3. string versionName=info.versionNam
複製代碼
二、獲取系統信息:
  1. String archiveFilePath="sdcard/download/Law.apk";//安裝包路徑  

  2. PackageManager pm = getPackageManager();  

  3. PackageInfo info = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES);  

  4. if(info != null){  

  5. ApplicationInfo appInfo = info.applicationInfo;  

  6. String appName = pm.getApplicationLabel(appInfo).toString();  

  7. String packageName = appInfo.packageName; //得到安裝包名稱  

  8. String version=info.versionName; //得到版本信息   

  9. Toast.makeText(test4.this, "packageName:"+packageName+";version:"+version, Toast.LENGTH_LONG).show(); 

  10. Drawable icon = pm.getApplicationIcon(appInfo);//得到圖標信息  

  11. TextView tv = (TextView)findViewById(R.id.tv); //顯示圖標  

  12. tv.setBackgroundDrawable(icon);
複製代碼
三、獲取安裝路徑和已安裝程序列表
  1. (1)android中獲取當前程序路徑 

  2. getApplicationContext().getFilesDir().getAbsolutePath() 

  3. (2)android取已安裝的程序列表 

  4. List<PackageInfo> packageInfoList = getPackageManager().getInstalledPackages(0);
複製代碼
四、獲取圖片、應用名、包名
  1. PackageManager pManager = MessageSendActivity.this.getPackageManager();  

  2. List<PackageInfo> appList = Utils.getAllApps(MessageSendActivity.this);  

  3.      for(int i=0;i<appList.size();i++) {  

  4.          PackageInfo pinfo = appList.get(i);  

  5.          ShareItemInfo shareItem = new ShareItemInfo();  

  6.          //set Icon   

  7.          shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo));
複製代碼
解決listview上 Item上有按鈕時 item本身不能點擊的問題:
  1. 1. 在item試圖上面添加代碼: android:descendantFocusability="blocksDescendants" 

  2. 2.在listview裏 添加代碼 android:focusable="true"
複製代碼
不讓文本框輸入中文:
  1. android:digits="1234567890qwertyuiopasdfghjklzxcvbnm`-=[]\;,./~!@#$%^*()_+}{:?&<>"'" 

  2. 這樣就不會輸入中文了。 
複製代碼
獲取屏幕寬高
  1. DisplayMetrics displayMetrics = new DisplayMetrics();  

  2. this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);  

  3. int height = displayMetrics.heightPixels;  

  4. int width = displayMetrics.widthPixels;
複製代碼
程序中安裝apk
  1. Intent intent = new Intent();            

  2.        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

  3.     intent.setAction(android.content.Intent.ACTION_VIEW); 

  4.     intent.setDataAndType(Uri.fromFile(“APK”),"application/vnd.android.package-archive"); 

  5.     startActivity(intent);
複製代碼
獲取設備型號、SDK版本及系統版本
  1. String device_model = Build.MODEL; // 設備型號    

  2. String version_sdk = Build.VERSION.SDK; // 設備SDK版本    

  3. String version_release = Build.VERSION.RELEASE; // 設備的系統版本
複製代碼
當listview滑動到底部或者頂部,會出現金色動畫,去掉的辦法
  1. listView.setOverScrollMode(View.OVER_SCROLL_NEVER);
複製代碼
獲取應用程序下所有Activity
  1. public static ArrayList<String> getActivities(Context ctx) {

  2.      ArrayList<String> result = new ArrayList<String>();

  3.      Intent intent = new Intent(Intent.ACTION_MAIN, null);

  4.      intent.setPackage(ctx.getPackageName());

  5. for (ResolveInfo info : ctx.getPackageManager().queryIntentActivities(intent, 0)) {

  6.      result.add(info.activityInfo.name);

  7.      }

  8.      return result;

  9. }
複製代碼
檢測字符串中是否包含漢字
  1. public static boolean checkChinese(String sequence) {

  2.        final String format = "[\\u4E00-\\u9FA5\\uF900-\\uFA2D]";

  3.        boolean result = false;

  4.        Pattern pattern = Pattern.compile(format);

  5.        Matcher matcher = pattern.matcher(sequence);

  6.        result = matcher.find();

  7.        return result;

  8.    }
複製代碼
檢測字符串中只能包含:中文、數字、下劃線(_)、橫線(-)
  1. public static boolean checkNickname(String sequence) {

  2.        final String format = "[^\\u4E00-\\u9FA5\\uF900-\\uFA2D\\w-_]";

  3.        Pattern pattern = Pattern.compile(format);

  4.        Matcher matcher = pattern.matcher(sequence);

  5.        return !matcher.find();

  6.    }
複製代碼



設置窗口格式爲半透明
  1. getWindow().setFormat(PixelFormat.TRANSLUCENT);
複製代碼
全屏顯示窗口
  1. requestWindowFeature(Window.FEATURE_NO_TITLE);
  2. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
複製代碼
調瀏覽器 載入網址
  1. Uri uri = Uri.parse("http://www.google.com");
  2. Intent it = new Intent(Intent.ACTION_VIEW, uri);
  3. startActivity(it);
複製代碼
取得內存大小
  1. ActivityManager.MemoryInfo outInfo = new ActivityManager.MemoryInfo();
  2. activityManager.getMemoryInfo(outInfo);
  3. //可用內存
  4. outInfo.availMem
  5. //是否在低內存狀態
  6. outInfo.lowMemory
複製代碼
取得ScrollView的實際高度
  1. scrollview.getHeight()
  2. scrollview.getMeasuredHeight()
  3. scrollview.compute()
  4. scrollview.getLayoutParams().height
複製代碼
取得IP地址
  1. A.
  2. //Connect via WIFI 通過wifi
  3. WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
  4. WifiInfo wifiInfo = wifiManager.getConnectionInfo();
  5. int ipAddress = wifiInfo.getIpAddress();
  6. B.
  7. //Connect via GPRS通過gprs
  8. public String getLocalIpAddress(){
  9. try{
  10. for(Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();){
  11. NetworkInterface intf = en.nextElement();
  12. for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();){
  13. InetAddress inetAddress = enumIpAddr.nextElement();
  14. if (!inetAddress.isLoopbackAddress()){
  15. return inetAddress.getHostAddress().toString();
  16. }
  17. }
  18. }
  19. }catch (SocketException ex){
  20. Log.e(S.TAG, ex.toString());
  21. }
  22. return null;
  23. }
複製代碼
圖片旋轉
  1. Bitmap bitmapOrg = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.moon);
  2. Matrix matrix = new Matrix();
  3. matrix.postRotate(-90);//旋轉的角度

  4. Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
  5.                     bitmapOrg.getWidth(), bitmapOrg.getHeight(), matrix, true);
  6. BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
複製代碼
獲取手機號碼
  1. //創建電話管理

  2. TelephonyManager tm = (TelephonyManager)

  3. //與手機建立連接
  4. activity.getSystemService(Context.TELEPHONY_SERVICE);

  5. //獲取手機號碼

  6. String phoneId = tm.getLine1Number();

  7. //記得在manifest file中添加
  8.     <uses-permission
  9. android:name="android.permission.READ_PHONE_STATE" />

  10. //程序在模擬器上無法實現,必須連接手機
複製代碼
查看電池使用情況
  1. Intent intentBatteryUsage = new Intent(Intent.ACTION_POWER_USAGE_SUMMARY);        
  2. startActivity(intentBatteryUsage);
複製代碼
android中添加新的聯繫人
  1. private Uri insertContact(Context context, String name, String phone) {
  2.            
  3.        ContentValues values = new ContentValues();
  4.        values.put(People.NAME, name);
  5.        Uri uri = getContentResolver().insert(People.CONTENT_URI, values);
  6.        Uri numberUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
  7.        values.clear();
  8.        
  9.        values.put(Contacts.Phones.TYPE, People.Phones.TYPE_MOBILE);
  10.        values.put(People.NUMBER, phone);
  11.        getContentResolver().insert(numberUri, values);
  12.        
  13.        return uri;
  14. }
複製代碼
android獲取存儲卡路徑以及使用情況
  1. /** 獲取存儲卡路徑 */ 
  2. File sdcardDir=Environment.getExternalStorageDirectory(); 
  3. /** StatFs 看文件系統空間使用情況 */ 
  4. StatFs statFs=new StatFs(sdcardDir.getPath()); 
  5. /** Block 的 size*/ 
  6. Long blockSize=statFs.getBlockSize(); 
  7. /** 總 Block 數量 */ 
  8. Long totalBlocks=statFs.getBlockCount(); 
  9. /** 已使用的 Block 數量 */ 
  10. Long availableBlocks=statFs.getAvailableBlocks();
複製代碼
android中獲取IP地址
  1. public String getLocalIpAddress() {
  2.     try {
  3.         for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); 
  4.                 en.hasMoreElements();) {
  5.             NetworkInterface intf = en.nextElement();
  6.             for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); 
  7.                 enumIpAddr.hasMoreElements();) {
  8.                 InetAddress inetAddress = enumIpAddr.nextElement();
  9.                 if (!inetAddress.isLoopbackAddress()) {
  10.                     return inetAddress.getHostAddress().toString();
  11.                 }
  12.             }
  13.         }
  14.     } catch (SocketException ex) {
  15.         Log.e(LOG_TAG, ex.toString());
  16.     }
  17.     return null;
  18. }
複製代碼
android 獲取設備唯一標識碼
  1. String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);
複製代碼

發佈了51 篇原創文章 · 獲贊 35 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章