編輯:關於Android編程
最近做的功能中涉及到了一些關於系統方面的東西,自己摸索以及網上搜集整理出來了一個工具類方便調用
包含的功能有:
獲取系統中所有APP應用、獲取用戶安裝的APP應用、根據包名和Activity啟動類查詢應用信息、跳轉到WIFI設置、WIFI網絡開關、移動網絡開關、GPS開關 當前若關則打開 當前若開則關閉、調節系統音量、設置亮度、獲取屏幕的亮度、跳轉到系統設置、獲取文件夾下所有文件、獲取視頻的縮略圖 、打開視頻文件...
工具類會持續更新,與大家共同學習進步。
SystemUtils.java
package com.player.utils; import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.List; import android.app.Activity; import android.app.PendingIntent; import android.app.PendingIntent.CanceledException; import android.content.ComponentName; import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.ResolveInfo; import android.graphics.Bitmap; import android.graphics.drawable.Drawable; import android.media.AudioManager; import android.media.ThumbnailUtils; import android.net.ConnectivityManager; import android.net.Uri; import android.net.wifi.WifiManager; import android.provider.Settings; import com.player.bean.AppInfo; public class SystemUtils { /** * 獲取系統所有APP應用 * * @param context */ public static ArrayList getAllApp(Context context) { PackageManager manager = context.getPackageManager(); Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); Listapps = manager.queryIntentActivities(mainIntent, 0); // 將獲取到的APP的信息按名字進行排序 Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager)); ArrayList appList = new ArrayList(); for (ResolveInfo info : apps) { AppInfo appInfo = new AppInfo(); appInfo.setAppLable(info.loadLabel(manager) + ""); appInfo.setAppIcon(info.loadIcon(manager)); appInfo.setAppPackage(info.activityInfo.packageName); appInfo.setAppClass(info.activityInfo.name); appList.add(appInfo); System.out.println("info.activityInfo.packageName="+info.activityInfo.packageName); System.out.println("info.activityInfo.name="+info.activityInfo.name); } return appList; } /** * 獲取用戶安裝的APP應用 * * @param context */ public static ArrayList getUserApp(Context context) { PackageManager manager = context.getPackageManager(); Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); List apps = manager.queryIntentActivities(mainIntent, 0); // 將獲取到的APP的信息按名字進行排序 Collections.sort(apps, new ResolveInfo.DisplayNameComparator(manager)); ArrayList appList = new ArrayList(); for (ResolveInfo info : apps) { AppInfo appInfo = new AppInfo(); ApplicationInfo ainfo = info.activityInfo.applicationInfo; if ((ainfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) { appInfo.setAppLable(info.loadLabel(manager) + ""); appInfo.setAppIcon(info.loadIcon(manager)); appInfo.setAppPackage(info.activityInfo.packageName); appInfo.setAppClass(info.activityInfo.name); appList.add(appInfo); } } return appList; } /** * 根據包名和Activity啟動類查詢應用信息 * * @param cls * @param pkg * @return */ public static AppInfo getAppByClsPkg(Context context, String pkg, String cls) { AppInfo appInfo = new AppInfo(); PackageManager pm = context.getPackageManager(); Drawable icon; CharSequence label = ""; ComponentName comp = new ComponentName(pkg, cls); try { ActivityInfo info = pm.getActivityInfo(comp, 0); icon = pm.getApplicationIcon(info.applicationInfo); label = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)); } catch (NameNotFoundException e) { icon = pm.getDefaultActivityIcon(); } appInfo.setAppClass(cls); appInfo.setAppIcon(icon); appInfo.setAppLable(label + ""); appInfo.setAppPackage(pkg); return appInfo; } /** * 跳轉到WIFI設置 * * @param context */ public static void intentWifiSetting(Context context) { if (android.os.Build.VERSION.SDK_INT > 10) { // 3.0以上打開設置界面,也可以直接用ACTION_WIRELESS_SETTINGS打開到wifi界面 context.startActivity(new Intent( android.provider.Settings.ACTION_SETTINGS)); } else { context.startActivity(new Intent( android.provider.Settings.ACTION_WIRELESS_SETTINGS)); } } /** * WIFI網絡開關 * */ public static void toggleWiFi(Context context, boolean enabled) { WifiManager wm = (WifiManager) context .getSystemService(Context.WIFI_SERVICE); wm.setWifiEnabled(enabled); } /** * 移動網絡開關 */ public static void toggleMobileData(Context context, boolean enabled) { ConnectivityManager conMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); Class> conMgrClass = null; // ConnectivityManager類 Field iConMgrField = null; // ConnectivityManager類中的字段 Object iConMgr = null; // IConnectivityManager類的引用 Class> iConMgrClass = null; // IConnectivityManager類 Method setMobileDataEnabledMethod = null; // setMobileDataEnabled方法 try { // 取得ConnectivityManager類 conMgrClass = Class.forName(conMgr.getClass().getName()); // 取得ConnectivityManager類中的對象mService iConMgrField = conMgrClass.getDeclaredField("mService"); // 設置mService可訪問 iConMgrField.setAccessible(true); // 取得mService的實例化類IConnectivityManager iConMgr = iConMgrField.get(conMgr); // 取得IConnectivityManager類 iConMgrClass = Class.forName(iConMgr.getClass().getName()); // 取得IConnectivityManager類中的setMobileDataEnabled(boolean)方法 setMobileDataEnabledMethod = iConMgrClass.getDeclaredMethod( "setMobileDataEnabled", Boolean.TYPE); // 設置setMobileDataEnabled方法可訪問 setMobileDataEnabledMethod.setAccessible(true); // 調用setMobileDataEnabled方法 setMobileDataEnabledMethod.invoke(iConMgr, enabled); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } /** * GPS開關 當前若關則打開 當前若開則關閉 */ public static void toggleGPS(Context context) { Intent gpsIntent = new Intent(); gpsIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); gpsIntent.addCategory("android.intent.category.ALTERNATIVE"); gpsIntent.setData(Uri.parse("custom:3")); try { PendingIntent.getBroadcast(context, 0, gpsIntent, 0).send(); } catch (CanceledException e) { e.printStackTrace(); } } /** * 調節系統音量 * * @param context */ public static void holdSystemAudio(Context context) { AudioManager audiomanage = (AudioManager) context .getSystemService(Context.AUDIO_SERVICE); // 獲取系統最大音量 // int maxVolume = audiomanage.getStreamMaxVolume(AudioManager.STREAM_MUSIC); // 獲取當前音量 // int currentVolume = audiomanage.getStreamVolume(AudioManager.STREAM_RING); // 設置音量 // audiomanage.setStreamVolume(AudioManager.STREAM_SYSTEM, currentVolume, AudioManager.FLAG_PLAY_SOUND); // 調節音量 // ADJUST_RAISE 增大音量,與音量鍵功能相同 // ADJUST_LOWER 降低音量 audiomanage.adjustStreamVolume(AudioManager.STREAM_SYSTEM, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI); } /** * 設置亮度(每30遞增) * * @param resolver * @param brightness */ public static void setBrightness(Activity activity) { ContentResolver resolver = activity.getContentResolver(); Uri uri = android.provider.Settings.System .getUriFor("screen_brightness"); int nowScreenBri = getScreenBrightness(activity); nowScreenBri = nowScreenBri <= 225 ? nowScreenBri + 30 : 30; System.out.println("nowScreenBri==" + nowScreenBri); android.provider.Settings.System.putInt(resolver, "screen_brightness", nowScreenBri); resolver.notifyChange(uri, null); } /** * 獲取屏幕的亮度 * * @param activity * @return */ public static int getScreenBrightness(Activity activity) { int nowBrightnessValue = 0; ContentResolver resolver = activity.getContentResolver(); try { nowBrightnessValue = android.provider.Settings.System.getInt( resolver, Settings.System.SCREEN_BRIGHTNESS); } catch (Exception e) { e.printStackTrace(); } return nowBrightnessValue; } /** * 跳轉到系統設置 * * @param context */ public static void intentSetting(Context context) { String pkg = "com.android.settings"; String cls = "com.android.settings.Settings"; ComponentName component = new ComponentName(pkg, cls); Intent intent = new Intent(); intent.setComponent(component); context.startActivity(intent); } /** * 獲取文件夾下所有文件 * @param path * @return */ public static ArrayList getFilesArray(String path){ File file = new File(path); File files[] = file.listFiles(); ArrayList listFile = new ArrayList (); if (files != null) { for (int i = 0; i < files.length; i++) { if (files[i].isFile()) { listFile.add(files[i]); } if (files[i].isDirectory()) { listFile.addAll(getFilesArray(files[i].toString())); } } } return listFile; } /** * 獲取視頻的縮略圖 * 先通過ThumbnailUtils來創建一個視頻的縮略圖,然後再利用ThumbnailUtils來生成指定大小的縮略圖。 * 如果想要的縮略圖的寬和高都小於MICRO_KIND,則類型要使用MICRO_KIND作為kind的值,這樣會節省內存。 * @param videoPath 視頻的路徑 * @param width 指定輸出視頻縮略圖的寬度 * @param height 指定輸出視頻縮略圖的高度度 * @param kind 參照MediaStore.Images.Thumbnails類中的常量MINI_KIND和MICRO_KIND。 * 其中,MINI_KIND: 512 x 384,MICRO_KIND: 96 x 96 * @return 指定大小的視頻縮略圖 */ public static Bitmap getVideoThumbnail(String videoPath, int width, int height, int kind) { Bitmap bitmap = null; // 獲取視頻的縮略圖 bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind); //System.out.println("w"+bitmap.getWidth()); //System.out.println("h"+bitmap.getHeight()); bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); return bitmap; } /** * 打開視頻文件 * @param context * @param file 視頻文件 */ public static void intentVideo(Context context, File file){ Intent intent = new Intent(Intent.ACTION_VIEW); String type = "video/*"; Uri uri = Uri.fromFile(file); intent.setDataAndType(uri, type); context.startActivity(intent); } }
開機清除用戶數據
添加開機廣播
package com.topwise.airlinemanager; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import android.app.ActivityManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.pm.IPackageDataObserver; import android.content.pm.PackageInfo; import android.content.pm.PackageManager; import android.util.Log; /** * 開機廣播 * @author XiaoSai * */ public class CleanDataReceiverd extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { //清除用戶數據 cleanUserData(context); } } class ClearUserDataObserver extends IPackageDataObserver.Stub { public void onRemoveCompleted(final String packageName, final boolean succeeded) { } } private void cleanUserData(Context context) { PackageManager manager = context.getPackageManager(); Listpackages = manager.getInstalledPackages(0); ClearUserDataObserver mClearDataObserver = new ClearUserDataObserver(); ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); Method targetMethod = null; Class> temp = am.getClass(); // String className = temp.getName(); Method[] methods = temp.getMethods(); for (int j = 0; j < methods.length; j++) { if (("clearApplicationUserData".equals(methods[j].getName()))) { targetMethod = methods[j]; break; } } String pkg = null; if (packages != null && packages.size() > 0) { for (int i = 0; i < packages.size(); i++) { pkg = packages.get(i).packageName; if (pkg.equals("com.sina.weibotab") || pkg.equals("com.tencent.android.pad") || pkg.equals("com.tencent.hd.qq") || pkg.equals("com.tencent.WBlog") || pkg.equals("com.tencent.mm")) { try { targetMethod.invoke(am, pkg, mClearDataObserver); Log.i("XIAOS", "clean pgk "+pkg); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } } } } }
AndroidManifest.xml
最後還需要在 Android.mk 中添加 一行
LOCAL_CERTIFICATE := platform
成功源於不斷的學習和積累 !
我們在開發中經常用到倒計時的功能,比如發送驗證碼後,倒計時60s再進行驗證碼的獲取,為了方便以後使用,這裡做個記錄,講講倒計時器的實現。 1、先進行倒計時工具類
正文結構型包含以下類型:適配器 Adapter Class/Object 橋接 Bridge 組合 Composite 裝飾 Decorator 外觀 Facade 享元
最近項目迭代了幾個版本,目前比較輕松,雖然項目閒了,但是人不能太閒,否則就廢了。千裡之行始於足下、量變引起質變、學而不思則罔.......好了好了好了.....編不下去了
1、為什麼要使用屬性動畫?Google在3.0以後推出了屬性動畫,之所以會出屬性動畫,是因為傳統動畫在對象交互方面存在缺陷。可以通過一個很經典的例子來發現屬性動畫和傳統動