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>
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 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;
}
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 ;
}
}
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;
}
});
How can I find whether a particular package or application, say: com.android.abc, exists on my Android device?
Call any of the below method with the package name.
import android.content.pm.PackageManager;
// ...
public boolean isPackageExisted(String targetPackage){
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
for (ApplicationInfo packageInfo : packages) {
if(packageInfo.packageName.equals(targetPackage))
return true;
}
return false;
}
import android.content.pm.PackageManager;
public boolean isPackageExisted(String targetPackage){
PackageManager pm=getPackageManager();
try {
PackageInfo info=pm.getPackageInfo(targetPackage,PackageManager.GET_META_DATA);
} catch (PackageManager.NameNotFoundException e) {
return false;
}
return true;
}
Without using a try-catch block or iterating through a bunch of packages:
public static boolean isPackageInstalled(Context context, String packageName) {
final PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(packageName);
if (intent == null) {
return false;
}
List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
Kotlin
fun isPackageExist(context: Context, target: String): Boolean {
return context.packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null
}
Edit: Extension Function
fun Context.isPackageExist(target: String): Boolean {
return packageManager.getInstalledApplications(0).find { info -> info.packageName == target } != null
}
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;
We can check like this:
if(getPackageManager().hasSystemFeature("android.software.webview") == true && isPackageExisted("com.google.android.webview")) {
if (Constant.isNetworkConnected(Activity.this)) {
//Your Intent
} else {
Toast.makeText(getApplicationContext(), resources.getString(R.string.internet_error), Toast.LENGTH_SHORT).show();
}
} else
{
Constant.showDialog(Activity.this,"Please install the webview");
}
}
Make method for package check ! this credit goes to "Kavi" https://stackoverflow.com/a/30708227/6209105
public boolean isPackageExisted(String targetPackage) {
List<ApplicationInfo> packages;
PackageManager pm;
pm = getPackageManager();
packages = pm.getInstalledApplications(0);
for (ApplicationInfo packageInfo : packages) {
if(packageInfo.packageName.equals(targetPackage))
{
return true;
}
}
return false;
}
You should use PackageManager's function called getInstalledPackages() to get the list of all installed packages and the search for the one you are interested in. Note that package name is located in PackageInfo.packageName field.
Since some devices have reported that the "getInstalledPackages" can cause TransactionTooLargeException (check here, here and here), I think you should also have a fallback like I did below.
This issue was supposed to be fixed on Android 5.1 (read here), but some still reported about it.
public static List<String> getInstalledPackages(final Context context) {
List<String> result = new ArrayList<>();
final PackageManager pm = context.getPackageManager();
try {
List<PackageInfo> apps = pm.getInstalledPackages(0);
for (PackageInfo packageInfo : apps)
result.add(packageInfo.packageName);
return result;
} catch (Exception ignored) {
//we don't care why it didn't succeed. We'll do it using an alternative way instead
}
// use fallback:
BufferedReader bufferedReader = null;
try {
Process process = Runtime.getRuntime().exec("pm list packages");
bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = bufferedReader.readLine()) != null) {
final String packageName = line.substring(line.indexOf(':') + 1);
result.add(packageName);
}
closeQuietly(bufferedReader);
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
} finally {
closeQuietly(bufferedReader);
}
return result;
}
public static void closeQuietly(final Closeable closeable) {
if (closeable == null)
return;
try {
closeable.close();
} catch (final IOException e) {
}
}
If you just want to use adb:
adb shell "pm list packages"|cut -f 2 -d ":"
it will list all installed packages.
You can use pm.getPackageUid() instead of iterating over the pm.getInstalledApplications()
boolean isPackageInstalled;
PackageManager pm = getPackageManager();
int flags = 0;
try
{
pm.getPackageUid(packageName,flags);
isPackageInstalled = true;
}
catch (final PackageManager.NameNotFoundException nnfe)
{
isPackageInstalled = false;
}
return isPackageInstalled;
According to the Package visibility filtering changes in Android 11, you need to add this permission to your manifest to be able to list installed apps:
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"/>
but Google doesn't recommend to use this way. You should use <queries> tag instead:
<manifest ...>
<queries>
<package android:name="com.app.package" />
...
</queries>
...
</manifest>
And in your code:
fun isAppInstalled(context: Context, packageId: String): Boolean {
return try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
context.packageManager
.getApplicationInfo(packageId, PackageManager.ApplicationInfoFlags.of(0))
} else {
context.packageManager.getApplicationInfo(packageId, 0)
}
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}