Search This Blog

Monday 19 August 2013

Getting List of non system application from Android phone

Getting all application list from android phone is quite easy. PackageManger gives the list of all installed application. But main concern remain how to avoid system from the list. I have done this job and prepare a piece of code to implement this functionality in an easiest way.
I keep filter to avoid non system application.

Filter to avoid Non System Application
           private boolean isSystemPackage(ApplicationInfo pkgInfo) {  
                int mask = ApplicationInfo.FLAG_SYSTEM  
                          | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;  
                return (pkgInfo.flags & mask) == 0;  
           }  

GetallApTest .Java will find all system app in background and will give you the list
 import java.util.ArrayList;  
 import java.util.List;  
 import android.app.Activity;  
 import android.app.ProgressDialog;  
 import android.content.pm.ApplicationInfo;  
 import android.content.pm.PackageInfo;  
 import android.content.pm.PackageManager.NameNotFoundException;  
 import android.os.AsyncTask;  
 import android.os.Bundle;  
 import android.util.Log;  
 import com.eurotronik.isenior.myMenu.MyAppData;  
 public class GetallApTest extends Activity {  
      @Override  
      protected void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           AndroidApplication mAsAndroidApplication = new AndroidApplication();  
           mAsAndroidApplication.execute();  
      }  
      private class AndroidApplication extends  
                AsyncTask<Void, ArrayList<MyAppData>, ArrayList<MyAppData>> {  
           ProgressDialog pd;  
           @Override  
           protected void onPreExecute() {  
                super.onPreExecute();  
                pd = new ProgressDialog(GetallApTest.this);  
                pd.setMessage("Loadin...");  
                pd.setCancelable(false);  
                pd.show();  
           }  
           @Override  
           protected ArrayList<MyAppData> doInBackground(Void... params) {  
                ArrayList<MyAppData> res = new ArrayList<MyAppData>();  
                List<PackageInfo> packs = getPackageManager().getInstalledPackages(  
                          0);  
                for (int i = 0; i < packs.size(); i++) {  
                     PackageInfo p = packs.get(i);  
                     try {  
                          ApplicationInfo ai = getPackageManager()  
                                    .getApplicationInfo(p.packageName, 0);  
                          if ((!isSystemPackage(ai))) {  
                               Log.e("Skip", "App");  
                               continue;  
                          }  
                     } catch (NameNotFoundException e) {  
                          e.printStackTrace();  
                     }  
                     MyAppData newInfo = new MyAppData();  
                     newInfo.setName(p.applicationInfo  
                               .loadLabel(getPackageManager()).toString());  
                     newInfo.setImage(p.applicationInfo  
                               .loadIcon(getPackageManager()));  
                     res.add(newInfo);  
                }  
                return res;  
           }  
           private boolean isSystemPackage(ApplicationInfo pkgInfo) {  
                int mask = ApplicationInfo.FLAG_SYSTEM  
                          | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP;  
                return (pkgInfo.flags & mask) == 0;  
           }  
           @Override  
           protected void onProgressUpdate(ArrayList<MyAppData>... values) {  
                super.onProgressUpdate(values);  
                pd.dismiss();  
                /**  
                 * List OF app  
                 */  
                ArrayList<MyAppData> mList = values[0];  
           }  
      }  
 }  

No comments:

Post a Comment

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code