我有以下代码开始打算拨打电话号码。 如果此代码在没有电话的设备上运行,它会直接运行,而不是抛出让我感到惊讶的异常。 就是它可能,我想知道如何编码,以便如果没有手机,那么它应该尝试找到一个VOIP应用程序。 怎么做?
ImageButton btnPhone = (ImageButton) view.findViewById(R.id.phone_image);
btnPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + employee.getPhone()));
try {
startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(getActivity(), "Can't make phone call", Toast.LENGTH_SHORT).show();
} catch (Exception ex) {
Toast.makeText(getActivity(), "Can't make phone call", Toast.LENGTH_SHORT).show();
}
}
});
编辑:缩回,将提出单独的问题。
答案 0 :(得分:2)
尝试这种方式:
1)测试Android设备的蜂窝无线电模块存在(来自here):
static public int getSDKVersion() {
Class<?> build_versionClass = null;
try {
build_versionClass = android.os.Build.VERSION.class;
} catch (Exception e) {
}
int retval = -1;
try {
retval = (Integer) build_versionClass.getField("SDK_INT").get(build_versionClass);
} catch (Exception e) {
}
if (retval == -1)
retval = 3; //default 1.5
return retval;
}
static public boolean hasTelephony(Context context)
{
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (tm == null)
return false;
//devices below are phones only
if (Utils.getSDKVersion() < 5)
return true;
PackageManager pm = context.getPackageManager();
if (pm == null)
return false;
boolean retval = false;
try
{
Class<?> [] parameters = new Class[1];
parameters[0] = String.class;
Method method = pm.getClass().getMethod("hasSystemFeature", parameters);
Object [] parm = new Object[1];
parm[0] = "android.hardware.telephony";
Object retValue = method.invoke(pm, parm);
if (retValue instanceof Boolean)
retval = ((Boolean) retValue).booleanValue();
else
retval = false;
}
catch (Exception e)
{
retval = false;
}
return retval;
}
与
<uses-feature android:name="android.permission.READ_PHONE_STATE"/>
许可和
<uses-feature android:name="android.hardware.telephony" android:required="false" />
使用功能。
然后,如果设备没有“电话模块”尝试通过所有可能的方式(来自here)呼叫:
public void call(String dialNumber) {
try{
Intent callIntent = new Intent("android.intent.action.CALL_PRIVILEGED");
callIntent.setData(Uri.parse("tel:" + dialNumber));
startActivity(callIntent);
}
catch (Exception e) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + dialNumber));
startActivity(callIntent);
}
}
或者
2)尝试手动找到已安装的VOIP (来自here)
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;
}
3)尝试通过p.2应用程序创建呼叫,例如通过Skype (来自here)
public static void skype(String number, Context ctx) {
try {
//Intent sky = new Intent("android.intent.action.CALL_PRIVILEGED");
//the above line tries to create an intent for which the skype app doesn't supply public api
Intent sky = new Intent("android.intent.action.VIEW");
sky.setData(Uri.parse("skype:" + number));
Log.d("UTILS", "tel:" + number);
ctx.startActivity(sky);
} catch (ActivityNotFoundException e) {
Log.e("SKYPE CALL", "Skype failed", e);
}
}