[英]How to check Whatsapp is installed in device in android?
我想檢查 whatsapp 是否安裝在移動設備中,如果安裝則顯示 toast“已安裝”,如果未安裝則顯示 Toast“未安裝”。我該怎么做請幫忙。
您可以使用此代碼。 它將檢查是否安裝了軟件包。
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.whatsapp");
if(installed) {
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) {
PackageManager pm = getPackageManager();
boolean app_installed;
try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
app_installed = true;
}
catch (PackageManager.NameNotFoundException e) {
app_installed = false;
}
return app_installed;
}
}
這是獲取設備中已安裝應用程序的所有包名的代碼
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List pkgAppsList = context.getPackageManager().queryIntentActivities( mainIntent, 0);
獲得軟件包列表后搜索com.whatsapp(最新的包名的應用程序在官方網站給出的WhatsApp )。 就是這樣..
試試這個。
在這里,您需要將包名稱作為 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;
}
像這樣檢查條件。
if(!appInstalledOrNot("com.whatsapp")){
// Toast message not installed.
}else{
// Toast message installed.
}
像這樣嘗試:
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;
}
}
試試這個方法:
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");
}
基於@eliasz-kubala 的回答,這將在 Android 11 上為我工作,只需將其添加到 Manifest
<manifest
...>
<queries> <package android:name="com.whatsapp" /> </queries>
<application ....>
</application>
</manifest>
然后使用這個 Kotlin 函數
private fun isAppInstalled(packageName: String): Boolean {
val pm: PackageManager = getPackageManager()
return try {
pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
true
} catch (e: PackageManager.NameNotFoundException) {
false
}
}
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")
}
現在創建 fun()
private fun isAppInstalled(packageName: String): Boolean {
return try {
packageManager.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES)
true
} catch (ignored: PackageManager.NameNotFoundException) {
false
}
}
現在在清單文件中添加相同的行
<queries>
<package android:name="com.whatsapp" />
<package android:name="com.facebook.katana" />
</queries>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.