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...
Related
I tried this code.
public static void openGallery(Context context) {
String bucketId = "";
final String[] projection = new String[] {"DISTINCT " + MediaStore.Images.Media.BUCKET_DISPLAY_NAME + ", " + MediaStore.Images.Media.BUCKET_ID};
final Cursor cur = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
while (cur != null && cur.moveToNext()) {
final String bucketName = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME)));
if (bucketName.equals("Your_dir_name")) {
bucketId = cur.getString((cur.getColumnIndex(MediaStore.Images.ImageColumns.BUCKET_ID)));
break;
}
}
Uri mediaUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
if (bucketId.length() > 0) {
mediaUri = mediaUri.buildUpon()
.authority("media")
.appendQueryParameter("bucketId", bucketId)
.build();
}
if(cur != null){
cur.close();
}
Intent intent = new Intent(Intent.ACTION_VIEW, mediaUri);
context.startActivity(intent);
}
But I failed to open the gallery specific folder, but the gallery home screen appears with 'file not supported' Toast.
Is there any way I can solve this problem?
You can't set initial path to specific folder when working with files stored in MediaStore.
However, you can use DocumentsContract.EXTRA_INITIAL_URI when working with DocumentFile, so you can set initial path when user wants to select files from Storage Access Framework (SAF):
Uri initialUri = DocumentFileCompat.createDocumentUri(DocumentFileCompat.PRIMARY, "DCIM")
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
.putExtra(DocumentsContract.EXTRA_INITIAL_URI, initialUri)
DocumentFileCompat.createDocumentUri() helps you constructing the initial URI. You can get DocumentFileCompat from SimpleStorage.
i want to get from another app on device an activity icon with the highest possible resolution, but cant figure out how to do it......
In my app i can select one activity from a list of all activities available on the device and the correspondig intent will return to onActivityResult. There i can get the activities icon like this:
Drawable icon = null;
ComponentName componentName = intent.getComponent();
String packageName = componentName.getPackageName();
PackageManager pm = this.getPackageManager();
icon = pm.getActivityIcon(intent);
i can then get the app-icon with the highest resolution like this:
PackageInfo pi = pm.getPackageInfo(packageName, 0);
Context otherAppCtx = this.createPackageContext(packageName, Context.CONTEXT_IGNORE_SECURITY);
int displayMetrics[] = { DisplayMetrics.DENSITY_TV, DisplayMetrics.DENSITY_HIGH, DisplayMetrics.DENSITY_XHIGH, DisplayMetrics.DENSITY_XXXHIGH };
for (int displayMetric : displayMetrics)
{
try
{
Drawable d = otherAppCtx.getResources().getDrawableForDensity(pi.applicationInfo.icon, displayMetric);
if (d != null)
{
icon = d;
}
}
catch (Resources.NotFoundException e)
{
continue;
}
}
Thats nice but i want to get the activity icon and not the app icon with the highest resolution. Does anyone know how to do this?
thx in advance!
You're on the right track with it, you just need to use ActivityInfo instead of PackageInfo. e.g. where you have:
Drawable icon = null;
ComponentName componentName = intent.getComponent();
String packageName = componentName.getPackageName();
PackageManager pm = this.getPackageManager();
icon = pm.getActivityIcon(intent);
Instead of getting the drawable, get the icon resource (if one is set):
ComponentName componentName = intent.getComponent();
ActivityInfo ai = this.getPackageManager().getActivityInfo(componentName, 0);
int iconResId = ai.icon;
Then you should be able to use your second method to resolve it by density.
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;
}
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'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);
}