尝试这种方式:
1),用于蜂窝无线模块存在测试机器人设备(从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)尽量使呼叫经创立于第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);
}
}
什么是'Hub'(从'Hub.MainContext.getSystemService()'? –
它只是用于'Context'。请参阅最新的答案。 –
明白了。让我稍微处理一下。 –