Is there a way run an intent from packageinfo? I've been searching and I don't find it.
I tried like that
Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(new ComponentName(p.applicationInfo.packageName,p.applicationInfo.name));
startActivity(i);
but it doesn't work because p.applicationInfo.name is always null.
The following code worked for me using SDK 8.18
Assuming that "p" is your PackageInfo
ApplicationInfo appInfo = p.applicationInfo;
String packageName = appInfo.packageName;
startIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if(startIntent != null){
startActivity(startIntent);
}
Try use folowing code:
Intent i = getPackageManager().p.applicationInfo.packageName(p.applicationInfo.packageName);
startActivity(i);
before start activity you may setup any flags (i.setFlags()), if you need.
Related
Its working properly in every API level, but not working properly on Lolipop(api 21)
Code is here:
Intent internetIntent = new Intent(Intent.ACTION_VIEW, Uri
.parse("http://www.google.com/"));
internetIntent.setComponent(new ComponentName(
"com.android.browser",
"com.android.browser.BrowserActivity"));
internetIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(internetIntent);
Check this code:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://www.google.com/"));
startActivity(intent);
Yes, it wont work in Lollipop, or may not work on any brand Manufactured(Sony,Samsung) phones, because you are trying to open an application with package name com.android.browser
internetIntent.setComponent(new ComponentName(
"com.android.browser",
"com.android.browser.BrowserActivity"));
In Lollipop default browser is chrome, which has package name something else. And there is no application with package com.android.browser in Lollipop.
Same thing can happen to any other Sony/Samsung phones.
What you can do is either call for a Application Chooser Dialog or find the default browser application, and open it
1) Code for Application chooser:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://"));
startActivity(intent);
2) Code to Find default app and launch it:
ComponentName cn=null;
Intent browserIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://"));
ResolveInfo resolveInfo = packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
if (resolveInfo.activityInfo.packageName.equals("android")) {
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo info : resolveInfos) {
if (!info.activityInfo.packageName.equals("android")) {
cn = new ComponentName(info.activityInfo.packageName, info.activityInfo.name);
}
}
if(cn==null)
cn = new ComponentName("com.android.browser", "com.android.browser.BrowserActivity");
} else {
cn = new ComponentName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name);
}
if(cn!=null){
Intent openIntent = new Intent();
openIntent.setComponent(cn);
openIntent.setData(Uri.parse("http://www.google.com/"));
startActivity(openIntent);
}
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.
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");
How can i call an intent, to start the Downloads application, that is native in the Android OS.
I have searched a lot for this , and this is as close as I got:
Intent i = new Intent();
ComponentName comp = new ComponentName("com.sec.android.providers.downloads","com.sec.android.providers.downloads.DownloadActivity");
i.setComponent(comp);
i.setAction("android.intent.action.VIEW");
self.startActivity(i);
I have also tried:
Intent i = new Intent();
PackageManager manager = getActivity().getPackageManager();
i = manager.getLaunchIntentForPackage("com.sec.android.providers.downloads");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
but it gives me a JavaNullPointerException at: i.addCategory(Intent.CATEGORY_LAUNCHER);
Intent i = new Intent();
i.setAction(Intent.ACTION_VIEW);
Uri uri= Uri.fromFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS));
i.setData(uri);
startActivity(i);
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);