PackageManager can't check for installed apps - android

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

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

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

Android Application ID

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

How to check if Facebook is installed Android

I am modifying my app to be able to catch if a user tries to publish without having the facebook app installed (required for SSO). Here is the code I am using:
try{
ApplicationInfo info = getPackageManager().
getApplicationInfo("com.facebook.android", 0 );
return true;
} catch( PackageManager.NameNotFoundException e ){
return false;
}
The problem is, it is always catching an error. According to the question here, I need to request the appropriate permission but I don't know what permissions I need to request.
Is my problem a permission one or something else?
com.facebook.android is the package name for the Facebook SDK. The Facebook app's package is com.facebook.katana.
To check whether or not an app is installed on Android use this method:
public static boolean isPackageInstalled(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;
}
In your case use any of these packages:
com.facebook.orca
com.facebook.katana
com.example.facebook
com.facebook.android
boolean hasPackage = isPackageInstalled(MainActivity.this, "com.facebook.katana");
For Kotlin
fun isPackageInstalled(packageName: String, context: Context): Boolean {
return try {
val packageManager = context.packageManager
packageManager.getPackageInfo(packageName, 0)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}
if (isAppInstalled()) {
Toast.makeText(getApplicationContext(), "facebook app already installed", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "facebook app not installing", Toast.LENGTH_SHORT).show();
}
public boolean isAppInstalled() {
try {
getApplicationContext().getPackageManager().getApplicationInfo("com.facebook.katana", 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
Write the function in Utilities or anywhere suit for you.This will function will help you to check any app installed or not.let me say for myself it is in Utilities.java
public static boolean isAppInstalled(Context context, String packageName) {
try {
context.getPackageManager().getApplicationInfo(packageName, 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
Then, Call this function from anywhere. for eg to check facebook app
if(Utilities.isAppInstalled(getApplicationContext(), "com.facebook.katana")) {
// Do something
}else {
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.facebook.katana"));
startActivity(i);
}
Enjoy
You can check it for all Facebook Apps that any of Facebook apps are installed or not .
For supporting OS level 11 we need to add this in AndrodiManifest.xml to avoid package name not found exception -
<manifest ...
<queries>
<package android:name="com.facebook.katana" />
<package android:name="com.facebook.lite" />
<package android:name="com.facebook.android" />
<package android:name="com.example.facebook" />
</queries>
<application .....
Then add this method to you code -
public static String isFacebookAppInstalled(Context context){
if(context!=null) {
PackageManager pm=context.getPackageManager();
ApplicationInfo applicationInfo;
//First check that if the main app of facebook is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.katana", 0);
return applicationInfo.enabled?"com.facebook.katana":"";
} catch (Exception ignored) {
}
//Then check that if the facebook lite is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.lite", 0);
return applicationInfo.enabled?"com.facebook.lite":"";
} catch (Exception ignored) {
}
//Then check the other facebook app using different package name is installed or not
try {
applicationInfo = pm.getApplicationInfo("com.facebook.android", 0);
return applicationInfo.enabled?"com.facebook.android":"";
} catch (Exception ignored) {
}
try {
applicationInfo = pm.getApplicationInfo("com.example.facebook", 0);
return applicationInfo.enabled?"com.example.facebook":"";
} catch (Exception ignored) {
}
}
return "";
}
And then launch the app -
if (!TextUtils.isEmpty(isFacebookAppInstalled(context))) {
/* Facebook App is installed,So launch it.
It will return you installed facebook app's package
name which will be useful to launch the app */
Uri uri = Uri.parse("fb://facewebmodal/f?href=" + yourURL);
Intent intent = context.getPackageManager().getLaunchIntentForPackage(isFacebookAppInstalled(context);
if (intent != null) {
intent.setAction(Intent.ACTION_VIEW);
intent.setData(uri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
else {
Intent intentForOtherApp = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(intentForOtherApp);
}
}
Best Approach is to pick the package name including com.facebook but anyway you may use following packages:
com.facebook.orca
com.facebook.katana
com.example.facebook
com.facebook.android
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.facebook.katana"));
startActivity(i);
this code worked for me
if (isAppInstalled()) {
Toast.makeText(getApplicationContext(), "facebook app already installed", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "facebook app not installing", Toast.LENGTH_SHORT).show();
}
public boolean isAppInstalled() {
try {
getApplicationContext().getPackageManager().getApplicationInfo("com.facebook.katana", 0);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.e("tag","url override url = "+ url);
if( url.startsWith("http:") || url.startsWith("https:") ) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
return true;
}
});

Categories

Resources