Remove application from other application - android

I try to remove application from another application in android but When I run below code I got a this error "The application was not found in the list of installed applications" how can I solve this problem furthermore try to remove application has a many package which can I use?
Uri packageURI = Uri.parse("package:com.example.anke");
Intent uninstallIntent = new Intent(Intent.ACTION_DELETE, packageURI);
startActivity(uninstallIntent);

try below code and see your application's package name in your device and use this package name which you want to remove application.
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = getApplicationContext().getPackageManager().queryIntentActivities( mainIntent, 0);

You can check for whether particular application is installed or not like below
private boolean isApplicationInstalled(String packagename, Context context) {
PackageManager packageManager = context.getPackageManager();
try {
packageManager.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
If you get true go for uninstall, if false then application is not installed.

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.

Deleting android apps from activity

Good day!
Are there some tools in Android SDK, that i can use to remove application from activity. In particular, i need activity method, that removes other application with the same app-name, but other package.
If you mean with "same name app" to app with same label that define in XML menifest as the label of your app, this snippest should work:
private void deleteAppByActivityName(#NonNull String myAppLabel,#NonNull Context context){
try {
PackageManager pm = context.getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> dataInDevice = pm.queryIntentActivities(mainIntent, 0);
for (ResolveInfo resolveInfo : dataInDevice){
String label = resolveInfo.loadLabel(pm).toString();
if (label.equals(myAppLabel)) { //we find app with same name as ours
Intent intent = new Intent(Intent.ACTION_DELETE);
intent.setData(Uri.parse("package:" + resolveInfo.activityInfo.packageName));
context.startActivity(intent);
break;
}
}
}catch (ActivityNotFoundException e){
e.printStackTrace();
}
}

How can detect an application is uninstallable?

I'm going to uninstall an application when user click on a button. with this code:
Uri packageURI = Uri.parse("package:"
+ pkNames[position]);
Intent uninstallIntent = new Intent(
Intent.ACTION_DELETE, packageURI);
context.startActivity(uninstallIntent);
but some application don't uninstallable. like Setting or Music or ... when i am going to uninstall these applications i see : uninstall not successful.
I get my packages with this code :
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
packages = pm.queryIntentActivities(mainIntent, 0);
How can i detect an application is uninstallable or not?
You should check if the application you are trying to uninstall is "system" by looking into ApplicationInfo.flags. System application have ApplicationInfo.FLAG_SYSTEM bit set.
Here is a little piece of code:
boolean isSystem(ApplicationInfo info) {
return (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
}
Check documentation for ApplicationInfo class for other useful flags.

Get Preferred/Default app on Android

I am trying to get the default/preferred application for a given Intent. For example, when the user installs a second web browser, then attempts to open a URL, he or she will get a dialog like this:
If the user then selects the Use by default for this action option, then the dialog box no longer opens when a URL is pressed.
I am working on an application that should be aware of what this default or preferred app/action is. How do I do this? I am currently using the code below, but getPreferredPackage doesn't return anything:
Intent i = (new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
PackageManager pm = context.getPackageManager();
final List<ResolveInfo> list = pm.queryIntentActivities(i, 0);
IntentFilter ifilter = new IntentFilter(i.getAction());
if (i.getCategories() != null) {
for(String category : i.getCategories()) {
ifilter.addCategory(category);
}
}
List<IntentFilter> filters = new ArrayList<IntentFilter>();
filters.add(ifilter);
List<ComponentName> preferredActivities = new ArrayList<ComponentName>();
pm.getPreferredActivities(filters, preferredActivities, null);
for (ComponentName activity : preferredActivities) {
for (ResolveInfo rinfo : list) {
if (rinfo.activityInfo.applicationInfo.packageName.equals(activity.getPackageName())) {
try {
final PackageInfo pi = pm.getPackageInfo(activity.getPackageName(), 0);
Toast.makeText(context, pm.getApplicationLabel(pi.applicationInfo), Toast.LENGTH_SHORT).show();
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
What am I doing wrong? Is this even the right approach?
Well, the solution turned out to be much simpler than I made it (although this is very poorly documented). The following code is my solution:
Intent i = (new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
PackageManager pm = context.getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(i, 0);
Toast.makeText(context, pm.getApplicationLabel(mInfo.activityInfo.applicationInfo), Toast.LENGTH_LONG).show();
The method launchUrlInDefaultBrowser below launches a URL without displaying any selection query for the user. First, it tries to find the user's default browser app and launch the URL with it. Second, if there was no default app, it lists all the capable activities for launching the URL and picks up the first one. In case an activity was launched, the method returns true; otherwise, false.
boolean launchUrlInDefaultBrowser(Context context, String url) {
final Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
browserIntent.setData(Uri.parse(url));
// 1: Try to find the default browser and launch the URL with it
final ResolveInfo defaultResolution = context.getPackageManager().resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (defaultResolution != null) {
final ActivityInfo activity = defaultResolution.activityInfo;
if (!activity.name.equals("com.android.internal.app.ResolverActivity")) {
browserIntent.setClassName(activity.applicationInfo.packageName, activity.name);
context.startActivity(launchIntent);
return true;
}
}
// 2: Try to find anything that we can launch the URL with. Pick up the first one that can.
final List<ResolveInfo> resolveInfoList = context.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (!resolveInfoList.isEmpty()) {
browserIntent.setClassName(resolveInfoList.get(0).activityInfo.packageName, resolveInfoList.get(0).activityInfo.name);
context.startActivity(browserIntent);
return true;
}
return false;
}
Beware, the OEMs may have their own ResolverActivity implementation. E.g. Huawei has com.huawei.android.internal.app.HwResolverActivity.
In Kitkat AOSP, getPreferredPackages() always returns empty list. Source code as below
public List<PackageInfo> getPreferredPackages(int flags) {
return new ArrayList<PackageInfo>();
}

How to get manifest permissions of any installed Android app

Is it possible to get the manifest permissions of any installed Android application?
Thanks for the hint,got it running with:
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0);
for (Object obj : pkgAppsList) {
ResolveInfo resolveInfo = (ResolveInfo) obj;
PackageInfo packageInfo = null;
try {
packageInfo = getPackageManager().getPackageInfo(resolveInfo.activityInfo.packageName, PackageManager.GET_PERMISSIONS);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] requestedPermissions = packageInfo.requestedPermissions;
}
Get installed packages using this.
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
And use the package name to get the info.
http://developer.android.com/reference/android/content/pm/PackageInfo.html
If you have apk file it is very simple just open cmd from start menu and write this command line:
aapt d permissions D:\dina\app-debug.apk
take care for the path you write then you can get list from all permissions in this application and package name also like this:
package: com.example.dinasaif.facebookintegration
uses-permission: android.permission.INTERNET

Categories

Resources