Deleting android apps from activity - android

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();
}
}

Related

Remove application from other application

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.

Remove shortcuts from home screen

I am trying to remove all shortcuts from all screens in android. I succeed it once but somehow it stopped working after next execute. Could I ask you to help me find out what would be the problem? There is not stack, there is no error, just I click and nothing happens. When I first managed to success I got many toasts describing that "X was removed".
Here is my code:
private void method0() {
final Intent anyIntent = new Intent(Intent.ACTION_MAIN, null);
List<ResolveInfo> pkgAppsList = this.getPackageManager().queryIntentActivities(anyIntent, 0);
for (final ResolveInfo resolveInfo : pkgAppsList) {
method(resolveInfo);
}
}
private void method(ResolveInfo resolveInfo) {
try {
final PackageManager pm = getPackageManager();
String packageName = ((ActivityInfo) resolveInfo.activityInfo).packageName;
String appName = (String) resolveInfo.loadLabel(pm);
ComponentName componentName = new ComponentName(resolveInfo.activityInfo.applicationInfo.packageName, resolveInfo.activityInfo.name);
if (packageName.startsWith("com.google") || (packageName.startsWith("com.android")))
return;
final Intent shortcutIntent = new Intent(Intent.ACTION_MAIN);
shortcutIntent.setComponent(new ComponentName(packageName, componentName.getClassName()));
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, appName);
intent.setComponent(new ComponentName(packageName, componentName.getClassName()));
intent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
sendBroadcast(intent, null);
} catch (Exception e) {
e.printStackTrace();
}
}
I added also permission
<uses-permission android:name="com.android.launcher.permission.UNINSTALL_SHORTCUT" />
I am using standard launcher for Nexus 4 - original image ROM from google site.
Looks like you are not using default Stock android launcher.
Please check this example.Hope that helps.

Android launch Twitter intent

I used below code for launching Twitter through intent but it's not working. I have twitter app installed on my phone.
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Content to share");
PackageManager pm = contexto.getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name)) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
contexto.startActivity(shareIntent);
break;
}
}
Getting exception when I try to calling the activity:
android.content.ActivityNotFoundException: Unable to find explicit
activity class {com.twitter.android/com.twitter.android.PostActivity};
have you declared this activity in your AndroidManifest.xml?
Typically for launching a user's feed
Intent intent = null;
try {
// get the Twitter app if possible
this.getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=USERID"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} catch (Exception e) {
// no Twitter app, revert to browser
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/USERID_OR_PROFILENAME"));
}
this.startActivity(intent);
For Post Intent
Intent tweetIntent = new Intent(Intent.ACTION_SEND);
tweetIntent.putExtra(Intent.EXTRA_TEXT, "This is a Test.");
tweetIntent.setType("text/plain");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(tweetIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for(ResolveInfo resolveInfo: resolvedInfoList){
if(resolveInfo.activityInfo.packageName.startsWith("com.twitter.android")){
tweetIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name );
resolved = true;
break;
}
}
if(resolved){
startActivity(tweetIntent);
}else{
Toast.makeText(this, "Twitter app isn't found", Toast.LENGTH_LONG).show();
}
Slightly corrected (thanks to Taranfx) an user's feed intent (change user_id=>screen_name):
public static void startTwitter(Context context) {
Intent intent = null;
try {
// get the Twitter app if possible
context.getPackageManager().getPackageInfo("com.twitter.android", 0);
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?screen_name=<place_user_name_here>"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
return intent;
} catch (Exception e) {
// no Twitter app, revert to browser
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/<place_user_name_here>"));
}
startActivity(intent);
}
I was using the com.twitter.android.composer.ComposerActivity to post text and images since 2016. But started to receive crash reports from users some time ago:
Fatal Exception: android.content.ActivityNotFoundException Unable to
find explicit activity class
{com.twitter.android/com.twitter.android.composer.ComposerActivity};
have you declared this activity in your AndroidManifest.xml?
The issue was caused by renaming
com.twitter.android.composer.ComposerActivity
to
com.twitter.composer.ComposerActivity
inside Twitter app.
The issue was resolved since I changed activity name to com.twitter.composer.ComposerActivity.
And I use the following code to post images with text to Twitter:
ShareCompat.IntentBuilder.from(activity)
.setText(getTextToShare())
.setStream(getImageUriToShare())
.setType("image/png")
.getIntent().addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
.setClassName("com.twitter.android", "com.twitter.composer.ComposerActivity");

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>();
}

Android launch browser without specifying a URL

How would one go about launching the browser from an activity without specifying a url. I would like to open the browser so the user can continue browsing without changing the page they were on?
SOLUTION:
Answer below was correct and worked, but to be more specific for future readers, here is the working code:
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setAction("com.android.browser");
ComponentName comp = new ComponentName("com.android.browser", "com.android.browser.BrowserActivity");
i.setComponent(comp);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Thanks!
Use Intent#setComponent() to set the Browser's package and class name. Then start the activity.
In case it (the ComponentName("com.android.browser", "com.android.browser.BrowserActivity")) changes in the future, you may try something like the following code:
public static ComponentName getDefaultBrowserComponent(Context context) {
Intent i = new Intent()
.setAction(Intent.ACTION_VIEW)
.setData(new Uri.Builder()
.scheme("http")
.authority("x.y.z")
.appendQueryParameter("q", "x")
.build()
);
PackageManager pm = context.getPackageManager();
ResolveInfo default_ri = pm.resolveActivity(i, 0); // may be a chooser
ResolveInfo browser_ri = null;
List<ResolveInfo> rList = pm.queryIntentActivities(i, 0);
for (ResolveInfo ri : rList) {
if (ri.activityInfo.packageName.equals(default_ri.activityInfo.packageName)
&& ri.activityInfo.name.equals(default_ri.activityInfo.name)
) {
return ri2cn(default_ri);
} else if ("com.android.browser".equals(ri.activityInfo.packageName)) {
browser_ri = ri;
}
}
if (browser_ri != null) {
return ri2cn(browser_ri);
} else if (rList.size() > 0) {
return ri2cn(rList.get(0));
} else if (default_ri == null) {
return null;
} else {
return ri2cn(default_ri);
}
}
private static ComponentName ri2cn(ResolveInfo ri) {
return new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name);
}
Basically, here I construct an intent to view a dummy http page, get the list of activities that may handle the intent, compare it to the default handler returned by resolveActivity() and return something. I do not need to check if there's a launcher MAIN action (my code uses the VIEW action), but you probably should.
this answer may help. from How to open the default android browser without specifying an URL?
PackageManager pm = getPackageManager();
Intent queryIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
ActivityInfo af = queryIntent.resolveActivityInfo(pm, 0);
Intent launchIntent = new Intent(Intent.ACTION_MAIN);
launchIntent.setClassName(af.packageName, af.name);
startActivity(launchIntent);

Categories

Resources