Reputation: 341
I have an application... on button click in my application i need to check whether a particular app is installed in the phone or not..If the application is not installed it should redirect to play store.
How can i do that please help?
public class Example extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Put the package name here...
boolean installed = appInstalledOrNot("com.Ch.Example.pack");
if(installed) {
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.Ch.Example.pack");
startActivity(LaunchIntent);
System.out.println("App already installed on your phone");
}
else {
System.out.println("App is not installed on your phone");
}
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed = false;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed ;
}
}
Does the above example work for checking whether an app is installed or not my my application.
Upvotes: 0
Views: 168
Reputation: 573
To check whether an app is installed in the device use following method and params
GooglePlayStorePackageNameOld
& GooglePlayStorePackageNameNew
both holds the package names of apps
private static final String GooglePlayStorePackageNameOld = "com.google.market";
private static final String GooglePlayStorePackageNameNew = "com.android.vending";
use and modify the following method accordingly to check if the app is presently installed in the device or not
public boolean isAppInstalled() {
PackageManager packageManager = getApplication().getPackageManager();
List<PackageInfo> packages = packageManager
.getInstalledPackages(PackageManager.GET_UNINSTALLED_PACKAGES);
for (PackageInfo packageInfo : packages) {
if (packageInfo.packageName.equals(GooglePlayStorePackageNameOld)
|| packageInfo.packageName
.equals(GooglePlayStorePackageNameNew)) {
googlePlayStoreInstalled = true;
break;
} else {
googlePlayStoreInstalled = false;
}
}
return googlePlayStoreInstalled;
}
and to navigate user to a particular Play Store link use the following code
final String appPackageName = getPackageName(); // getPackageName() from Context or Activity object
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
}
EDIT According to your code
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.Ch.Example.pack");
startActivity(LaunchIntent);
will launch the app which has the package name com.Ch.Example.pack
Upvotes: 2
Reputation: 28519
The way you check if the app is installed is a standard way. To redirect to playStore you can use the following snippet
private goToPlayStore(String packageName) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + packageName))
startActivity(intent);
}
also with respect to the comment, you should check first if the market is installed, the package name should be com.android.vending. This code will throw an exception if market is not available.
Upvotes: 0