Recent features involved some of the system aspects of their own groping and online collection of a tool class to facilitate the call
The included features are:
Get all app apps in the system, get user-installed Apps, query app information based on package name and activity launch class, jump to WiFi settings, wifi network switch, mobile network switch, GPS switch current if off turn on current if off, adjust system volume, set brightness, Get the brightness of the screen, jump to system settings, get all the files under the folder, get thumbnails of videos, open video files ...
Tools will continue to update, with everyone to learn progress together.
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 {/** * get system all apps * * @param context */public static arraylist<appinfo> Getallapp (Context context) {Packagemanager manager = Context.getpackagemanager (); Intent mainintent = new Intent (intent.action_main, NULL); Mainintent.addcategory (Intent.category_launcher); List<resolveinfo> apps = manager.queryintentactivities (mainintent, 0);//Sorts the information obtained into the app by name Collections.sort ( Apps, new Resolveinfo.displaynamecomparator (manager)); arraylist<appinfo> applist = new arraylist<appinfo> (); for (ResolveInfo Info:apps) {AppInfo AppInfo = new APP Info (); 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;} /** * Get user-installed apps shouldwith * * @param context */public static arraylist<appinfo> Getuserapp (context context) {Packagemanager manager = Conte Xt.getpackagemanager (); Intent mainintent = new Intent (intent.action_main, null); Mainintent.addcategory ( Intent.category_launcher); List<resolveinfo> apps = manager.queryintentactivities (mainintent, 0);//Sorts the information obtained into the app by name Collections.sort ( Apps, new Resolveinfo.displaynamecomparator (manager)); arraylist<appinfo> applist = new arraylist<appinfo> (); for (ResolveInfo Info:apps) {AppInfo AppInfo = new APP Info (); ApplicationInfo Ainfo = Info.activityinfo.applicationinfo;if ((ainfo.flags & Applicationinfo.flag_system) <= 0) {appinfo.setapplable (manager) + ""); Appinfo.setappicon (Info.loadicon (manager)); Appinfo.setapppackage (Info.activityInfo.packageName); Appinfo.setappclass (info.activityInfo.name); Applist.add ( AppInfo);}} return applist;} /** * Based on package name and Activity start class query application information * * @param CLS * @param pkg * @return */public static AppInfo getappbyclsPKG (context context, string pkg, string cls) {AppInfo AppInfo = new AppInfo (); Packagemanager pm = Context.getpackagemanager ();D rawable icon; Charsequence label = ""; ComponentName comp = new ComponentName (pkg, CLS); try {activityinfo info = pm.getactivityinfo (comp, 0); icon = Pm.getapplica Tionicon (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;} /** * Jump to WiFi settings * * @param context */public static void intentwifisetting (context context) {if (Android.os.Build.VERSION. Sdk_int > 10) {//3.0 + Open the Settings screen, or you can open it directly with action_wireless_settings to the WiFi interface context.startactivity (New Intent ( Android.provider.Settings.ACTION_SETTINGS));} else {context.startactivity (new Intent (Android.provider.Settings.ACTION_WIRELESS_SETTINGS));}} /** * WiFi network switch * */public static void Togglewifi (ContextContext, Boolean enabled) {Wifimanager wm = (Wifimanager) context.getsystemservice (Context.wifi_service); wm.setwifienabled (enabled);} /** * Mobile network switch */public static void Togglemobiledata (context context, Boolean enabled) {Connectivitymanager Conmgr = (connec Tivitymanager) Context.getsystemservice (Context.connectivity_service); Class<?> conmgrclass = null; Connectivitymanager class field iconmgrfield = null; field in the Connectivitymanager class object iconmgr = null; The reference to the Iconnectivitymanager class class<?> iconmgrclass = null; Iconnectivitymanager class method setmobiledataenabledmethod = null; Setmobiledataenabled method try {//get Connectivitymanager class Conmgrclass = Class.forName (Conmgr.getclass (). GetName ());// Gets the object in the Connectivitymanager class Mserviceiconmgrfield = Conmgrclass.getdeclaredfield ("Mservice");// Set Mservice to access iconmgrfield.setaccessible (TRUE);//Get mservice instantiation class Iconnectivitymanagericonmgr = Iconmgrfield.get ( CONMGR);//Get Iconnectivitymanager class Iconmgrclass = Class.forName (Iconmgr.getclass (). GetName ());//Get The Setmobiledataenabled (Boolean) method in the Iconnectivitymanager class Setmobiledataenabledmethod = Iconmgrclass.getdeclaredmethod ("setmobiledataenabled", boolean.type);// Set the Setmobiledataenabled method to access setmobiledataenabledmethod.setaccessible (TRUE);// Call the Setmobiledataenabled method 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 Switch Current if off open the current if open */public static void Togglegps (context context) {Intent gpsintent = new Intent (); gpsintent.set ClassName ("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 ();}} /** * Adjust System Volume * * @param context */public static void Holdsystemaudio (context context) {Audiomanager Audiomanage = (audiom Anager) Context.getsystemservice (context.audio_service)//Get system maximum volume//int maxvolume = Audiomanage.getstreammaxvolume (Audiomanager.stream_music); Get current volume//int currentvolume = Audiomanage.getstreamvolume (audiomanager.stream_ring); Set volume//Audiomanage.setstreamvolume (Audiomanager.stream_system, Currentvolume, audiomanager.flag_play_sound);// Adjust volume//adjust_raise increase volume, same as volume key function//adjust_lower lower volume audiomanage.adjuststreamvolume (audiomanager.stream_system, Audiomanager.adjust_raise, audiomanager.flag_show_ui);} /** * Set Brightness (increment per 30) * * @param resolver * @param brightness */public static void setbrightness (activity activity) {CONTENTR Esolver 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);} /** * Get the brightness of the screen * * @param activity * @return */public static int getscreenbrightness (activity activity) {int nowbrightness Value = 0; Contentresolver resolver = Activity.getcontentresolver (); try {nowbrightnessvalue = Android.provider.Settings.System.getInt (resolver, Settings.System.SCREEN_BRIGHTNESS);} catch (Exception e) {e.printstacktrace ();} return nowbrightnessvalue;} /** * Jump to System settings * * @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);} /** * Get all Files under folder* @param path * @return */public static arraylist<file> Getfilesarray (String path) {File File = new file (path); File files[] = File.listfiles (); arraylist<file> listfile = new arraylist<file> (), 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;} /** * Get thumbnails of videos * First create a thumbnail of a video by Thumbnailutils, and then use thumbnailutils to generate a thumbnail of the specified size. * If the desired thumbnail width and height are less than micro_kind, then the type will use Micro_kind as the KIND value, which saves memory. * @param the path of the Videopath video * @param width Specifies the width of the output video thumbnail * @param height Specifies the height of the output video thumbnail * @param kind reference Mediasto Re. Constants Mini_kind and Micro_kind in the Images.thumbnails class. * Where mini_kind:512 x 384,micro_kind:96 x * * @return A video thumbnail of the specified size */public static Bitmap Getvideot Humbnail (String videopath, int width, int height, int kind) {Bitmap Bitmap = null; Get the videoThumbnail 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; }/** * Open Video File * @param context * @param file video file */public static void Intentvideo (context context, file file) {Intent inte NT = new Intent (Intent.action_view); String type = "video/*"; Uri uri = uri.fromfile (file); Intent.setdataandtype (URI, type); context.startactivity (intent);}}
Power on clear User data
Add a start-up broadcast
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;/** * Boot broadcast * @author Xiaosai * */public class Cleandatareceiverd extends Broadcastreceiver {@Overridepublic void OnReceive (context context, Intent Intent) {if (Intent . Getaction (). Equals (intent.action_boot_completed)) {//clear User Data 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 (); list<packageinfo> packages = 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 (M Ethods[j].getname ()))) {Targetmethod = Methods[j];break;}} String pkg = null;if (packages! = null && packages.size () > 0) {for (int i = 0; i < packages.size (); i++) {p kg = 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 ();}}}}}}
Clearing the user data requires calling the Clearapplicationuserdata method in the Activitymanager class, but this method is not called by the system to the hidden class, but we can invoke it through the reflection mechanism.
Androidmanifest.xml
<manifest xmlns:android= "http://schemas.android.com/apk/res/android" package= "Com.player.playlauncher" Android : versioncode= "1" android:versionname= "1.0" android:shareduserid= "Android.uid.system" > <uses-sdk and roid:minsdkversion= "8" android:targetsdkversion= "/> <uses-permission android:name=" android.permission. Receive_boot_completed "/> <uses-permission android:name=" Android.permission.CLEAR_APP_USER_DATA "/> < Application android:allowbackup= "true" android:icon= "@drawable/ic_launcher" android:label= "@string/ap P_name "Android:theme=" @style/apptheme "> <activity android:name=" Com.player.playlauncher. Firstlevel "android:label=" @string/app_name "android:icon=" @drawable/ic_launcher "> < ; Intent-filter > <action android:name= "Android.intent.action.MAIN"/> <category A Ndroid:name= "Android.intent. category. LAUNCHER "/> </intent-filter> </activity> <receiver android: Name= ". Cleandatareceiverd "> <intent-filter> <action android:name=" Android.intent.action.BOO T_completed "/> </intent-filter> </receiver> </application></manifest>
Save may be error, clean the project will be
Finally, you need to add a line to the ANDROID.MK
Local_certificate: = Platform
Success stems from continuous learning and accumulation!