嘗試這種方式:
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'。請參閱最新的答案。 –
明白了。讓我稍微處理一下。 –