Android development: Get all software information, android Development

Source: Internet
Author: User

Android development: Get all software information, android Development

Program running:


Program code:

/*** Obtain all software information ** 1. display All software in the system in asynchronous mode * 2. click Open specified software * 3. local SharedPreferences * @ author jph * Date: 2014.09.21 */public class ScanPackage1 extends Activity {/** scan successful **/private final static int FLAG_LOAD_SUCCESS = 0x10001; private final static int SCANNING = 0x10002; private ListView list; private List <Map <String, Object> items = new ArrayList <Map <String, Object> (); private SimpleAdapter adapter; // Obtain all installed software information: private List <PackageInfo> allPackageInfos; // obtain the installed software information: private List <PackageInfo> userPackageInfos; // obtain the software information installed in the system. private List <PackageInfo> sysPackageInfos; Handler mHandler = new Handler () {@ Overridepublic void handleMessage (Message msg) {// TODO Auto-generated method stubswitch (msg. what) {case FLAG_LOAD_SUCCESS: // scan break; case SCANNING: // SCANNING items. add (Map <String, Object>) msg. obj );// Notifies the adapter of data change. yydatasetchanged (); break; default: break ;}};@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. sp_layout); list = (ListView) findViewById (R. id. list); new ScanThread (). start (); adapter = new SimpleAdapter (this, items, R. layout. line, new String [] {"imgIco", "appName", "packageName"}, new int [] {R. id. imgIco, R. id. tvAppName, R. Id. tvAppDesc}); list. setAdapter (adapter); // The ViewBinder class can help SimpleAdapter load the image (for example, Bitmap, Drawable) adapter. setViewBinder (new ViewBinder () {@ Overridepublic boolean setViewValue (View view, Object data, String textRepresentation) {// TODO Auto-generated method stub if (view instanceof ImageView & data instanceof Drawable) {ImageView iv = (ImageView) view; iv. setImageDrawable (Drawable) data); return true;} Else {return false ;}}); list. setOnItemClickListener (new OnItemClickListener () {@ Overridepublic void onItemClick (AdapterView <?> Arg0, View arg1, int arg2, long arg3) {// TODO Auto-generated method stubtry {PackageInfo pInfo = allPackageInfos. get (arg2); Intent intent = new Intent (); intent. setComponent (new ComponentName (pInfo. packageName, pInfo. activities [0]. name); startActivity (intent);} catch (Exception e) {// TODO: handle has tione. printStackTrace ();}}});} // *************** -------- * Create a thread to load the installer *--------------************* ***** * // Private class ScanThread extends Thread {@ Overridepublic void run () {// obtain information about all software installed in the system allPackageInfos = getPackageManager (). getInstalledPackages (PackageManager. GET_UNINSTALLED_PACKAGES | PackageManager. GET_ACTIVITIES); // defines the user's Installation Software Package userPackageInfos = new ArrayList <PackageInfo> (); // defines the system installation software package sysPackageInfos = new ArrayList <PackageInfo> (); // cyclically retrieve all software information for (int I = 0; I <allPackageInfos. size (); I + +) {// Obtain each software information PackageInfo temp = allPackageInfos. get (I); ApplicationInfo appInfo = temp. applicationInfo; if (appInfo. flags & ApplicationInfo. FLAG_UPDATED_SYSTEM_APP )! = 0 | (appInfo. flags & ApplicationInfo. FLAG_SYSTEM )! = 0) {// System Software sysPackageInfos. add (temp);} else {// The user installs the software userPackageInfos. add (temp);} // obtain the program icon Drawable ico = ScanPackage1.this. getPackageManager (). getApplicationIcon (appInfo); // obtain the program name String appName = (String) ScanPackage1.this. getPackageManager (). getApplicationLabel (appInfo); Map <String, Object> item = new HashMap <String, Object> (); // obtain the program package name String packageName = appInfo. packageName; item. put ("imgIco", ico); item. put ("appName", appName); item. put ("packageName", packageName); Message message = new Message (); message. what = SCANNING; message. obj = item; mHandler. sendMessage (message);} saveInfo (sysPackageInfos, userPackageInfos); mHandler. sendEmptyMessage (FLAG_LOAD_SUCCESS );}}; /*** write information about programs installed in the system to the configuration file * @ param sysPackageInfos System Installation Software Package * @ param userPackageInfos user installation software package */private void saveInfo (List <PackageInfo> sysPackageInfos, list <PackageInfo> userPackageInfos) {// Add the software installed by the user to the sysPackageInfos collection added to the system software. addAll (userPackageInfos); SharedPreferences sp = this. getSharedPreferences ("appInfs", MODE_PRIVATE); Editor editor = sp. edit (); for (int I = 0; I <sysPackageInfos. size (); I ++) {try {// get the package name of the program String packageName = sysPackageInfos. get (I ). packageName; // retrieves activity information ActivityInfo activityInfo = sysPackageInfos. get (I ). activities [0]; // retrieve activity name String activityName = activityInfo. name; // write program information to the configuration file editor. putString (packageName, activityName);} catch (Exception e) {// TODO: handle effectione. printStackTrace () ;}} editor. commit ();}}



How to obtain the app version number in Android Development

Application scenarios: 1. display the application version number on the interface;
2. When the user starts the application, the background determines whether the application is the latest version.
In the preceding scenario, the application version must be automatically obtained in the program.
Idea Introduction: In Android, the application version is in AndroidManifest. the PackageInfo class encapsulates all information obtained from the configuration file and describes the overall information of the Package content, you can use the versionName attribute of the PackageInfo object to obtain the application version number.
How can I obtain the PackageInfo object? You can obtain the PackageManager object. PackageManager is a class used to retrieve information about related application packages installed on devices. The getPackageInfo method in the PackageManager object can obtain the PackageInfo object. This method requires two parameters: application package name and condition. Generally, the package name of an application can be obtained through the getPackageName () method of Activity or Context (Activity inherited from Context). However, many settings can be added, usually set to 0.
Finally, the PackageManager object is obtained. The Context object provides the getPackageManager () method to obtain the object.
To sum up,
The template code is as follows: (Note that the encapsulated method is located in an Activity, so use this directly to replace the Context object)
Copy to Clipboard
Reference content:

In android development, how does one obtain the content specified by the system SMS ??

Java code
// Android obtains all text message content
// Pay attention to setting permissions [add to AndroidMainfest. xml] <uses-permission android: name = "android. permission. READ_SMS"/>

Public String getSmsInPhone ()
{
Final String SMS_URI_ALL = "content: // sms /";
Final String SMS_URI_INBOX = "content: // sms/inbox ";
Final String SMS_URI_SEND = "content: // sms/sent ";
Final String SMS_URI_DRAFT = "content: // sms/draft ";

StringBuilder smsBuilder = new StringBuilder ();

Try {
ContentResolver cr = getContentResolver ();
String [] projection = new String [] {"_ id", "address", "person ",
"Body", "date", "type "};
Uri uri = Uri. parse (SMS_URI_ALL );
Cursor cur = cr. query (uri, projection, null, null, "date desc ");

If (cur. moveToFirst ()){
String name;
String phoneNumber;
String smsbody;
String date;
String type;

Int nameColumn = cur. getColumnIndex ("person ");
Int phoneNumberColumn = cur. getColumnIndex ("address ");
Int smsbodyColumn = cur. g ...... remaining full text>
 

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.