The example in this article describes how Android enforces a forced stop application through reflection. Share to everyone for your reference, specific as follows:
Private Activitymanager Manager;
Private list<runningappprocessinfo> runningprocesses;
Private String Packname;
Private Packagemanager Pmanager;
@Override public void OnReceive (context context, Intent Intent) {Pmanager = Context.getpackagemanager ();
Manager = (Activitymanager) context.getsystemservice (Context.activity_service);
Killothers (context); /** * kills other running programs * * @param context/private void killothers (context) {runningprocesses = Manager.getrun
Ningappprocesses ();
for (Runningappprocessinfo runningprocess:runningprocesses) {try {packname = Runningprocess.processname;
ApplicationInfo applicationinfo = pmanager.getpackageinfo (packname, 0). ApplicationInfo; if (!)
Com.asms ". Equals (Packname) &&filterapp (applicationinfo)) {forcestoppackage (packname,context);
System.out.println (packname+ "JJJJJJ");
}}catch (Exception e) {e.printstacktrace (); }}}/** * mandatoryStop application * @param pkgname */private void Forcestoppackage (String pkgname,context context) throws exception{Activityman
Ager am = (activitymanager) context.getsystemservice (Context.activity_service);
Method method = Class.forName ("Android.app.ActivityManager"). GetMethod ("Forcestoppackage", String.class);
Method.invoke (AM, pkgname); /** * To determine whether an application is a tripartite application * @param info * @return/public boolean filterapp (ApplicationInfo info) {if (Info.flag
S & Applicationinfo.flag_updated_system_app)!= 0) {return true;
else if ((Info.flags & applicationinfo.flag_system) = = 0) {return true;
return false;
}
Note: If the Onreceiver method is replaced by OnCreate method in the activity.
2. Need to set the package UserID to system in Androidmanifest.xml
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
package= "Com.asms"
android: Versioncode= "1"
android:versionname= "1.0" android:shareduserid= "Android.uid.system" >
More interested readers of Android-related content can view this site: "Introduction to Android Development and advanced Course", "Android Service Component Usage Summary", "Android Basic component Usage Summary" and "Android Control usage Summary"
I hope this article will help you with the Android program.