First of all, I searching for my question many times in Google but I didn't find what I want.
My Question is: Is there any way to open application in Android by "label name" where on the other hand the package name is "knowing" ?
My approach that I used to do this is by creating two ArrayList and store the package name and label name in each one, then, I matching the label name with package name after I converted them to lowercase in order to be the same letters to see if there are matching, if matching , then I launch the application by getting its package name.
the problem that I faced it, the package name sometimes didn't have or matching the same name in label name, based on the application developer what he had write the package name.
for example: if I want to open gmail , based on my method I cannot because the package name is com.google.android.gm and label name is gmail , I apologized to the prolongation ,thanks in advanced.
My code:
private void getAllApps() {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> activities = getPackageManager()
.queryIntentActivities(mainIntent, 0);
for (ResolveInfo resolveInfo : activities) {
appsName.add(resolveInfo.loadLabel(getPackageManager()).toString()
.toLowerCase());
pkgsName.add(resolveInfo.activityInfo.packageName.toString());
}
}
private void openApplication(String appName) {
String packageName = null, appNameLowerCase = null, pkgNameLowerCase = null;
// matching the package name with label name
if (appsName.contains(appName)) {
appNameLowerCase = appName.trim().replace(" ", "");
for (int i = 0; i < pkgsName.size(); i++) {
pkgNameLowerCase = pkgsName.get(i).trim().toLowerCase();
if (pkgNameLowerCase
.matches("(.*)" + appNameLowerCase + "(.*)")) {
packageName = pkgsName.get(i);
break;
}
}
}
// to launch the application
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage(packageName);
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
}
thanks for all replies, i solve it by #CommonsWare solution and it works correctly :
class Applications {
private String packageName;
private String labelName;
}
private void getAllApps() {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> activities = getPackageManager()
.queryIntentActivities(mainIntent, 0);
for (ResolveInfo resolveInfo : activities) {
Applications applications = new Applications();
applications.labelName = resolveInfo.loadLabel(getPackageManager())
.toString().toLowerCase();
applications.packageName = resolveInfo.activityInfo.packageName
.toString();
applicationsArrayList.add(applications);
}
}
private void openApplication(String appName) {
String packageName = null;
// matching the package name with label name
for (int i = 0; i < applicationsArrayList.size(); i++) {
if (applicationsArrayList.get(i).labelName.trim().equals(
appName.trim())) {
packageName = applicationsArrayList.get(i).packageName;
break;
}
}
// to launch the application
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage(packageName);
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}
}
Try this
public List<ApplicationInfo> getApplicationList(Context con){
PackageManager p = con.getPackageManager();
List<ApplicationInfo> info = p.getInstalledApplications(0);
String example = info.get(0).packageName.toString();
return info;
}
Related
I have this code that when I copy some text in clipboard this program paste text and application name automatically in the text box.I want paste app's name that running/on top
myClipboard.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
#Override
public void onPrimaryClipChanged()
{
PackageManager pm = getApplicationContext().getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(getPackageName(), 0);
} catch (final PackageManager.NameNotFoundException e) {
ai = null;
}
final String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
ClipData cp = myClipboard.getPrimaryClip();
ClipData.Item item = cp.getItemAt(0);
String text1 = item.getText().toString();
pastetext.setText(text1+"---"+applicationName);
}
});
To get the list of activities/applications installed on Android :
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
To check all running applications
ActivityManager actvityManager = (ActivityManager)
this.getSystemService( ACTIVITY_SERVICE );
List<RunningAppProcessInfo> procInfos = actvityManager.getRunningAppProcesses();
If you want to fetch the package name of your app or currently opened app in android, than refer this http://shortcutsandroid.blogspot.in/2015/02/how-to-get-package-name-in-android.html
Hope this helps...
My app has a widget with two buttons that goes back and forth and displays information stored in a JSONArray. It works very well but the problem is when i close the app the widget stops working because the JSONArray becomes null. How can i keep it working even when the app is closed ?
its too long to post it here but this is the part when i click next button
if(intent.getAction().equals(NEXT_LISTING)){
try{
if (listingNumber !=15)
{
listingNumber++;
}
JSONObject latestListing = jsonListings.getJSONObject(listingNumber);
String titleString = latestListing.getString("title");
String descriptionString = latestListing.getString("description");
String categoryString = latestListing.getString("category");
Date postDate = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").parse(latestListing.getString("created"));
Date newDate = new Date(postDate.getTime() + Utils.getCurrentTimezoneOffset() * (3600 * 100));
String createdDate = Utils.getFormattedDate(newDate);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.njoftime_widget);
views.setTextViewText(R.id.title, titleString);
views.setTextViewText(R.id.description, descriptionString);
views.setTextViewText(R.id.textView_category, categoryString);
views.setTextViewText(R.id.textView_date, createdDate);
for(int i=0; i < categoriesList.size();i++)
{
if(categoriesList.get(i).get(0).equals(latestListing.getString("category")))
{
views.setImageViewResource(R.id.category_icon_widget, iconList.get(i));
}
}
if(listingThumbnails.get(listingNumber) != null)
{
views.setViewVisibility(R.id.listing_photo, View.VISIBLE);
views.setImageViewUri(R.id.listing_photo, Uri.parse(""));
Bitmap roundedBitmap = getRoundedLeftCornerBitmap(listingThumbnails.get(listingNumber), 10);
views.setImageViewBitmap(R.id.listing_photo,roundedBitmap);
}
else
{
views.setViewVisibility(R.id.listing_photo, View.GONE);
}
// Instruct the widget manager to update the widget
ComponentName componentName = new ComponentName(context, NjoftimeWidget.class);
AppWidgetManager.getInstance(context).updateAppWidget(componentName, views);
}
catch (Exception e)
{
e.printStackTrace();
}
}
My Problem:
I want to get all installed applications with the Intent Category Category.LAUNCHER.
So I want to get all apps that would be displayed in the Launcher
and then resolve the package and the name.
Such as package: "com.android.phone" -- name: "Phone"
My code basically works, but i think, no i know this can be made easier:
final PackageManager pm = getPackageManager();
Intent main = new Intent(Intent.ACTION_MAIN, null);
main.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> packages = pm.queryIntentActivities(main, 0);
for (ResolveInfo resolve_info : packages)
{
try
{
//THIS TWO
String package_name = resolve_info.activityInfo.packageName;
String app_name = (String)pm.getApplicationLabel(pm.getApplicationInfo(package_name, PackageManager.GET_META_DATA));
Log.i("TEST", "package = <" + package_name + "> name = <" + app_name + ">");
}
catch(Exception e)
{
//package not found -- should never happen
}
}
So is there a better / faster or easier way?
final PackageManager pm = getActivity().getPackageManager();
final IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_LAUNCHER);
List<IntentFilter> outFilters = new ArrayList<IntentFilter>();
outFilters.add(filter);
List<ComponentName> outActivities = new ArrayList<ComponentName>();
pm.getPreferredActivities(outFilters, outActivities, null);
Intent startupIntent = new Intent(Intent.ACTION_MAIN);
startupIntent.addCategory(Intent.CATEGORY_LAUNCHER);
PackageManager pm = this.getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(startupIntent, 0);
Log.i(TAG, "Found " + activities.size() + " activities.");
in MainActivity which extends AppCompatActivity or Activity
I want to start manage application (Settings -> Application -> manage application -> Application info) screen programmatically. I'm unable to do it. Can anyone please help me?
Thanks in advance.
as per this link
In Android 2.3, you can use startActivity() on an ACTION_APPLICATION_DETAILS_SETTINGS Intent, with a proper Uri, to bring up your app's "manage" screen
or
private static final String SCHEME = "package";
private static final String APP_PKG_NAME_21 = "com.android.settings.ApplicationPkgName";
private static final String APP_PKG_NAME_22 = "pkg";
private static final String APP_DETAILS_PACKAGE_NAME = "com.android.settings";
private static final String APP_DETAILS_CLASS_NAME = "com.android.settings.InstalledAppDetails";
public static void showInstalledAppDetails(Context context, String packageName) {
Intent intent = new Intent();
final int apiLevel = Build.VERSION.SDK_INT;
if (apiLevel >= 9) { // above 2.3
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts(SCHEME, packageName, null);
intent.setData(uri);
} else { // below 2.3
final String appPkgName = (apiLevel == 8 ? APP_PKG_NAME_22
: APP_PKG_NAME_21);
intent.setAction(Intent.ACTION_VIEW);
intent.setClassName(APP_DETAILS_PACKAGE_NAME,
APP_DETAILS_CLASS_NAME);
intent.putExtra(appPkgName, packageName);
}
context.startActivity(intent);
}
From API Level 9 (Android 2.3) you can start an Intent with android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS. Thus:
packageName = "your.package.name.here"
try {
//Open the specific App Info page:
Intent intent = new Intent(android.provider.Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + packageName));
startActivity(intent);
} catch ( ActivityNotFoundException e ) {
//e.printStackTrace();
//Open the generic Apps page:
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_APPLICATIONS_SETTINGS);
startActivity(intent);
}
I'm trying to create programmability a home shortcut for any installed application.
Considering that the only thing I would have available would the application name, let's say for example the Calculator com.android.calculator2 (1.5).
I'm using current code bellow, shortcut is created successfully but activity cannot be launched from shortcut (wrong activity class name I guess) and also sometimes the icon seems to be corrupted.
Also add com.android.launcher.permission.INSTALL_SHORTCUT to manifest.xml.
Is there a optimal way to achieved this?
String appName = "com.android.calculator2";
Context newAppContext = null;
// Get other package context
try {
newAppContext =
context.createPackageContext(appName, Context.CONTEXT_IGNORE_SECURITY);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
// Create shortcut
if(newAppContext != null) {
// Get Application Name
PackageManager pm = context.getPackageManager();
ApplicationInfo ai;
try {
ai = pm.getApplicationInfo(appName, 0);
} catch (final NameNotFoundException e) {
ai = null;
}
// Get application label
String applicationName = (String) (ai != null ? pm.getApplicationLabel(ai) : "(unknown)");
// Shortcut intent
Intent shortcutIntent = new Intent (Intent.ACTION_MAIN);
/** Problem in here **
shortcutIntent.setClassName(newAppContext, newAppContext.getClass().getName());
*********************/
shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Create intent
final Intent putShortCutIntent = new Intent();
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, applicationName);
putShortCutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(newAppContext,
R.drawable.icon));
putShortCutIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
// Broadcast intent
context.sendBroadcast(putShortCutIntent);
}
EDIT:
Managed to achieve this by getting the Intent from PackageManager.getLaunchIntentForPackage(String packageName).
So:
// Intent shortcutIntent = new Intent (Intent.ACTION_MAIN);
// shortcutIntent.setClassName(newAppContext, newAppContext.getClass().getName());
// shortcutIntent.addCategory(Intent.CATEGORY_LAUNCHER);
// shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent shorcutIntent = pm.getLaunchIntentForPackage(appName);
You will get "wrong activity class name", when it cannot find the main activity class in the package. It needs that info so that it can launch the correct Activity in the correct package. So in your example, it should be:
shortcutIntent.setClassName("com.android.calculator2", "ClassName");
I don't know what the "ClassName" should be for the Calculator application(maybe you can check its source code), but it should be something like "com.android.calculator2.MainActivity"
EDIT:
Ok, it seems it is possible to get the "ClassName" dynamically:
PackageManager packageManager = context.getPackageManager();
ResolveInfo info = packageManager.resolveActivity(shortcutIntent, 0);
if(info != null) {
shortcutIntent.setClassName(info.activityInfo.packageName, info.activityInfo.name);
}