我需要检查我的拨号器应用是否是默认拨号器应用。目前我正在使用此代码
private boolean isDefault() {
Intent i=new Intent(Intent.ACTION_DIAL);
i.addCategory(Intent.CATEGORY_DEFAULT);
ResolveInfo info=getPackageManager().resolveActivity(i,0);
CharSequence name=getPackageManager().getApplicationLabel(info.activityInfo.applicationInfo);
Log.d(TAG, "isDefault: "+name);
return name==getString(R.string.app_name);
}
但name
始终是“Android系统”
对你的帮助表示感谢。感谢
答案 0 :(得分:2)
我能够使用TelecomManager
来解决我的问题,这是我的代码
private boolean isDefault() {
TelecomManager manger= (TelecomManager) getSystemService(TELECOM_SERVICE);
String name=manger.getDefaultDialerPackage();
Log.d(TAG, "isDefault: "+name);
return name.equals(getPackageName());
}
答案 1 :(得分:0)
这是一种安全的方法,适用于所有Android版本:
fun isDefaultDialer(context: Context, packageNameToCheck: String = context.packageName): Boolean {
val dialingIntent = Intent(Intent.ACTION_DIAL).addCategory(Intent.CATEGORY_DEFAULT)
val resolveInfoList = packageManager.queryIntentActivities(dialingIntent, 0)
if (resolveInfoList.size != 1)
return false
return packageNameToCheck == resolveInfoList[0].activityInfo.packageName
}
样品用量:
Log.d("AppLog", "is current app the default dialer?${isDefaultDialer(this)}")
Log.d("AppLog", "is Google Dialer the the default dialer?${isDefaultDialer(this,"com.google.android.dialer")}")
答案 2 :(得分:0)
Telecom Manager的默认拨号程序包可从API级别23获得。
private fun getDefaultDialerApp() : String?{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val manager = applicationContext.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return manager.defaultDialerPackage;
}
}
return null //Change it based on your requirement.
}