Get the phone running software and size via thread communication (imitation phone butler scanning software)

Source: Internet
Author: User

In fact, I originally just want to say a thread between the communication handler, but feel a little bit of content, directly wrote a demo. I've done this kind of software (of course, just doing my own fun), so I've got a little bit of content combined with thread communication to write a small demo.

Don't talk nonsense, directly on the code:

Activity_main.xml

<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:id= "@+id/ll_main_layout"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical"
tools:context= "Com.ehsure.handlerdemo.MainActivity" >
<textview
Android:id= "@+id/main_change_tv"
android:gravity= "Center"
Android:textsize= "16SP"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
/>
<textview
Android:id= "@+id/main_scan_tv"
Android:layout_width= "Match_parent"
android:gravity= "Center"
Android:textsize= "16SP"
android:layout_height= "Wrap_content"
/>

</LinearLayout>

Mianactivity.class

public class Mainactivity extends Activity {
Private list<string> appnamelist = new arraylist<string> ();
public int backappsize = 0;
TextView Main_scan_tv;
TextView Main_change_tv;
LinearLayout ll_main_layout;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Initwidget ();
New MyThread (). Start ();
}

public void Initwidget () {
MAIN_CHANGE_TV = (TextView) Findviewbyid (R.ID.MAIN_CHANGE_TV);
MAIN_SCAN_TV = (TextView) Findviewbyid (R.ID.MAIN_SCAN_TV);
Ll_main_layout = (linearlayout) Findviewbyid (r.id.ll_main_layout);
}

/*
* Thread Scan all background software
*/
Class MyThread extends thread{
@Override
public void Run () {
TODO auto-generated Method Stub
try {
Packagemanager pm = MainActivity.this.getPackageManager ();
Activitymanager Activitymanager = (activitymanager) mainactivity.this
. Getsystemservice (MainActivity.this.ACTIVITY_SERVICE);
list<activitymanager.runningserviceinfo> Runningapp = Activitymanager
. getrunningservices (100);//This number is the maximum number of programs set
iterator<activitymanager.runningserviceinfo> Iterator = Runningapp.iterator ();
int Allcount = 0;//This is the total size of all background software
while (Iterator.hasnext ()) {
Activitymanager.runningserviceinfo Backapp = Iterator.next ();
ComponentName name = Backapp.service;
String pkgname = Name.getpackagename ();
ApplicationInfo apps = pm.getapplicationinfo (pkgname, 0);
String appName = (string) Apps.loadlabel (PM);//Get a name
drawable AppIcon = Apps.loadicon (PM);//Get icon
String AppPath = apps.publicsourcedir;//Get the path to the app
int length = (int) new File (AppPath). Length ();
Allcount+=length;
Systemclock.sleep (100);
String out = appname+ "~" +getfilesize (allcount);//This method is to convert bytes to KB or MB, write a small algorithm of their own
The reason this is written is because a software can have several processes, and if it is not judged by name, there will be a lot of repetition.
Boolean flag = true;
for (String ss:appnamelist) {
if (Ss.equals (AppName)) {
Flag = false;
}
}
if (flag==true) {
Appnamelist.add (AppName);
}
Handler.obtainmessage (0x001, Out). Sendtotarget ();
}
Backappsize = Allcount;
Systemclock.sleep (2000);
Handler.sendemptymessage (0x002);
} catch (Packagemanager.namenotfoundexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}

/*
* Convert bytes to KB or MB
*/
Public String GetFileSize (long size) {
String mysize = "";
if (size> (1024*1024)) {
BigDecimal bd = new BigDecimal ((double) size/(1024*1024));
BigDecimal Bd2 =bd.setscale (2, Bigdecimal.round_down);
Mysize = (double.parsedouble (bd2+ "")) + "M";
}else{
Mysize = (size/1024) + "K";
}
return mysize;
}




Handler Handler = new Handler () {
public void Handlemessage (Message msg) {
int code = Msg.what;
Switch (code) {
Case 0X001:
String str = (string) msg.obj;
String [] ss = Str.split ("~");
Main_scan_tv.settext (Ss[0]);
Main_change_tv.settext (Ss[1]);
Break
Case 0X002:
Main_scan_tv.settext ("Found" + appnamelist.size () + "background software");
Ll_main_layout.setbackgroundcolor (Color.rgb (10, 163, 245));
Break
}
}
};

}
I use the handler to send messages in two ways, of course, is recommended
1.

Handler.sendemptymessage (0x002); This is the one that doesn't deliver the message or the data.

2.
Handler.obtainmessage (0x001, Out). Sendtotarget (); This is also the most common one, is passing object objects past

In fact, there is another:

Message msg = new Message ();
Msg.what =0x001;
Msg.obj = out;
Handler.sendmessage (msg);

However, this method handler how many times the message will be instantiated, so it is not recommended because it will cause memory loss.








Get the phone running software and size via thread communication (imitation phone butler scanning software)

Related Article

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.