This is the code that i have at the moment but i still can't get a list of all the apps on the phone. Does anyone see what i am doing wrong?
public class GetAppList extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
List<PackageInfo> appListInfo1 = this.getPackageManager()
.getInstalledPackages(0);
JSONArray ja = new JSONArray();
try {
HttpClient httpclient = new DefaultHttpClient();
Object sendDataUrl = null;
HttpPost request = new HttpPost(sendDataUrl.toString());
List<NameValuePair> params = new ArrayList<NameValuePair>();
ContextWrapper context = null;
PackageManager pm = context.getPackageManager();
List<PackageInfo> appListInfo = pm.getInstalledPackages(0);
for (PackageInfo p : appListInfo) {
if (p.applicationInfo.uid > 10000) {
JSONObject jo = new JSONObject();
jo.put("label", p.applicationInfo.loadLabel(pm).toString());
jo.put("packageName", p.applicationInfo.packageName);
ja.put(jo);
}
System.out.print(ja);
}
} catch (Exception e) {
// TODO: handle exception
}
finally {
// ... cleanup that will execute whether or not an error occurred ...
}
}catch (Exception e) {
// TODO: handle exception
}
finally {
// ... cleanup that will execute whether or not an error occurred ...
}
}}
Sorry cant get this to format the code properly.
This code logs name and package name of all the installed applications. You can easily check the log using LogCat (if you are using Eclipse).
Package name - name of the app's package.
Name - text label associated with the application. You can't use just appInfo.name as it will return null. Using appInfo.loadLabel(packageManager) you get the actual app's name like Speech Recorder instead of package name com.android.speechrecorder.
final PackageManager packageManager = getPackageManager();
List<ApplicationInfo> installedApplications =
packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
for (ApplicationInfo appInfo : installedApplications)
{
Log.d("OUTPUT", "Package name : " + appInfo.packageName);
Log.d("OUTPUT", "Name: " + appInfo.loadLabel(packageManager));
}
This is the code I use to list applications:
PackageManager pm = this.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
String activityName = rInfo.activityInfo.name;
List<ResolveInfo> list = pm.queryIntentActivities(intent, PackageManager.PERMISSION_GRANTED);
for (ResolveInfo rInfo : list) {
pkg = rInfo.activityInfo.applicationInfo.packageName;
if (pm.getLaunchIntentForPackage(pkg) == null) {
continue;
}
String label = rInfo.activityInfo.applicationInfo.loadLabel(pm).toString();
arrayList.add(new AppEntry(label, activityName, pkg, null));
}
If you later on want to run application only knowing pkg and activityName you can do:
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClassName(pkg, activityname);
ctx.startActivity(intent);
Related
I'm trying to get the ApplicationInfo for Settings.ACTION_ACCESSIBILITY_SETTINGS or Settings.ACTION_BATTERY_SAVER_SETTINGS, so that I can launch them directly.
I tried following steps but it's return setting's ApplicationInfo.
private static final String[] SETTINGS_ACTION_MENUS = new String[]{
Settings.ACTION_ACCESSIBILITY_SETTINGS,Settings.ACTION_ADD_ACCOUNT};
public void getSettingPages(){
for(String menu : SETTINGS_ACTION_MENUS){
final Intent intent = new Intent(menu);
ComponentName componentName = intent.resolveActivity(packageManager);
if(componentName != null){
try {
ApplicationInfo info = packageManager.getApplicationInfo(componentName.getPackageName(),0);
if(packageManager.getLaunchIntentForPackage(info.packageName) != null){
launchableApps.add(info);
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
}
}
I tried following steps but it's return setting's ApplicationInfo
That is because those Intent actions are tied to activities in the Settings app. Individual activities do not have their own ApplicationInfo.
Sorry for this question but i want to filter the apps list with permission name INTERNET
how can i do that ?
<uses-permission android:name="android.permission.INTERNET" />
trying something like this
PackageManager pm = context.getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
for (ResolveInfo ri : activities) {
ApplicationInfoclass info = new ApplicationInfoclass();
// Add the non system package to the list
try {
PackageInfo pInfo = context.getPackageManager().getPackageInfo(ri.activityInfo.packageName, 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
info.packageName = ri.activityInfo.packageName;
info.name = (String) ri.loadLabel(pm);
info.icon = ri.activityInfo.loadIcon(pm);
apps.add(info);
}
PackageManager pm = context.getPackageManager();
for (ResolveInfo ri : pm.queryIntentActivities(intent, 0)) {
try {
PackageInfo packageInfo = pm.getPackageInfo(ri.activityInfo.packageName, 0);
for (PermissionInfo permissionInfo : packageInfo.permissions) {
if (permissionInfo.name.equals(Manifest.permission.INTERNET)) {
//package has internet permission, do something with it
break;
}
}
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
}
I am developing an app in which if any app is installed in device I have to update to server with app name for that I have to get Application name from package name.
for example :- from package let suppose :- <com.example.Deals> from this I have to get only <Deals>. How can I do that.
here is my code from which I got package name using Broadcast receiver.
public class Receiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals("android.intent.action.PACKAGE_ADDED")){
Logger.debug("DATA:"+intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REMOVED")){
Logger.debug("DATA:"+intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REPLACED")){
Logger.debug("DATA:"+intent.getData().toString());
}
}
}
You can get Application name from package using following code
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( "your_package_name", 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
PackageManager packageManagers= getApplicationContext().getPackageManager();
try {
String appName = (String) packageManagers.getApplicationLabel(packageManagers.getApplicationInfo("com.example.packagename", PackageManager.GET_META_DATA));
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
Here dont miss to change your package name com.example.packagename
check following code
PackageManager packageManager = context.getPackageManager();
ApplicationInfo applicationInfo;
try {
applicationInfo = packageManager.getApplicationInfo(packageName, 0);
} catch (final NameNotFoundException e) {}
final String title = (String)((applicationInfo != null) ? packageManager.getApplicationLabel(applicationInfo) : "???");
I want to find from within my broadcast receiver what other activities are currently running. This is the code that i use from an activity to find the other running activites but when i try to use this code in my broadcast receiver i get errors on the following lines:
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
shows the error in eclipse ACTIVITY_SERVICE cannot be resolved to a variable
PackageManager pm = this.getPackageManager();
and this shows the error in eclipse The method getPackageManager() is undefined for the type ScreenReceiver (my broadcast receiver)
Here is the full code:
public void getRunning(){
ActivityManager am = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = this.getPackageManager();
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
runningApplications.add(c.toString());
}catch(Exception e) {
//Name Not Found Exception
}
}
}
Since BroadcastReceiver does not descent from a Context, you cannot use this, as you can do in Activity. You should use the instance of Context that is passed to your onReceive() method.
public void getRunning(Context context){
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = context.getPackageManager();
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
runningApplications.add(c.toString());
}catch(Exception e) {
//Name Not Found Exception
}
}
}
try as:
public class ScreenReceiver extends BroadcastReceiver {
private Context ctext;
#Override
public void onReceive(Context context, Intent intent) {
ctext=context;
//OR you can also pass context as param to getRunning()
//your code here....
}
public void getRunning(){
ActivityManager am = (ActivityManager)ctext.getSystemService(Context.ACTIVITY_SERVICE);
List l = am.getRunningAppProcesses();
Iterator i = l.iterator();
PackageManager pm = ctext.getPackageManager();
while(i.hasNext()) {
ActivityManager.RunningAppProcessInfo info = (ActivityManager.RunningAppProcessInfo)(i.next());
try {
CharSequence c = pm.getApplicationLabel(pm.getApplicationInfo(info.processName, PackageManager.GET_META_DATA));
Log.w("LABEL", c.toString());
runningApplications.add(c.toString());
}catch(Exception e) {
//Name Not Found Exception
}
}
}
}
I want to get the application name from application package name. Somebody please show me how I can get this.
public class AppInstalledListener extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
if(action.equals("android.intent.action.PACKAGE_ADDED")){
Logger.debug("DATA:"+intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REMOVED")){
Logger.debug("DATA:"+intent.getData().toString());
}
if(action.equals("android.intent.action.PACKAGE_REPLACED")){
Logger.debug("DATA:"+intent.getData().toString());
}
}
}
You can use PackageManager class to obtain ApplicationInfo:
final PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo( this.getPackageName(), 0);
} catch (final NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
This would return the application name as defined in <application> tag of its manifest.
Try this
final String packageName = "my.application.package"
PackageManager packageManager= getApplicationContext().getPackageManager();
String appName = (String) packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA));
and replace packageName with your package full name.
you can get packageName using mContext.getPackageName() where mContext = yourActivityName.this for Activity and getActivity() for fragment.
public static String getAppNameFromPkgName(Context context, String Packagename) {
try {
PackageManager packageManager = context.getPackageManager();
ApplicationInfo info = packageManager.getApplicationInfo(Packagename, PackageManager.GET_META_DATA);
String appName = (String) packageManager.getApplicationLabel(info);
return appName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
return "";
}
}
It seems that you are able to receive the event of new package added after that its a very simple concept to get the all relevant information about that package like one such information is the application name so here is the concept
-> your device package manager has all the information related to it so just make an object of that it will give you all the information related with the package name.
-> You should also remember that the intent gives you "package: real_package_name" so first you have to get real name first by spilling(I used) or by any other simple implementation of String
-> Here is the code hope you will get what you want I m also giving information about how you can get app name,app icon,app version,app version code etc.....
public class NewAppReciver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.intent.action.PACKAGE_ADDED")){
String[] a=intent.getData().toString().split(":");
String packageName=a[a.length-1];
List<PackageInfo> packageInfoList = context.getPackageManager().getInstalledPackages(0);
for (int i = 0; i < packageInfoList.size(); i++) {
PackageInfo packageInfo = packageInfoList.get(i);
if(packageInfo.packageName.equals(packageName)){
String appName = packageInfo.applicationInfo.loadLabel(context.getPackageManager()).toString();
String appVersion = packageInfo.versionName;
int appVerCode = packageInfo.versionCode;
Drawable app_icon = packageInfo.applicationInfo.loadIcon(context.getPackageManager());
}
}
}
}
}
But at the time of the application Uninstall you can only get the package name as on Un installation all other information gets removed by the system.
Try this below, it worked for me.
public static ArrayList<ApplicationInfo> getLaunchableInstalledApps(Context context){
final PackageManager pm = context.getPackageManager();
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ArrayList<ApplicationInfo> list = new ArrayList<>();
for (ResolveInfo resolveInfo:pm.queryIntentActivities(intent, PackageManager.GET_META_DATA)){
try {
ApplicationInfo app = context.getPackageManager().getApplicationInfo(resolveInfo.activityInfo.packageName, 0);
list.add(app);
} catch (Exception e) {
e.printStackTrace();
}
}
if (cacheAppsList.isEmpty()){
cacheAppsList.addAll(list);
}
return list;
}
PackageManager pm = getPackageManager();
IntentFilter filter = new IntentFilter();
filter.addAction("android.intent.action.MAIN");
filter.addCategory("android.intent.category.HOME");
filter.addCategory("android.intent.category.DEFAULT");
Context context = getApplicationContext();
ComponentName component = new ComponentName(context.getPackageName(), TestReplaceHomeAppActivity.class.getName());
ComponentName[] components = new ComponentName[] {new ComponentName("com.android.launcher", "com.android.launcher.Launcher"), component};
pm.clearPackagePreferredActivities("com.android.launcher");
pm.addPreferredActivity(filter, IntentFilter.MATCH_CATEGORY_EMPTY, components, component);