在我的应用中,我需要在某些情况下以编程方式启动呼叫。
它大部分时间都有效,但我可以看到,有时它不会,特别是2.3.7版本。也许该特定权限被删除并且在该版本中是非法的? 我无法找到关于异常起源的线索。
我在Manifest中获得了许可:
<uses-permission android:name="android.permission.CALL_PHONE" />
java代码:
private void launchCall(String number, int calltype) {
Intent callIntent = new Intent();
callIntent.setAction(Intent.ACTION_CALL);
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setData(Uri.parse("tel:"+ number));
if (calltype == CALL_TYPE_VIDEO) {
callIntent.setAction(ACTION_VIDEO_CALL);
callIntent.putExtra("videocall", true);
}
context.startActivity(callIntent);
}
异常追踪:
java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:xxx-xxx-xxxx flg=0x10000000 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{415f7158 4016:com.tawkon/10092} (pid=4016, uid=10092) requires android.permission.CALL_PHONE
at android.os.Parcel.readException(Parcel.java:1322)
at android.os.Parcel.readException(Parcel.java:1276)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1359)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
at android.app.ContextImpl.startActivity(ContextImpl.java:640)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:258)
at com.tawkon.Service.PrecallSuggestionManager.launchCall(PrecallSuggestionManager.java:305)
at com.tawkon.Service.PrecallSuggestionManager.access$12(PrecallSuggestionManager.java:291)
at com.tawkon.Service.PrecallSuggestionManager$6.run(PrecallSuggestionManager.java:215)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3703)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我遇到了同样的问题,我发现许可并未位于正确的位置!将其复制到您的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shalom.itai.automaticdailer" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
以下解决方案适用于我。
的AndroidManifest.xml
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.CALL_PHONE" />
activity_main.xml中
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/phoneNumberEditTextId"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="make phone call"
android:onClick="makePhoneCall"/>
MainActivity.java
private static final int MY_PERMISSIONS_REQUEST_CALL_PHONE = 1;
public void makePhoneCall(View view) {
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSIONS_REQUEST_CALL_PHONE);
} else {
callPhone();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CALL_PHONE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
callPhone();
}
}
}
}
private void callPhone() {
EditText editText = (EditText) findViewById(R.id.phoneNumberEditTextId);
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + editText.getText().toString()));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
}
}