[Android] simple task manager (non-root)

Source: Internet
Author: User

[Android] simple task manager (non-root)

Because it is not a system-level application and does not obtain the ROOT permission, it is not very significant to implement the task manager by yourself, just as the mobile assistant like LBE is installed on a mobile phone without a root phone, although it also provides a one-click cleanup function to clear background processes, because the mobile assistant does not have the highest ROOT permission, it is also useless to enable the daemon process or other self-starting application processes. with the promotion of Google, the new Android M system that will be pushed to the market is also increasingly strict with permission management, which also shows a trend, the security and stability requirements for mobile systems are getting higher and higher.

 

Although it doesn't make a lot of sense, let's practice it. Maybe it will be used one day... hahaha.

()

 

APIS directly provide interfaces for obtaining package information, application process information, and disabling background processes. the idea is probably to first obtain all running processes, including system application processes and non-system application processes. to prevent system process errors caused by false shutdown, we filter out system processes and only display non-system processes in the list. based on the package name of the process, you can disable a running process or all processes.

 

However, through the example here, we found that software such as this was disabled, but soon we re-created the thread in the background to continue running, so the best third-party mobile assistant application, as long as the application does not have the root permission or system permission, the process cannot be completely closed or self-initiated. even with functions such as screen lock automatic background clearing and one-click memory cleanup, common applications without root permissions can maintain the vitality of the application in a way similar to the daemon process, then, the background process is cleared at intervals. it also consumes a little additional power. of course, this is good. It depends on whether an application in the background can kill many third-party background applications and save more power than the original ones.

 

There is no difficulty in the implementation process, so you can directly go to the Code:

The first is the ListView item layout file process_list_item.xml:

 

     
      
       
    
     
    
   
  
 

Then the layout file activity_main.xml of the main class:

 

 

     
  
       
    
  
 

 

Process Information object ProcessInfo:

 

/** * Created by AlexTam on 2015/7/29. */public class ProcessInfo {    private String labelName;    private Drawable labelIcon;    private String processName;    public ProcessInfo(){ }    public ProcessInfo(String labelName, Drawable labelIcon, String processName) {        this.labelName = labelName;        this.labelIcon = labelIcon;        this.processName = processName;    }    public String getLabelName() {        return labelName;    }    public void setLabelName(String labelName) {        this.labelName = labelName;    }    public Drawable getLabelIcon() {        return labelIcon;    }    public void setLabelIcon(Drawable labelIcon) {        this.labelIcon = labelIcon;    }    public String getProcessName() {        return processName;    }    public void setProcessName(String processName) {        this.processName = processName;    }}

Next is the Adapter, which adds a click event to the buttons of each item:

 

 

/** * Created by AlexTam on 2015/7/29. */public class ProcessListAdapter extends BaseAdapter{    private Context context;    private List
 
   processList;    private viewHolder holder;    private processListButtonClick listener;    public ProcessListAdapter(Context context, List
  
    processList,            processListButtonClick listener)    {        this.context = context;        this.processList = processList;        this.listener = listener;    }    @Override    public int getCount() {        return processList.size();    }    @Override    public Object getItem(int position) {        return processList.get(position);    }    @Override    public long getItemId(int position) {        return position;    }    @Override    public View getView(int position, View convertView, ViewGroup parent) {        ProcessInfo processInfo = (ProcessInfo)getItem(position);        if(convertView == null)        {            holder = new viewHolder();            convertView = View.inflate(context, R.layout.process_list_item, null);            holder.imv_avatar = (ImageView) convertView.findViewById(R.id.imv_avatar);            holder.tv_name = (TextView) convertView.findViewById(R.id.tv_app_name);            holder.tv_processName = (TextView) convertView.findViewById(R.id.tv_app_process_name);            holder.btn = (Button) convertView.findViewById(R.id.btn_stop_app);        }        else        {            holder = (viewHolder)convertView.getTag();        }        holder.imv_avatar.setImageDrawable(processInfo.getLabelIcon());        holder.tv_name.setText(processInfo.getLabelName());        holder.tv_processName.setText(processInfo.getProcessName());        holder.btn.setOnClickListener(new POnClickListener(processInfo.getProcessName()));        convertView.setTag(holder);        return convertView;    }    private class POnClickListener implements View.OnClickListener    {        private String processName;        public POnClickListener(String processName)        {            this.processName = processName;        }        @Override        public void onClick(View v)        {            if(listener != null)                listener.onButtonClick(processName);        }    }    private class viewHolder    {        ImageView imv_avatar;        TextView tv_name;        TextView tv_processName;        Button btn;    }    public interface processListButtonClick    {        void onButtonClick(String processName);    }}
  
 

OK, and the main MainActivity:

 

 

/*** Created by AlexTam on 2015/7/29. */public class MainActivity extends Activity {private ListView ll_main; private List
 
  
ProcessList = new ArrayList
  
   
(); Private List applicationInfoList; private ProcessListAdapter adapter = null; private Button btn_clear; private List
   
    
ProcessNamelist = new ArrayList
    
     
(); Private ProgressDialog progreprogre; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); ll_main = (ListView) findViewById (R. id. lv_main); btn_clear = (Button) findViewById (R. id. btn_main_clear); getProcessList (); btn_clear.setOnClickListener (new MyOnclick () ;}@ Override public void onDestroy () {super. onDes Troy ();} private class MyOnclick implements View. onClickListener {@ Override public void onClick (View v) {if (v = btn_clear) {clearAllBackgroundProcess ();}}} /*** obtain the process information List */private void getProcessList () {ActivityManager activityManager = (ActivityManager) getSystemService (ACTIVITY_SERVICE); // obtain the List of all running processes runningAppList = activityManager. getRunningAppProcesses (); PackageManager packag EManager = this. getPackageManager (); // obtain all package information applicationInfoList = packageManager. getInstalledApplications (PackageManager. GET_UNINSTALLED_PACKAGES); if (processList! = Null & processList. size ()> 0) processList. clear (); if (processNamelist! = Null & processNamelist. size ()> 0) processNamelist. clear (); for (ActivityManager. runningAppProcessInfo process: runningAppList) {if (process. processName. indexOf (this. getPackageName () <0) {// filter the application package name ProcessInfo p = new ProcessInfo (); ApplicationInfo appInfo = getApplicationInfoByProcessName (process. processName); if (appInfo = null) {// The daemon of some applications does not correspond to the target application. At this time, null} else {p. setLabelIcon (appInfo. loadIcon (packageManager); p. setLabelName (appInfo. loadLabel (packageManager ). toString (); p. setProcessName (appInfo. processName); processNamelist. add (appInfo. processName); processList. add (p) ;}}if (adapter = null) {adapter = new ProcessListAdapter (MainActivity. this, processList, new ItemButtonClick (); ll_main.setAdapter (adapter); ll_main.setOnItemClickListener (new MyOnItemClickListener ();} else {adapter. notifyDataSetChanged () ;}}/*** obtain application information based on the process name * @ param processNames * @ return */private ApplicationInfo getApplicationInfoByProcessName (String processNames) {if (applicationInfoList = null | applicationInfoList. size () <1) return null; for (ApplicationInfo applicationInfo: applicationInfoList) {if (applicationInfo. processName. equals (processNames) & (applicationInfo. flags & ApplicationInfo. FLAG_SYSTEM) <= 0) // only display third-party application processes, not system applications // to display all application processes, delete (applicationInfo. flags & ApplicationInfo. FLAG_SYSTEM) <= 0 to return applicationInfo;} return null;} private class MyOnItemClickListener implements AdapterView. onItemClickListener {@ Override public void onItemClick (AdapterView
     Parent, View view, int position, long id) {TextView TV _appName = (TextView) view. findViewById (R. id. TV _app_name); TextView TV _processName = (TextView) view. findViewById (R. id. TV _app_process_name); String appName = TV _appName.getText (). toString (); String processName = TV _processName.getText (). toString (); Toast. makeText (MainActivity. this, application: + appName + process: + processName, Toast. LENGTH_SHORT ). show ();} Private class ItemButtonClick implements ProcessListAdapter. processListButtonClick {String pName = null; @ Override public void onButtonClick (String processName) {pName = processName; AlertDialog. builder builder = new AlertDialog. builder (MainActivity. this); builder. setTitle (disable process ). setMessage (Are you sure you want to disable + processName + process ?). SetPositiveButton (OK, new DialogInterface. OnClickListener () {public void onClick (DialogInterface Diener, int which) {if (pName! = Null) {ActivityManager activityManager = (ActivityManager) MainActivity. this. getSystemService (ACTIVITY_SERVICE); activityManager. killBackgroundProcesses (pName); getProcessList ();}}}). setNegativeButton (cancel, new DialogInterface. onClickListener () {public void onClick (DialogInterface diich, int which) {dialog. dismiss () ;}}); builder. show () ;}} private Handler mHandler = new Handler () {@ Overr Ide public void handleMessage (Message msg) {if (msg. what = 0x1) {startClearAnim ();} else if (msg. what = 0x2) {stopClearAnim (); getProcessList ();} super. handleMessage (msg) ;}};/*** one-click cleanup */private void clearAllBackgroundProcess () {mHandler. sendEmptyMessage (0x1); ActivityManager activityManager = (ActivityManager) MainActivity. this. getSystemService (ACTIVITY_SERVICE); if (processNamelist! = Null & processNamelist. size ()> 0) {for (String processName: processNamelist) {activityManager. killBackgroundProcesses (processName);} mHandler. sendEmptyMessageDelayed (0x2, 2000);} private void startClearAnim () {progressDialog = new ProgressDialog (MainActivity. this); progressDialog. setMessage (trying to clean up ...); progressDialog. show ();} private void stopClearAnim () {progressDialog. dismiss ();}}
    
   
  
 

Because it is a function to explore, a one-click cleanup does not include complex animations. if you want to add an ImageView, you can consider hiding an ImageView in the middle of the view, and then using the AnimatorSet to store two Property animations. One is to control the ImageView from transparent to completely displayed, the size is also from 0.0f to 1.0f. the second is to control infinite rotation. of course, it can also be implemented using Animation. after cleaning, hide the ImageView again and set it to gone. for those who put shortcuts on the desktop of the mobile phone, and then click the shortcut component to have an animation, while cleaning the background, you can consider making an Activity, theme is a dieme, then play the animation and clear the background. similar results can also be achieved.

 

 

I believe many people understand that the Android system has its own operating mechanism, and each application has its own process. Each process has a virtual machine, this is designed to prevent system running and other processes from being crashed. when running some memory-consuming applications, the system will automatically take the low priority as useless and obsolete background processes will be shut down to squeeze out the memory. of course, this is not omnipotent, because the system is not always able to handle it in such a timely manner, so that when we use applications such as games, low-memory mobile phones may still feel stuck due to insufficient memory. therefore, it seems reasonable to manually ask the background.

 

 

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.