How to construct package URI using PackageManager in android - android

I need to construct PackageUri in order to uninstall package using action.delete. I obtained PackageInfo object using PackageMananger. Is there any way to construct PackageUri using PackageInfo?
thanks.

I got it, lets say we have PackageInfo pinfo
String pkg= "package:"+ pinfo.packageName;
Uri pkg_uri= Uri.parse(pkg);
Now we have package uri, to uninstall
Intent intent = new Intent(Intent.ACTION_DELETE, pkg_uri);
startActivity(intent);

Related

How can I get OS default apps in android

I'm developing a launcher app, I need to retrieve an Android OS default Phone app, Browser app and SMS apps', application Info (Application name, Package name, Launcher icon). Following code is used to get all launchable applications.
private static List<ApplicationInfo> getInstalledApps(Context context, PackageManager pm) {
List<ApplicationInfo> installedApps = context.getPackageManager().getInstalledApplications(0);
List<ApplicationInfo> laughableInstalledApps = new ArrayList<>();
for(int i =0; i<installedApps.size(); i++){
if(pm.getLaunchIntentForPackage(installedApps.get(i).packageName) != null){
laughableInstalledApps.add(installedApps.get(i));
}
}
return laughableInstalledApps;
}
After spending some time with the code, I found a way get what I wanted.
Default Dial App
Intent mainIntent = new Intent(Intent.ACTION_DIAL, null);
mainIntent.addCategory(Intent.CATEGORY_DEFAULT);
List<ResolveInfo> pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
ActivityInfo info = pkgAppsList.get(0).activityInfo;
Default SMS App
String smsPkgName = Telephony.Sms.getDefaultSmsPackage(context);
ApplicationInfo info = getPackageManager().getApplicationInfo(smsPkgName, 0);
Default Browser App
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
ResolveInfo resolveInfo = getPackageManager().resolveActivity(browserIntent,
PackageManager.MATCH_DEFAULT_ONLY);
ActivityInfo info = resolveInfo.activityInfo;
Try with PackageManager#getPreferredActivities(java.util.List, java.util.List, java.lang.String), where the first parameter is a list of IntentFilter, which you would like to get default apps for. Then the answer is written in list passed as second parameter.
Here are some common intents for which you might try to find default apps.

How to get a list of android installed apps which accept http: as URI?

i guess packagemanager is the starting point but it seems that it is quite complicated.
i want to list the set of supporting app accepting http: in a list with its launcher icon, clicking it will launch the app.
thank you
The following example will show how to get a list of application that can open HTTP URLs.
String url = "http://www.example.com"; // you can give any url here
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
List<ResolveInfo> resolveInfoList = getPackageManager()
.queryIntentActivities(i, 0);
for (ResolveInfo resolveInfo : resolveInfoList) {
//you will get all information you need from `resolveInfo`
//eg:for package name - resolveInfo.activityInfo.packageName
}

How to uninstall the app from listview in android

Greeting everyone!!
I am creating an android application on it i have to make an option of uninstalling the installed application.
I have listed the installed application using the following code.
Here is my code.
packageManager = getPackageManager();
List<ApplicationInfo> list = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
PACKAGENAME = getApplicationContext().getPackageName();
new LoadApplications().execute();
But i don't know how to uninstall the apps by presenting my code on OnClickListener.
Please give me some suggestion.
String packageNameOfAppToUninstall = "org.abc.test";
Uri packageUri = Uri.parse("package:" + packageNameOfAppToUninstall);
Intent uninstallIntent = new Intent(Intent.ACTION_UNINSTALL_PACKAGE, packageUri);
startActivity(uninstallIntent);

Checking through intent if user has certain messaging app installed android

I am working with this code which Ragu Swaminathan helped me with on my original post found at: How to show both texting and dialer apps with a single Intent on Android?.
final List<Intent> finalIntents = new ArrayList<Intent>();
final Intent textIntent = new Intent(Intent.ACTION_VIEW);
textIntent.setType("text/plain");
textIntent.setData(Uri.parse("sms:"));
final PackageManager packageManager = getActivity().getPackageManager();
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(textIntent, 0);
for (ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(textIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
finalIntents.add(intent);
}
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:121"));
Intent chooserIntent = Intent.createChooser(
callIntent, "Select app to share");
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS, finalIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
I have this code that brings up snackup separating sms and dialer apps installed on users phone and allows them to pick one to either send a message / call a person.
What I want to know is, is it possbile to add another section on this snackbar that checks if the user has whatsapp installed or another specfic app installed.
Also, being new to android, I have tried playing around with the code but ended up messing it up. another thing that I would like to do is create sections like theses;
Sms apps:
.....
.....
.....
Dialer apps:
.....
.....
.....
Whatsapp:
.....
Any help is welcomed, please let me know if my question is not clear.
Edited;
You can use getPackageInfo method
PackageManager pm = getPackageManager();
PackageInfo pi = pm.getPackageInfo(certainAppPackageName, 0);
if (pi != null) {
//app is installed, do smth
}
Google play links exist package names.
For example:
https://play.google.com/store/apps/details?id=org.telegram.messenger
where org.telegram.messenger is a package name

how to find my android application's storing path of apk file

I want to make an application that could send itself (apk file) by bluetooth. but i have trouble with finding the apk file path. i tried this code:
final PackageManager pm = this.getPackageManager();
List<PackageInfo> packages = pm.getInstalledPackages(PackageManager.GET_META_DATA);
String st = null;
for (PackageInfo packageInfo : packages) {
if(packageInfo.packageName.contains("testbutton"))
st=packageInfo.packageName;
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
String uri = "/data/app/";
uri+=st;
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(uri)));
startActivity(intent);
but st returns null value.
please help me with this. thanks in advance
finally i'd found the right answer that works in this purpose, thanks to #Kanak for her help :)
PackageManager pm = getPackageManager();
String uri = null;
for (ApplicationInfo app : pm.getInstalledApplications(0)) {
if(!((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1))
if(!((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1)){
uri=app.sourceDir;
if(uri.contains("com.example.test"))
break;
}
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(uri)));
startActivity(intent);
There is no need to iteration. Getting the application itself APK file uri is as easy as this:
String appUri = getApplicationInfo().publicSourceDir;
Also note that doc says this about publicSourceDir:
Full path to the publicly available parts of sourceDir,
including resources and manifest. This may be different from
sourceDir if an application is forward locked.
And also note that to send an APK file, you need to set the type to application/vnd.android.package-archive instead of image/*
So the complete snippet would be:
String appUri = getApplicationInfo().publicSourceDir;
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("application/vnd.android.package-archive");
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(appUri)));
startActivity(Intent.createChooser(sharingIntent, "Share via"));

Categories

Resources