this is my code which show all install application - android

This is my source code below which shows all installed system applications. I want to show only selected applications like only 5 applications which name I provite not show all applications what do I do?? please help me
public class ListInstalledApps extends Activity implements OnItemClickListener {
/* whether or not to include system apps */
private static final boolean INCLUDE_SYSTEM_APPS = false;
private ListView mAppsList;
private AppListAdapter mAdapter;
private List<App> mApps;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAppsList = (ListView) findViewById(R.id.appslist);
mAppsList.setOnItemClickListener(this);
mApps = loadInstalledApps(INCLUDE_SYSTEM_APPS);
mAdapter = new AppListAdapter(getApplicationContext());
mAdapter.setListItems(mApps);
mAppsList.setAdapter(mAdapter);
new LoadIconsTask().execute(mApps.toArray(new App[]{}));
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final App app = (App) parent.getItemAtPosition(position);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
String msg = app.getTitle() + "\n\n" +
"Version " + app.getVersionName() + " (" +
app.getVersionCode() + ")" +
(app.getDescription() != null ? ("\n\n" + app.getDescription()) : "");
builder.setMessage(msg)
.setCancelable(true)
.setTitle(app.getTitle())
.setIcon(mAdapter.getIcons().get(app.getPackageName()))
.setPositiveButton("Launch", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// start the app by invoking its launch intent
Intent i = getPackageManager().getLaunchIntentForPackage(app.getPackageName());
try {
if (i != null) {
startActivity(i);
} else {
i = new Intent(app.getPackageName());
startActivity(i);
}
} catch (ActivityNotFoundException err) {
Toast.makeText(ListInstalledApps.this, "Error launching app",
Toast.LENGTH_SHORT).show();
}
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
/**
* Uses the package manager to query for all currently installed apps which are put
into beans and returned
* in form of a list.
*
* #param includeSysApps whether or not to include system applications
* #return a list containing an {#code App} bean for each installed application
*/
private List<App> loadInstalledApps(boolean includeSysApps) {
List<App> apps = new ArrayList<App>();
// the package manager contains the information about all installed apps
PackageManager packageManager = getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
//PackageManager.GET_META_DATA
for(int i=0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
// skip system apps if they shall not be included
if ((!includeSysApps) && ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1)) {
continue;
}
App app = new App();
app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
app.setPackageName(p.packageName);
app.setVersionName(p.versionName);
app.setVersionCode(p.versionCode);
CharSequence description = p.applicationInfo.loadDescription(packageManager);
app.setDescription(description != null ? description.toString() : "");
apps.add(app);
}
return apps;
}
/**
* An asynchronous task to load the icons of the installed applications.
*/
private class LoadIconsTask extends AsyncTask<App, Void, Void> {
#Override
protected Void doInBackground(App... apps) {
Map<String, Drawable> icons = new HashMap<String, Drawable>();
PackageManager manager = getApplicationContext().getPackageManager();
for (App app : apps) {
String pkgName = app.getPackageName();
Drawable ico = null;
try {
Intent i = manager.getLaunchIntentForPackage(pkgName);
if (i != null) {
ico = manager.getActivityIcon(i);
}
} catch (NameNotFoundException e) {
Log.e("ERROR", "Unable to find icon for package '" + pkgName + "': " +
e.getMessage());
}
icons.put(app.getPackageName(), ico);
}
mAdapter.setIcons(icons);
return null;
}
#Override
protected void onPostExecute(Void result) {
mAdapter.notifyDataSetChanged();
}
}
}

try this -
provide the package_name of application you want to show
and compare it with package name present in device
//for browser application given package name as com.android.browser
for (int i = 0; i < packs.size(); i++) {
if ((p.packageName).equals("com.android.browser")) {
App app = new App();
app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
app.setPackageName(p.packageName);
app.setVersionName(p.versionName);
app.setVersionCode(p.versionCode);
CharSequence description = p.applicationInfo.loadDescription(packageManager);
app.setDescription(description != null ? description.toString() : "");
apps.add(app);
}
}
hope this will help you.

You need a screening process to see if the application is in the list of your mentioned applications store your entered app package name in array and see if the current app is in that list if it is there add it to the list and then display listView using this list
private List<App> loadInstalledApps(boolean includeSysApps) {
List<App> apps = new ArrayList<App>();
// the package manager contains the information about all installed apps
PackageManager packageManager = getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
//PackageManager.GET_META_DATA
for(int i=0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
ApplicationInfo a = p.applicationInfo;
// skip system apps if they shall not be included
if ((!includeSysApps) && ((a.flags & ApplicationInfo.FLAG_SYSTEM) == 1)) {
continue;
}
App app = new App();
app.setTitle(p.applicationInfo.loadLabel(packageManager).toString());
app.setPackageName(p.packageName);
app.setVersionName(p.versionName);
app.setVersionCode(p.versionCode);
CharSequence description = p.applicationInfo.loadDescription(packageManager);
app.setDescription(description != null ? description.toString() : "");
if( Arrays.asList(appliation1.packageName,appliation2.packageName,appliation3.packageName).contains(p.packageName) )
apps.add(app);
}
return apps;
}

Related

PocketSphinx: How to recognize Keyword followed by dynamic word?

I have been working on PocketSphinx Android voice command project where the user can open application listed on their Android by use command "open xxx" (I made "open" as Keyword and xxx is application's name), but sadly it didn't work.
My question is, how PocketSphinx recognize two-words command, where the first word is Keyword and the second word is a dynamic word (user can say any application's name).
Declare keyword:
public class PocketSphinxActivity extends Activity implements
RecognitionListener {
/* Named searches allow to quickly reconfigure the decoder */
private static final String KWS_SEARCH = "wakeup";
/* Keyword we are looking for to activate menu */
private static final String KEYPHRASE = "open";
I tried to Split() two-words keyword (ex: open camera) and successfully opened camera, but what I need is "open xxx" so the user can define any application they want. That's why I change keyword to "open" only.
public void onPartialResult(Hypothesis hypothesis) {
if (hypothesis == null)
return;
String text = hypothesis.getHypstr();
String[] split = text.split("\\s+");
String word1 = split[0];
String word2 = split[1];
if (word2 != null) {
if (word1.equals("open")) {
PackageManager packageManager = getPackageManager();
List<PackageInfo> packs = packageManager.getInstalledPackages(0);
int size = packs.size();
boolean uninstallApp = false;
boolean exceptFlg = false;
for (int v = 0; v < size; v++) {
PackageInfo p = packs.get(v);
String tmpAppName = p.applicationInfo.loadLabel(packageManager).toString();
String pname = p.packageName;
tmpAppName = tmpAppName.toLowerCase();
if (tmpAppName.trim().toLowerCase().equals(word2.trim().toLowerCase())) {
PackageManager pm = this.getPackageManager();
Intent appStartIntent = pm.getLaunchIntentForPackage(pname);
if (null != appStartIntent) {
try {
this.startActivity(appStartIntent);
} catch (Exception e) {
}
}
}
}
finish();
public void onResult(Hypothesis hypothesis) {
((TextView) findViewById(R.id.result_text)).setText("");
if (hypothesis != null) {
String text = hypothesis.getHypstr();
String[] split = text.split("\\s+");
String word1 = split[0];
String word2 = split[1];
makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}
}
Any help on this would be grateful. I really need it to complete my final project. Thank you.

How to Get the package Names of System pre-installed applications in Android

I Already know how to get the package names of applications installed by the user.
But i cannot figure out how to get the package names of applications that are preinstalled or are system-ware like (Samsung Fit, Calender etc.) ??
List<ApplicationInfo> packages = context.getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo packageInfo : packages) {
Log.d(TAG, "Package :" + packageInfo.packageName);
Log.d(TAG, "Launcher Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName));
}
ApplicationInfo is having a packageName field which provides you package name of the application.
You can use below code to get the list of system application PackageInfo,
public static ArrayList<PackageInfo> getSystemPackageInfos(final Activity context) {
ArrayList<PackageInfo> list = new ArrayList<PackageInfo>();
ArrayList<String> packageNames = new ArrayList<String>();
PackageManager pm = context.getPackageManager();
List<PackageInfo> pinfoList = pm.getInstalledPackages(0);
Collections.sort(pinfoList, PackageNameComparator);
for (PackageInfo pinfo : pinfoList) {
packageNames.add(pinfo.packageName);
boolean isSystem = false;
if (((pinfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0)) {
isSystem = false;
} else {
isSystem = true;
}
if (pinfo.applicationInfo.sourceDir.startsWith("/data/app/") && isSystem) {
//Non-system app
isSystem = false;
}
if (!isSystem) {
continue;
}
Bitmap icon = null;
Drawable apkIcon = getApplicationIcon(pinfo.applicationInfo, context);
try {
icon = Bitmap.createBitmap(apkIcon.getIntrinsicWidth(), apkIcon.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(icon);
apkIcon.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
apkIcon.draw(canvas);
} catch (ClassCastException e) {
}
String name = pinfo.applicationInfo.loadLabel(pm).toString();
long apkSize = new File(pinfo.applicationInfo.sourceDir).length();
list.add(pinfo);
}
return list;
}

How to get the list of installed application from google play store in android?

i want the application list that is installed from google play or from any external storage, but not the pre installed system application.
i just use below code.....
class PInfo {
private String appname = "";
private String pname = "";
private String versionName = "";
private int versionCode = 0;
private Drawable icon;
private void prettyPrint() {
Log.v("", "*** appname = " + appname + " *** \n" + " packagename = " + pname + "\n" + " version name = " + versionName + "\n" + " version code = " + versionCode + " \n\n\n\n");
}
}
private ArrayList<PInfo> getPackages() {
ArrayList<PInfo> apps = getInstalledApps(true); /* false = no system packages */
final int max = apps.size();
int i = 0;
for (i=0; i<max; i++) {
apps.get(i).prettyPrint();
}
Log.v(TAG, "Total APPs = "+i);
return apps;
}
private ArrayList<PInfo> getInstalledApps(boolean getSysPackages) {
ArrayList<PInfo> res = new ArrayList<PInfo>();
List<PackageInfo> packs = getPackageManager().getInstalledPackages(PackageManager.GET_META_DATA);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
PInfo newInfo = new PInfo();
newInfo.appname = p.applicationInfo.loadLabel(getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(getPackageManager());
res.add(newInfo);
}
return res;
}
but this code gives me information of system application also.....
can anybody know how to get only installed application except the installed system application ?
thanks....
To discover if your application is a system application you can use the following code:
PackageManager pm = getPackageManager();
List<ApplicationInfo> installedApps = pm.getInstalledApplications(0);
for (ApplicationInfo aInfo: installedApps) {
if ((aInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
// system application
} else {
//user application
}
}
If you need to discover if an application is installed from a market you need to use the following method of Package: getInstallerPackageName. It will return the packageName of the application that installed your application.
public class AppList extends Activity {
private ListView lView;
private ArrayList results = new ArrayList();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lView = (ListView) findViewById(R.id.list1);
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
results.add(rInfo.activityInfo.applicationInfo
.loadLabel(pm).toString());
Log.w("Installed Applications", rInfo.activityInfo.applicationInfo
.loadLabel(pm).toString());
}
lView.setAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, results));
}
}

Get icons of all installed apps in android

I want to get icons of my all installed apps. Can I get that icons using package manager? Is there any function for it? Or any other way to get icons of all installed apps in bitmap?
Thanks!
try {
String pkg = "com.app.my";//your package name
Drawable icon = getContext().getPackageManager().getApplicationIcon(pkg);
imageView.setImageDrawable(icon);
} catch (PackageManager.NameNotFoundException ne) {
}
Check here for more details.
Above answers are pretty good.
Your Question is:- Get icons of all installed apps in android?
you want list of install apps icon
Here is the code which help you to get install apps list with Application (icons,packages names).
**Declare variable in your Activity**
private CustomAppListAdapter customAppListAdapter;
private ArrayList<AppListMain> appListMainArrayList;
private AppListMain appListMain;
Just call below function loadApps() in your Activity onCreate()
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_app_list);
loadApps();
}
public void loadApps() {
try {
packageManager = getPackageManager();
appListMainArrayList = new ArrayList<>();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> resolveInfoList = packageManager.queryIntentActivities(intent, 0);
for (ResolveInfo resolveInfo : resolveInfoList) {
AppListMain appListMain = new AppListMain();
appListMain.setAppIcon(resolveInfo.activityInfo.loadIcon(packageManager));
appListMain.setAppName(resolveInfo.loadLabel(packageManager).toString());
appListMain.setAppPackage(resolveInfo.activityInfo.packageName);
appListMainArrayList.add(appListMain);
}
} catch (Exception e) {
e.printStackTrace();
}
}
Here is Link for reference
OR
You can download custom launcher code from My Github repository
I find it the easiest way:
private List<ResolveInfo> installedApps() {
final Intent main_intent = new Intent(Intent.ACTION_MAIN, null);
main_intent.addCategory(Intent.CATEGORY_LAUNCHER);
return package_manager.queryIntentActivities(main_intent, 0);
}
Now to get the icons, use this:
for(ResolveInfo ri : installedApps()) {
// to get drawable icon --> ri.loadIcon(package_manager)
}
Try this way: Make a class called PackageInformation:
public class PackageInformation {
private Context mContext;
public PackageInformation(Context context) {
mContext = context;
}
class InfoObject {
public String appname = "";
public String pname = "";
public String versionName = "";
public int versionCode = 0;
public Drawable icon;
public void InfoObjectAggregatePrint() { //not used yet
Log.v(appname, appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
}
}
private ArrayList < InfoObject > getPackages() {
ArrayList < InfoObject > apps = getInstalledApps(false);
final int max = apps.size();
for (int i = 0; i < max; i++) {
apps.get(i).prettyPrint();
}
return apps;
}
public ArrayList < InfoObject > getInstalledApps(boolean getSysPackages) {
ArrayList < InfoObject > res = new ArrayList < InfoObject > ();
List < PackageInfo > packs = mContext.getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packs.size(); i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue;
}
InfoObject newInfo = new InfoObject();
newInfo.appname = p.applicationInfo.loadLabel(mContext.getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(mContext.getPackageManager());
res.add(newInfo);
}
return res;
}
}
tuck this away somewhere and now to access the info from your working Activity class do this:
PackageInformation androidPackagesInfo = new PackageInformation(this);
ArrayList < InfoObject > appsData = androidPackagesInfo.getInstalledApps(true);
for (InfoObject info: appsData) {
Toast.makeText(MainActivity.this, info.appname, 2).show();
Drawable somedrawable = info.icon;
}
Here below is the code with which you can get the icons of all installed Apps.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
// try getting the properly colored launcher icons
LauncherApps launcher = (LauncherApps) this.getSystemService(LAUNCHER_APPS_SERVICE);
List<LauncherActivityInfo> activityList = launcher.getActivityList(packageName, android.os.Process.myUserHandle());
drawable = activityList.get(0).getBadgedIcon(0);
} catch (Exception e) {
}
}
if (drawable == null) {
try {
getPackageManager().getApplicationIcon(packageName);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}

getting Icons of applications running on device

I have a list activity which display all running applications in the device. it's displaying a default icon for all application . but I need to get actual application Icon for each application in the list.
public class ApplicationList extends ListActivity {
DataHelper dh;
ImageView vi;
private Drawable icon;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.applist);
dh = new DataHelper(getApplicationContext());
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
Log.d("Test1", "INSERTING APP LIST TO SERVER DB");
List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
ArrayList<String> applist = new ArrayList<String>();
for(ResolveInfo rInfo: list){
applist.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString() );
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.installedapp, R.id.textView1, applist);
setListAdapter(adapter);
}
How to get the icon of other applications (Android)
I tried this link but I did not get things solved. so can any one help me out. thanks!
try like this
List<PackageInfo> packs = getPackageManager().getInstalledPackages(0);
Drawable[] icons=new Drawable[packs.size];
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
icons[i]= p.applicationInfo.loadIcon(getPackageManager());
}
try this:
Icon icon = p.applicationInfo.loadIcon(getPackageManager());
imageView.setImageDrawable(icon);
try this way make a class called packageinformation:
public class PackageInformation{
private Context mContext;
public PackageInformation(Context context){
mContext=context;
}
class InfoObject {
public String appname = "";
public String pname = "";
public String versionName = "";
public int versionCode = 0;
public Drawable icon;
public void InfoObjectAggregatePrint() {//not used yet
Log.v(appname,appname + "\t" + pname + "\t" + versionName + "\t" + versionCode);
}
}
private ArrayList getPackages() {
ArrayList apps = getInstalledApps(false); /* false = no system packages */
final int max = apps.size();
for (int i=0; i
public ArrayList<InfoObject> getInstalledApps(boolean getSysPackages) {
ArrayList<InfoObject> res = new ArrayList<InfoObject>();
List<PackageInfo> packs = mContext.getPackageManager().getInstalledPackages(0);
for(int i=0;i<packs.size();i++) {
PackageInfo p = packs.get(i);
if ((!getSysPackages) && (p.versionName == null)) {
continue ;
}
InfoObject newInfo = new InfoObject();
newInfo.appname = p.applicationInfo.loadLabel(mContext.getPackageManager()).toString();
newInfo.pname = p.packageName;
newInfo.versionName = p.versionName;
newInfo.versionCode = p.versionCode;
newInfo.icon = p.applicationInfo.loadIcon(mContext.getPackageManager());
res.add(newInfo);
}
return res;
}
}
tuck this away somewhere and now to access the info from your working Activity class do this:
PackageInformation androidPackagesInfo=new PackageInformation(this);
ArrayList<InfoObject> appsData=androidPackagesInfo.getInstalledApps(true);
for (InfoObject info : appsData) {
Toast.makeText(MainActivity.this, info.appname,2).show();
Drawable somedrawable=info.icon;
}

Categories

Resources