Android Application ID - android

Can anyone say how to get an application id of market application installed in device. I want to get ID of particular installed application?

boolean installed = appInstalledOrNot(Your App Package as String);
if(installed){
////////App is Installed
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}

Related

Android check if facebook is preinstalled or not?

I have added one code to check if Facebook installed or not, in normal cases it is working but when facebook comes by default on some devices it is not working, it says package not found. can anyone help me here?
public Boolean checkFbInstalled() {
PackageManager pm = getPackageManager();
boolean flag = false;
try {
pm.getPackageInfo("com.facebook.katana", PackageManager.GET_ACTIVITIES);
flag = true;
} catch (PackageManager.NameNotFoundException e) {
flag = false;
}
if (flag == false) {
try {
pm.getPackageInfo("com.facebook.lite", PackageManager.GET_ACTIVITIES);
flag = true;
} catch (PackageManager.NameNotFoundException e) {
flag = false;
}
}
if (flag == false) {
try {
pm.getPackageGids("com.facebook.katana");
flag = true;
} catch (PackageManager.NameNotFoundException e) {
flag = false;
}
}
return flag;
}
Check if the facebook package name exist with try-catch:
try{
ApplicationInfo info = getPackageManager().
getApplicationInfo("com.facebook.katana", 0 );
return true;
}catch( PackageManager.NameNotFoundException e ){
return false;
}
Note: If you like to see if the Facebook SDK exist and not the Facebook app you need to change the package name on the getApplicationInfo() method from com.facebook.katana to com.facebook.android

PackageManager can't check for installed apps

I'm currently building an app that has Facebook and Instagram integration and I need to check whether the app has been installed on the user's device or not.
I've tried out :
private boolean appInstalledOrNot(String uri)
{
PackageManager pm = fragment.getContext().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 ;
}
And I've passed in "com.instagram.android" and "com.facebook.katana" (Facebook) as the URI for the package name.
My Code :
binding.facebook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
boolean haveApp = appInstalledOrNot("com.facebook.katana");
if (haveApp) {
LoginManager.getInstance().logInWithReadPermissions(SocialMediaSharingFragment.this, Arrays.asList("public_profile", "email"));
} else {
Toast.makeText(getContext(), "App not installed", Toast.LENGTH_SHORT).show();
}
}
});
I've deleted the Facebook App from my phone and ran the code, but somehow I'm still getting a true returned from the appInstalledOrNot method.
Did the same for Instagram as well, still doesn't work. I'm using DataBinding as you can see and I'm using it on a Fragment, not an Activity.
Would really appreciate help on this.
Thanks for reading...
Try to use another flag:
pm.getPackageInfo(uri, 0);

getPackageManager Non-Static Error

I want to check an app is Installed on user device or not my class is extends fragment so I have to use Context.getPackageManager() instead of getPackageManager() when I try I got a Non-static error what is a solution in this case?
here is my function code:
private boolean appInstalledOrNot(String uri) {
PackageManager pm = Context.getPackageManager();
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
}
return false;
}
You should use getActivity().getPackageManager()

Android - check if Facebook app exists returns wrong result

I'm opening links received in notifications in FB native app if it's installed on the app and in the browser otherwise, using this code:
PackageManager packageManager = context.getPackageManager();
try {
packageManager.getPackageInfo("com.facebook.katana", PackageManager.GET_ACTIVITIES);
return context.getString(R.string.fb_app_prefix) + fb_url;
} catch (PackageManager.NameNotFoundException e) {
return context.getString(R.string.fb_site_prefix) + fb_url; //normal web mUrl
}
It works on most devices (including the emulator), but in some of them it doesn't throw an error although the app isn't installed.
What's wrong with my code?
I can add the following code for every link I have but not sure it's "healthy":
Intent testIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(fb_app_url));
if (testIntent.resolveActivity(packageManager) != null) {
return fb_app_url;
}
public static boolean isPackageExisted(Context c, String targetPackage) {
PackageManager pm = c.getPackageManager();
try {
PackageInfo info = pm.getPackageInfo(targetPackage,
PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
return false;
}
return true;
}

Find out if app is installed

I think the question says it all: What is the best way to find out if the user has installed Facebook or Whatsapp on his phone? Do I have to go over the package or what is the best way for this?
This was question was answered here. You can using the following piece of code to check for the package name
com.facebook.android OR com.facebook.katana
Code:
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.facebook.android");
if(installed)
{
//This intent will help you to launch if the package is already installed
Intent LaunchIntent = getPackageManager()
.getLaunchIntentForPackage("com.facebook.android");
startActivity(LaunchIntent);
System.out.println("App already installed om your phone");
}
else
{
System.out.println("App is not installed om 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 ;
}
}

Categories

Resources