How to start another installed app in Android from my application? - android

Hello guys I am new to android core apps but I love to do animation stuffs in Android. But what I want is I want to start another another app using my application. For eg. I want to start facebook app using my application.
I dont know if the question is understood or not, but really need a clue on how to do such stuffs.
Thank you!!

Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.package.address");
startActivity(launchIntent);
In your case, you should check this apps package names and replace them.
It wouldn't be a bad idea to check if it's installed first :
private boolean isPackageInstalled(String packagename, Context context) {
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}

check android's developer document about: Intent
refer to Context:
startActivity
startService
sendBroadcast

Related

Application cannot retrieve the package name in signed releases

Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage(getBaseContext().getPackageName());
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
After changing the language from inside the application, when I use this code in an unsigned APK then it will work fine.
But when I generate signed APK, the code above does not work.
Please help me with this.
There is no specific error provided, but my answer is based on the assumption that PackageManager.NameNotFoundException() is being thrown.
I cannot see the location of the code either, but I'm generally cautious about using getBaseContext(). You should be using getApplicationContext() to get the package information.
The app can get its own application ID like this:
Log.i(TAG, BuildConfig.APPLICATION_ID);
By default when you create an app in Android Studio, the package name and the application ID are identical. You could use this method if you are struggling for a solution.
Edit:
BuildConfig.APPLICATION_ID is now deprecated. Use the following instead:
BuildConfig.LIBRARY_PACKAGE_NAME
But most likely the problem can be solved by using the correct Context with the PackageManager rather than using BuildConfig.

How to open youtube applications home activity from my android application?

I have an android application. I just need to open youtube application home activity from my android application. Please note I do not want to open any specific video or channel. Just want to open application. I have gone through this and this but all are for opening any video or channel.
Please suggest me if anyone knows this.
Note: Application is running on TV and android version is 4.4
for opening any app form your device you have to know the package name of that app......
By firing an intent of that package name you can open an app form your app but if intent does not get a package name than no intent is their for handle that intent so take care of it...
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("any package name you want to open");
startActivity( LaunchIntent );
In your case it is youtube so package name is :
com.google.android.youtube
open app using package name youtube package name is com.google.android.youtube so you can use below intent code
try {
Intent LaunchIntent = packageManager.getLaunchIntentForPackage("com.google.android.youtube");
startActivity(LaunchIntent);
} catch (Exception e) {
e.printStackTrace();
}
just surround with try catch if in case package not found to get package name of any app use this Application on Google Play
same code work for Android TV
Actually the package name for youtube app in TV was not same as that in phones.
the package name for youtube app on TV is "com.google.android.youtube.googletv" so following code worked for me.
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.google.android.youtube.googletv");
startActivity( LaunchIntent );
This will work on a device but not the emulator
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));
Try this :
public static void openInstalledApp(Context context, String bundle_id) {
PackageManager pm = context.getPackageManager();
Intent appStartIntent = pm.getLaunchIntentForPackage(bundle_id);
if (null != appStartIntent) {
context.startActivity(appStartIntent);
}
}
And use it :
openInstalledApp(<your_context>,"com.google.android.youtube");
Where in second param you can pass any bundleID which is installed in your device and you wants to open it.

How to check if calendar app installed in android?

I have added some buttons to my app in order to create intents for calendar events. But I think it would be better to check if the user has or not any calendar app. How could I do this?
Calendar intents look like:
Intent calIntent = new Intent(Intent.ACTION_INSERT);
calIntent.setType("vnd.android.cursor.item/event");
calIntent.putExtra(Events.TITLE, "My House Party");
calIntent.putExtra(Events.EVENT_LOCATION, "My Beach House");
calIntent.putExtra(Events.DESCRIPTION, "A Pig Roast on the Beach");
GregorianCalendar calDate = new GregorianCalendar(2012, 7, 15);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
calDate.getTimeInMillis());
calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
calDate.getTimeInMillis());
startActivity(calIntent);
In section 3.2.3.1 of the Android Compatibility Definition Document, it says
The Android upstream project defines a number of core applications, such as contacts, calendar, photo gallery, music player, and
so on. Device implementers MAY replace these applications with alternative versions.
However, any such alternative versions MUST honor the same Intent patterns provided by the upstream project. For example, if a
device contains an alternative music player, it must still honor the Intent pattern issued by third-party applications to pick a song.
The document goes on to list all core applications:
Desk Clock
Browser
Calendar
Contacts
Gallery
GlobalSearch
Launcher
Music
Settings
This basically means that all devices with the Google Play Store will have a calendar app (as well as all the apps listed above) installed that honour the standard intent patterns. Therefore, you don't need to check at all.
As MusicMaster said, all Android devices should have a Calendar application. However if you're making an intent that you're not sure will be honored by any activities, you can query the package manager using your intent to see if there are any activities able to respond to it. Like this:
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(yourIntent, 0);
If the size of activities is greater than zero, the user has an app that can respond to your Intent.
How to check if an app is installed in android?
This is my method:
public static boolean isPackageInstalled(String packagename, Context context) {
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (NameNotFoundException e) {
return false;
}
}
for example, if the app exists then start an intent:
if(isPackageInstalled("com.android.calendar", getApplicationContext())){
Intent i = new Intent();
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage(packageName);
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
}else{
Log.i("myApp", "Application NOT Installed! :( ");
}

How to call activity of one project from activity of another project in android?Also vice versa?

I am doing an integration project,which involves integrating two projects into one.How I want to do this is,I have a common project,the activity of this common project should be able to call activities of the other two projects,as per different events like a particular button press,etc.How can I do this?Is it possible through intents?
Also,the activities of the other two projects should be able to call each other.How to do this?
this Android Developer blog post explains how to make custom application intents available to other applications for this sort of integration:
http://android-developers.blogspot.com/2009/11/integrating-application-with-intents.html
You will have to use intent filters, Sample code below
PackageManager packageManager = getPackageManager();
Intent baseIntent = new Intent(ACTION_PICK_PLUGIN);
baseIntent.addCategory("matching.catagory");
List<ResolveInfo> activities = packageManager.queryIntentActivities(baseIntent, PackageManager.GET_RESOLVED_FILTER);
Then fire intent using the following,
Intent baseIntent = new Intent(activities.get(indexOfChild).filter.getAction(0));
baseIntent.addCategory(activities.get(indexOfChild).filter.getCategory(0));
baseIntent.setComponent(newComponentName(activities.get(indexOfChild).activityInfo.packageName,activities.get(indexOfChild).activityInfo.name));
startActivity(baseIntent);
I hope it helps..
If you want to call the MainActivity of a project from the existing project and vice versa you can use PackageManager class
Intent i;
PackageManager manager = getPackageManager();
try {
i = manager.getLaunchIntentForPackage("Target package");
if (i == null)
throw new PackageManager.NameNotFoundException();
i.addCategory(Intent.ACTION_VIEW );
startActivity(i);
} catch (PackageManager.NameNotFoundException e) {
}

How can I check if the Android Market is installed on my user's device?

How could I write a code which can tell me that android market is installed on your android phone?
There are two ways. You can use the already mentioned getPackageManager() and getApplicationInfo() (if the package is not found, a PacketManager.NameNotFoundException will be thrown - see here). Android Market's package name is com.android.vending.
However, you can also create a dummy intent for searching the market and check how it is handled. If the resulting list has at least one entry, you can be sure that Android Market is installed:
Intent market = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=dummy"));
PackageManager manager = getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(mmarket, 0);
Here's what I did (assumes browser exists):
Intent market = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("market://details?id=com.example.app"));
Intent website = new Intent(Intent.ACTION_VIEW).setData(Uri
.parse("http://play.google.com/store/apps/details?id=com.example.app"));
try {
startActivity(market);
} catch (ActivityNotFoundException e) {
startActivity(website);
}

Categories

Resources