Nim*_*a G 4 android android-intent
我的程序中有以下代码:
public static void callPhoneNumber(Context context, String clientPhoneNum) {
if (isCallingSupported(context)) {
Intent i = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + clientPhoneNum));
context.startActivity(i);
} else {
final AlertDialog alertDialog =
new AlertDialog.Builder(context).setMessage(context.getString(R.string.error))
.setMessage(context.getString(R.string.no_call_functionality))
.setPositiveButton(context.getString(R.string.ok),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.create();
alertDialog.show();
}
}
private static boolean isCallingSupported(Context context) {
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return (telephonyManager.getPhoneType() != TelephonyManager.PHONE_TYPE_NONE);
}
Run Code Online (Sandbox Code Playgroud)
我想知道是否isCallingSupported()有必要呢?我不记得为什么我这样写它,但现在当我正在审查我认为用户可能只是使用他的Skype或其他VOIP应用程序拨打号码.我是否应该进行任何其他检查,或者这个意图是否安全没有isCallingSupported()(我的意思是安全,即使用户的平板电脑没有呼叫功能,也没有其他应用程序可以处理呼叫,意图不会导致崩溃)?
从这个问题:
PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
//Then there is application can handle your intent
}else{
//No Application can handle your intent
}
Run Code Online (Sandbox Code Playgroud)
首先检查是否有任何应用已注册此意图.如果有,请使用它.如果没有,请显示您的对话框.
您最终只需使用上面的代码替换isCallingSupported函数:
private static boolean isCallingSupported(Context context) {
boolean result = true;
PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() <= 0) {
result = false;
}
return result;
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
1671 次 |
| 最近记录: |