Android uses reflection to enforce a method to stop an application _android

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.