Android實用代碼片斷整合

本帖最後由 ℡_莫忘ヽ悇溫╭ 於 2015-3-24 11:49 編輯

一、        精確獲取屏幕尺寸(例如:3.五、4.0、5.0寸屏幕)
  1. public static double getScreenPhysicalSize(Activity ctx) {
  2.         DisplayMetrics dm = new DisplayMetrics();
  3.         ctx.getWindowManager().getDefaultDisplay().getMetrics(dm);
  4.         double diagonalPixels = Math.sqrt(Math.pow(dm.widthPixels, 2) + Math.pow(dm.heightPixels, 2));
  5.         return diagonalPixels / (160 * dm.density);
  6.     }
複製代碼
          通常是7寸以上是平板
二、        判斷是不是平板(官方用法)
  1. public static boolean isTablet(Context context) {
  2.         return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
  3.     }
複製代碼
三、        文字根據狀態更改顏色 android:textColor 
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  2.     <item android:color="#53c1bd" android:state_selected="true"/>
  3.     <item android:color="#53c1bd" android:state_focused="true"/>
  4.     <item android:color="#53c1bd" android:state_pressed="true"/>
  5.     <item android:color="#777777"/>
  6. </selector>
複製代碼
            放在res/color/目錄下
四、背景色根據狀態更改顏色 android:backgroup
  1. <selector xmlns:android="http://schemas.android.com/apk/res/android">

  2.     <item android:state_selected="true"><shape>            <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />
  3.         </shape></item>
  4.     <item android:state_focused="true"><shape>
  5.             <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />
  6.         </shape></item>
  7.     <item android:state_pressed="true"><shape>
  8.             <gradient android:angle="0" android:centerColor="#00a59f" android:endColor="#00a59f" android:startColor="#00a59f" />
  9.         </shape></item>
  10.     <item><shape>
  11.             <gradient android:angle="0" android:centerColor="#00ff00" android:endColor="00ff00" android:startColor="00ff00" />
  12.         </shape></item>

  13. </selector>
複製代碼
            若是直接給背景色color會報錯。
五、        啓動APK的默認Activity
  1. public static void startApkActivity(final Context ctx, String packageName) {
  2.         PackageManager pm = ctx.getPackageManager();
  3.         PackageInfo pi;
  4.         try {
  5.             pi = pm.getPackageInfo(packageName, 0);
  6.             Intent intent = new Intent(Intent.ACTION_MAIN, null);
  7.             intent.addCategory(Intent.CATEGORY_LAUNCHER);
  8.             intent.setPackage(pi.packageName);

  9.             List<ResolveInfo> apps = pm.queryIntentActivities(intent, 0);

  10.             ResolveInfo ri = apps.iterator().next();
  11.             if (ri != null) {
  12.                 String className = ri.activityInfo.name;
  13.                 intent.setComponent(new ComponentName(packageName, className));
  14.                 ctx.startActivity(intent);
  15.             }
  16.         } catch (NameNotFoundException e) {
  17.             Log.e("startActivity", e);
  18.         }
  19.     }
複製代碼
七、計算字寬
  1. public static float GetTextWidth(String text, float Size) {
  2.         TextPaint FontPaint = new TextPaint();
  3.         FontPaint.setTextSize(Size);
  4.         return FontPaint.measureText(text);
  5.     }
複製代碼
             注意若是設置了textStyle,還須要進一步設置TextPaint。 
八、獲取應用程序下全部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.   }
複製代碼
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.     }
複製代碼
10、檢測字符串中只能包含:中文、數字、下劃線(_)、橫線(-)
  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.     }
複製代碼
十一、檢查有沒有應用程序來接受處理你發出的intent
  1. public static boolean isIntentAvailable(Context context, String action) {
  2.         final PackageManager packageManager = context.getPackageManager();
  3.         final Intent intent = new Intent(action);
  4.         List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
  5.         return list.size() > 0;
  6.     }
複製代碼
十二、使用TransitionDrawable實現漸變效果 
  1. private void setImageBitmap(ImageView imageView, Bitmap bitmap) {
  2.         // Use TransitionDrawable to fade in.
  3.         final TransitionDrawable td = new TransitionDrawable(new Drawable[] { new ColorDrawable(android.R.color.transparent), new BitmapDrawable(mContext.getResources(), bitmap) });
  4.         //noinspection deprecation
  5.             imageView.setBackgroundDrawable(imageView.getDrawable());
  6.         imageView.setImageDrawable(td);
  7.         td.startTransition(200);
  8.     }
複製代碼
            比使用AlphaAnimation效果要好,可避免出現閃爍問題。
1三、掃描指定的文件
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
     用途:從本軟件新增、修改、刪除圖片、文件某一個文件(音頻、視頻)須要更新系統媒體庫時使用,沒必要掃描整個SD卡

1四、Dip轉px
  1. public static int dipToPX(final Context ctx, float dip) {
  2.         return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, ctx.getResources().getDisplayMetrics());
  3.     }
複製代碼
       用途:不免在Activity代碼中設置位置、大小等,本方法就頗有用了! 
1五、獲取已經安裝APK的路徑
  1. PackageManager pm = getPackageManager();

  2.         for (ApplicationInfo app : pm.getInstalledApplications(0)) {
  3.              Log.d("PackageList", "package: " + app.packageName + ", sourceDir: " + app.sourceDir);
  4.         }
複製代碼
     輸出以下:
  1. package: com.tmobile.thememanager, sourceDir: /system/app/ThemeManager.apk
  2.         package: com.touchtype.swiftkey, sourceDir: /data/app/com.touchtype.swiftkey-1.apk
複製代碼


1六、 多進程Preferences數據共享
  1. public static void putStringProcess(Context ctx, String key, String value) {
  2.         SharedPreferences sharedPreferences = ctx.getSharedPreferences("preference_mu", Context.MODE_MULTI_PROCESS);
  3.         Editor editor = sharedPreferences.edit();
  4.         editor.putString(key, value);
  5.         editor.commit();
  6.     }

  7.     public static String getStringProcess(Context ctx, String key, String defValue) {
  8.         SharedPreferences sharedPreferences = ctx.getSharedPreferences("preference_mu", Context.MODE_MULTI_PROCESS);
  9.         return sharedPreferences.getString(key, defValue);
  10.     }
複製代碼

             相關文章: php

                        http://zengrong.net/post/1687.htm java


1七、泛型ArrayList轉數組 android

  1. @SuppressWarnings("unchecked")
  2.     public static <T> T[] toArray(Class<?> cls, ArrayList<T> items) {
  3.         if (items == null || items.size() == 0) {
  4.             return (T[]) Array.newInstance(cls, 0);
  5.         }
  6.         return items.toArray((T[]) Array.newInstance(cls, items.size()));
  7.     }
複製代碼

1八、保存恢復ListView當前位置 數據庫

  1. private void saveCurrentPosition() {
  2.         if (mListView != null) {
  3.             int position = mListView.getFirstVisiblePosition();
  4.             View v = mListView.getChildAt(0);
  5.             int top = (v == null) ? 0 : v.getTop();
  6.             //保存position和top
  7.         }
  8.     }
  9.     
  10.     private void restorePosition() {
  11.         if (mFolder != null && mListView != null) {
  12.             int position = 0;//取出保存的數據
  13.             int top = 0;//取出保存的數據
  14.             mListView.setSelectionFromTop(position, top);
  15.         }
  16.     }
複製代碼

             能夠保存在Preference中或者是數據庫中,數據加載完後再設置。 swift


1九、調用 便攜式熱點和數據共享 設置 數組

  1. public static Intent getHotspotSetting() {
  2.         Intent intent = new Intent();
  3.         intent.setAction(Intent.ACTION_MAIN);
  4.         ComponentName com = new ComponentName("com.android.settings", "com.android.settings.TetherSettings");
  5.         intent.setComponent(com);
  6.         return intent;
  7.     }
複製代碼

20、格式化輸出IP地址 網絡

  1. public static String getIp(Context ctx) {
  2.         return Formatter.formatIpAddress((WifiManager) ctx.getSystemService(Context.WIFI_SERVICE).getConnectionInfo().getIpAddress());
  3.     }
複製代碼

2一、文件夾排序(先文件夾排序,後文件排序) app

  1. public static void sortFiles(File[] files) {
  2.         Arrays.sort(files, new Comparator<File>() {

  3.             @Override
  4.             public int compare(File lhs, File rhs) {
  5.                 //返回負數表示o1 小於o2,返回0 表示o1和o2相等,返回正數表示o1大於o2。 
  6.                 boolean l1 = lhs.isDirectory();
  7.                 boolean l2 = rhs.isDirectory();
  8.                 if (l1 && !l2)
  9.                     return -1;
  10.                 else if (!l1 && l2)
  11.                     return 1;
  12.                 else {
  13.                     return lhs.getName().compareTo(rhs.getName());
  14.                 }
  15.             }
  16.         });
  17.     }
複製代碼

2二、發送不重複的通知(Notification) ide

  1. public static void sendNotification(Context context, String title,
  2.             String message, Bundle extras) {
  3.         Intent mIntent = new Intent(context, FragmentTabsActivity.class);
  4.         mIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  5.         mIntent.putExtras(extras);

  6.         int requestCode = (int) System.currentTimeMillis();

  7.         PendingIntent mContentIntent = PendingIntent.getActivity(context,
  8.                 requestCode, mIntent, 0);

  9.         Notification mNotification = new NotificationCompat.Builder(context)
  10.                 .setContentTitle(title).setSmallIcon(R.drawable.app_icon)
  11.                 .setContentIntent(mContentIntent).setContentText(message)
  12.                 .build();
  13.         mNotification.flags |= Notification.FLAG_AUTO_CANCEL;
  14.         mNotification.defaults = Notification.DEFAULT_ALL;

  15.         NotificationManager mNotificationManager = (NotificationManager) context
  16.                 .getSystemService(Context.NOTIFICATION_SERVICE);

  17.         mNotificationManager.notify(requestCode, mNotification);
  18.     }
複製代碼

              關鍵點在這個requestCode,這裏使用的是當前系統時間,巧妙的保證了每次都是一個新的Notification產生。 post


2三、代碼設置TextView的樣式

                使用過自定義Dialog可能立刻會想到用以下代碼:

                new TextView(this,null,R.style.text_style);

                但你運行這代碼你會發現毫無做用!正確用法:

        new TextView(new ContextThemeWrapper(this, R.style.text_style))

2四、ip地址轉成8位十六進制串

  1. /** ip轉16進制 */
  2.     public static String ipToHex(String ips) {
  3.         StringBuffer result = new StringBuffer();
  4.         if (ips != null) {
  5.             StringTokenizer st = new StringTokenizer(ips, ".");
  6.             while (st.hasMoreTokens()) {
  7.                 String token = Integer.toHexString(Integer.parseInt(st.nextToken()));
  8.                 if (token.length() == 1)
  9.                     token = "0" + token;
  10.                 result.append(token);
  11.             }
  12.         }
  13.         return result.toString();
  14.     }

  15.     /** 16進制轉ip */
  16.     public static String texToIp(String ips) {
  17.         try {
  18.             StringBuffer result = new StringBuffer();
  19.             if (ips != null && ips.length() == 8) {
  20.                 for (int i = 0; i < 8; i += 2) {
  21.                     if (i != 0)
  22.                         result.append('.');
  23.                     result.append(Integer.parseInt(ips.substring(i, i + 2), 16));
  24.                 }
  25.             }
  26.             return result.toString();
  27.         } catch (NumberFormatException ex) {
  28.             Logger.e(ex);
  29.         }
  30.         return "";
  31.     }
複製代碼

             ip:192.168.68.128 16 =>hex :c0a84480


2五、WebView保留縮放功能但隱藏縮放控件

  1. mWebView.getSettings().setSupportZoom(true);
  2.         mWebView.getSettings().setBuiltInZoomControls(true);
  3.         if (DeviceUtils.hasHoneycomb())
  4.               mWebView.getSettings().setDisplayZoomControls(false);
複製代碼

             注意:setDisplayZoomControls是在API Level 11中新增。


2六、獲取網絡類型名稱

  1. public static String getNetworkTypeName(Context context) {
  2.         if (context != null) {
  3.             ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  4.             if (connectMgr != null) {
  5.                 NetworkInfo info = connectMgr.getActiveNetworkInfo();
  6.                 if (info != null) {
  7.                     switch (info.getType()) {
  8.                     case ConnectivityManager.TYPE_WIFI:
  9.                         return "WIFI";
  10.                     case ConnectivityManager.TYPE_MOBILE:
  11.                         return getNetworkTypeName(info.getSubtype());
  12.                     }
  13.                 }
  14.             }
  15.         }
  16.         return getNetworkTypeName(TelephonyManager.NETWORK_TYPE_UNKNOWN);
  17.     }

  18.     public static String getNetworkTypeName(int type) {
  19.         switch (type) {
  20.         case TelephonyManager.NETWORK_TYPE_GPRS:
  21.             return "GPRS";
  22.         case TelephonyManager.NETWORK_TYPE_EDGE:
  23.             return "EDGE";
  24.         case TelephonyManager.NETWORK_TYPE_UMTS:
  25.             return "UMTS";
  26.         case TelephonyManager.NETWORK_TYPE_HSDPA:
  27.             return "HSDPA";
  28.         case TelephonyManager.NETWORK_TYPE_HSUPA:
  29.             return "HSUPA";
  30.         case TelephonyManager.NETWORK_TYPE_HSPA:
  31.             return "HSPA";
  32.         case TelephonyManager.NETWORK_TYPE_CDMA:
  33.             return "CDMA";
  34.         case TelephonyManager.NETWORK_TYPE_EVDO_0:
  35.             return "CDMA - EvDo rev. 0";
  36.         case TelephonyManager.NETWORK_TYPE_EVDO_A:
  37.             return "CDMA - EvDo rev. A";
  38.         case TelephonyManager.NETWORK_TYPE_EVDO_B:
  39.             return "CDMA - EvDo rev. B";
  40.         case TelephonyManager.NETWORK_TYPE_1xRTT:
  41.             return "CDMA - 1xRTT";
  42.         case TelephonyManager.NETWORK_TYPE_LTE:
  43.             return "LTE";
  44.         case TelephonyManager.NETWORK_TYPE_EHRPD:
  45.             return "CDMA - eHRPD";
  46.         case TelephonyManager.NETWORK_TYPE_IDEN:
  47.             return "iDEN";
  48.         case TelephonyManager.NETWORK_TYPE_HSPAP:
  49.             return "HSPA+";
  50.         default:
  51.             return "UNKNOWN";
  52.         }
  53.     }
複製代碼

2七、Android解壓Zip包

  1. /**
  2.      * 解壓一個壓縮文檔 到指定位置
  3.      * 
  4.      * @param zipFileString 壓縮包的名字
  5.      * @param outPathString 指定的路徑
  6.      * [url=home.php?mod=space&uid=2643633]@throws[/url] Exception
  7.      */
  8.     public static void UnZipFolder(String zipFileString, String outPathString) throws Exception {
  9.         java.util.zip.ZipInputStream inZip = new java.util.zip.ZipInputStream(new java.io.FileInputStream(zipFileString));
  10.         java.util.zip.ZipEntry zipEntry;
  11.         String szName = "";

  12.         while ((zipEntry = inZip.getNextEntry()) != null) {
  13.             szName = zipEntry.getName();

  14.             if (zipEntry.isDirectory()) {

  15.                 // get the folder name of the widget
  16.                 szName = szName.substring(0, szName.length() - 1);
  17.                 java.io.File folder = new java.io.File(outPathString + java.io.File.separator + szName);
  18.                 folder.mkdirs();

  19.             } else {

  20.                 java.io.File file = new java.io.File(outPathString + java.io.File.separator + szName);
  21.                 file.createNewFile();
  22.                 // get the output stream of the file
  23.                 java.io.FileOutputStream out = new java.io.FileOutputStream(file);
  24.                 int len;
  25.                 byte[] buffer = new byte[1024];
  26.                 // read (len) bytes into buffer
  27.                 while ((len = inZip.read(buffer)) != -1) {
  28.                     // write (len) byte from buffer at the position 0
  29.                     out.write(buffer, 0, len);
  30.                     out.flush();
  31.                 }
  32.                 out.close();
  33.             }
  34.         }//end of while

  35.         inZip.close();

  36.     }//end of func
複製代碼

2八、從assets中讀取文本和圖片資源

  1. /** 從assets 文件夾中讀取文本數據 */
  2.     public static String getTextFromAssets(final Context context, String fileName) {
  3.         String result = "";
  4.         try {
  5.             InputStream in = context.getResources().getAssets().open(fileName);
  6.             // 獲取文件的字節數
  7.             int lenght = in.available();
  8.             // 建立byte數組
  9.             byte[] buffer = new byte[lenght];
  10.             // 將文件中的數據讀到byte數組中
  11.             in.read(buffer);
  12.             result = EncodingUtils.getString(buffer, "UTF-8");
  13.             in.close();
  14.         } catch (Exception e) {
  15.             e.printStackTrace();
  16.         }
  17.         return result;
  18.     }
  19.     
  20.     /** 從assets 文件夾中讀取圖片 */
  21.     public static Drawable loadImageFromAsserts(final Context ctx, String fileName) {
  22.         try {
  23.             InputStream is = ctx.getResources().getAssets().open(fileName);
  24.             return Drawable.createFromStream(is, null);
  25.         } catch (IOException e) {
  26.             if (e != null) {
  27.                 e.printStackTrace();
  28.             }
  29.         } catch (OutOfMemoryError e) {
  30.             if (e != null) {
  31.                 e.printStackTrace();
  32.             }
  33.         } catch (Exception e) {
  34.             if (e != null) {
  35.                 e.printStackTrace();
  36.             }
  37.         }
  38.         return null;
  39.     }
複製代碼

2九、展開、收起狀態欄

  1. public static final void collapseStatusBar(Context ctx) {
  2.         Object sbservice = ctx.getSystemService("statusbar");
  3.         try {
  4.             Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
  5.             Method collapse;
  6.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
  7.                 collapse = statusBarManager.getMethod("collapsePanels");
  8.             } else {
  9.                 collapse = statusBarManager.getMethod("collapse");
  10.             }
  11.             collapse.invoke(sbservice);
  12.         } catch (Exception e) {
  13.             e.printStackTrace();
  14.         }
  15.     }

  16.     public static final void expandStatusBar(Context ctx) {
  17.         Object sbservice = ctx.getSystemService("statusbar");
  18.         try {
  19.             Class<?> statusBarManager = Class.forName("android.app.StatusBarManager");
  20.             Method expand;
  21.             if (Build.VERSION.SDK_INT >= 17) {
  22.                 expand = statusBarManager.getMethod("expandNotificationsPanel");
  23.             } else {
  24.                 expand = statusBarManager.getMethod("expand");
  25.             }
  26.             expand.invoke(sbservice);
  27.         } catch (Exception e) {
  28.             e.printStackTrace();
  29.         }
  30.     }
複製代碼

              用途:可用於點擊Notifacation以後收起狀態欄


30、獲取狀態欄高度

  1. public static int getStatusBarHeight(Context context){
  2.         Class<?> c = null;
  3.         Object obj = null;
  4.         Field field = null;
  5.         int x = 0, statusBarHeight = 0;
  6.         try {
  7.             c = Class.forName("com.android.internal.R$dimen");
  8.             obj = c.newInstance();
  9.             field = c.getField("status_bar_height");
  10.             x = Integer.parseInt(field.get(obj).toString());
  11.             statusBarHeight = context.getResources().getDimensionPixelSize(x);
  12.         } catch (Exception e1) {
  13.             e1.printStackTrace();
  14.         }
  15.         return statusBarHeight;
  16.     }
複製代碼

3一、ListView使用ViewHolder極簡寫法

  1. public static <T extends View> T getAdapterView(View convertView, int id) {
  2.         SparseArray<View> viewHolder = (SparseArray<View>) convertView.getTag();
  3.         if (viewHolder == null) {
  4.             viewHolder = new SparseArray<View>();
  5.             convertView.setTag(viewHolder);
  6.         }
  7.         View childView = viewHolder.get(id);
  8.         if (childView == null) {
  9.             childView = convertView.findViewById(id);
  10.             viewHolder.put(id, childView);
  11.         }
  12.         return (T) childView;
  13.     }
複製代碼

      用法:

  1. @Override
  2.     public View getView(int position, View convertView, ViewGroup parent) {
  3.         if (convertView == null) {
  4.             convertView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_feed_item, parent, false);
  5.         }

  6.         ImageView thumnailView = getAdapterView(convertView, R.id.video_thumbnail);
  7.         ImageView avatarView =  getAdapterView(convertView, R.id.user_avatar);
  8.         ImageView appIconView = getAdapterView(convertView, R.id.app_icon);
複製代碼

               用起來很是簡練,將ViewHolder隱於無形。


3二、設置Activity透明

  1. <style name="TransparentActivity" parent="AppBaseTheme">
  2.         <item name="android:windowBackground">@android:color/transparent</item>
  3.         <item name="android:colorBackgroundCacheHint">@null</item>
  4.         <item name="android:windowIsTranslucent">true</item>
  5.         <item name="android:windowNoTitle">true</item>
  6.         <item name="android:windowContentOverlay">@null</item>
  7.     </style>
複製代碼

           說明:AppBaseTheme通常是你application指定的android:theme是啥這裏就是啥,不然Activity內部的空間風格可能不一致。

           用途:用於模擬Dialog效果,好比再Service中無法用Dialog,就能夠用Activity來模擬

3三、代碼切換全屏

  1. //切換到全屏
  2.         getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
  3.         getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

  4.         //切換到非全屏
  5.         getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
  6.         getWindow().addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
複製代碼

            注意:切換到全屏時,底部的虛擬按鍵仍然是顯示的。次方法可屢次調用用於切換

            用途:播放器界面常常會用到


3四、調用開發者選項中顯示觸摸位置功能

android.provider.Settings.System.putInt(getContentResolver(), "show_touches", 1);

             設置1顯示,設置0不顯示。


3五、獲取設備上已安裝而且可啓動的應用列表

  1. Intent intent = new Intent(Intent.ACTION_MAIN);
  2.             intent.addCategory(Intent.CATEGORY_LAUNCHER);

  3.             List<ResolveInfo> activities = getPackageManager().queryIntentActivities(intent, 0)
複製代碼

            注意:使用getInstalledApplications會返回不少沒法啓動甚至沒有圖標的系統應用。ResolveInfo.activityInfo.applicationInfo也能取到你                     想要的數據。

相關文章
相關標籤/搜索