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 ;
}
}
Related
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
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);
I want to check that whether whatsapp is installed in mobile or not if installed then show toast "installed" and if not installed then show Toast "Not Installed".How can I do that Kindly help.
You can use this code. It will check if package is installed.
public class Example extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Put the package name here...
if(isAppInstalled("com.whatsapp")) {
System.out.println("App is already installed on your phone");
} else {
System.out.println("App is not currently installed on your phone");
}
}
private boolean isAppInstalled(String packageName) {
try {
getPackageManager().getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
return true;
}
catch (PackageManager.NameNotFoundException ignored) {
return false;
}
}
}
based on #eliasz-kubala answer this will work for me on Android 11 after only adding this to Manifest
<manifest
...>
<queries> <package android:name="com.whatsapp" /> </queries>
<application ....>
</application>
</manifest>
and Then use this Kotlin function
private fun isAppInstalled(packageName: String): Boolean {
val pm: PackageManager = getPackageManager()
return try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}
This is the code to get all package names of installed applications in device
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
after getting list of packages search for com.whatsapp(package name of whats app given on official webiste Whatsapp). Thats it..
Try this.
Here you need to pass package name as uri.
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
Check condition like this.
if(!appInstalledOrNot("com.whatsapp")){
// Toast message not installed.
}else{
// Toast message installed.
}
Try like this:
public class WhatsApp_Check extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
boolean installed = appInstalledOrNot("whatsapp_package_name");
if(installed) {
//print whatsApp is already installed on your phone
else{
// print whatsApp is not currently installed on your phone
}
}
private boolean appInstalledOrNot(String uri) {
PackageManager pm = getPackageManager();
boolean app_installed;
try {
pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
}
Try this method:
private void checkWhatsapp() {
String packageName = "com.whatsapp";
String mesgToShare = "Hey, I am searching for Whatsapp in your device.";
boolean gotPackage = false;
Intent shareIntent = new Intent( android.content.Intent.ACTION_SEND );
shareIntent.setType( "text/plain" );
shareIntent.putExtra( android.content.Intent.EXTRA_TEXT, mesgToShare );
List<ResolveInfo> activityList = getPackageManager().queryIntentActivities( shareIntent, 0 );
for ( final ResolveInfo app : activityList ) {
if ( (app.activityInfo.name).contains( packageName ) ) {
gotPackage = true;
final ActivityInfo activity = app.activityInfo;
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 );
startActivity( shareIntent );
break; // We already found what we were looking for. Don't need to execute the rest of the Loop
}
}
if ( !gotPackage )
Log.e("TAG", "Whatsapp is not installed in your device");
}
if (isAppInstalled("com.whatsapp")) {
val sendIntent = Intent("android.intent.action.MAIN")
sendIntent.component = ComponentName("com.whatsapp", "com.whatsapp.Conversation")
sendIntent.putExtra(
"jid",
PhoneNumberUtils.stripSeparators("918219243720").toString() + "#s.whatsapp.net"
)
startActivity(sendIntent)
} else {
showToast("App is not currently installed on your phone")
}
Now Create fun()
private fun isAppInstalled(packageName: String): Boolean {
return try {
packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
true
} catch (ignored: PackageManager.NameNotFoundException) {
false
}
}
Now Add same lines in Manifest file
<queries>
<package android:name="com.whatsapp" />
<package android:name="com.facebook.katana" />
</queries>
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;
}
}
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;
}
});