我正在使用以下代码在Android中进行调用,但它给了我安全例外,请帮忙。
posted_by = "111-333-222-4";
String uri = "tel:" + posted_by.trim() ;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
权限
<uses-permission android:name="android.permission.CALL_PHONE" />
异常
11-25 14:47:01.661: ERROR/AndroidRuntime(302): Uncaught handler: thread main exiting due to uncaught exception
11-25 14:47:01.681: ERROR/AndroidRuntime(302): java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.CALL dat=tel:111-333-222-4 cmp=com.android.phone/.OutgoingCallBroadcaster } from ProcessRecord{43d32508 302:com.Finditnear/10026} (pid=302, uid=10026) requires android.permission.CALL_PHONE
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.os.Parcel.readException(Parcel.java:1218)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.os.Parcel.readException(Parcel.java:1206)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1214)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1373)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.app.Activity.startActivityForResult(Activity.java:2749)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.app.Activity.startActivity(Activity.java:2855)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at com.Finditnear.PostDetail$2$1$1$1.onClick(PostDetail.java:604)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:884)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.widget.ListView.performItemClick(ListView.java:3285)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): at android.widget.AbsListView$PerformClick.run(AbsListView.java:1640)
答案 0 :(得分:351)
您可以使用Intent.ACTION_DIAL
代替Intent.ACTION_CALL
。这会显示已输入号码的拨号器,但允许用户决定是否实际拨打电话。 ACTION_DIAL
不需要CALL_PHONE
权限。
答案 1 :(得分:208)
此演示对您有帮助......
通话时按钮点击:
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
startActivity(intent);
清单许可:
<uses-permission android:name="android.permission.CALL_PHONE" />
答案 2 :(得分:143)
更优雅的选择:
String phone = "+34666777888";
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
startActivity(intent);
答案 3 :(得分:68)
一切都很好。
我刚刚在清单文件中的应用程序标记之前放置了调用权限标记
现在一切都运转正常。
答案 4 :(得分:52)
在您的意图中使用操作ACTION_DIAL,这样您就不会需要任何权限。您需要ACTION_CALL权限的原因是在没有用户任何操作的情况下拨打电话。
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0987654321"));
startActivity(intent);
答案 5 :(得分:22)
只需简单的oneliner,无需任何其他权限:
private void dialContactPhone(final String phoneNumber) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phoneNumber, null)));
}
答案 6 :(得分:21)
如果您使用Intent.ACTION_CALL
,则必须添加CALL_PHONE
权限。
只有当您不希望自己的应用在谷歌播放中显示不能使用SIM卡或没有使用GSM的平板电脑时,它才能实现。
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + Constants.CALL_CENTER_NUMBER));
startActivity(callIntent);
<uses-permission android:name="android.permission.CALL_PHONE" />
因此,如果它不是您应用的关键功能,请尽量不要添加CALL_PHONE
权限。
是用屏幕上显示的号码显示手机应用程序,因此用户只需点击通话按钮:
Intent dialIntent = new Intent(Intent.ACTION_DIAL);
dialIntent.setData(Uri.parse("tel:" + Constants.CALL_CENTER_NUMBER));
startActivity(dialIntent);
无需许可。
答案 7 :(得分:15)
使用此完整代码
Intent callIntent = new Intent(Intent.ACTION_DIAL);
callIntent.setData(Uri.parse("tel:"+Uri.encode(PhoneNum.trim())));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
答案 8 :(得分:11)
清单中的请求权限
<uses-permission android:name="android.permission.CALL_PHONE" />
致电使用此代码
Intent in = new Intent(Intent.ACTION_CALL, Uri.parse("tel:99xxxxxxxx"));
try {
startActivity(in);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(mcontext, "Could not find an activity to place the call.", Toast.LENGTH_SHORT).show();
}
答案 9 :(得分:8)
权限:
<uses-permission android:name="android.permission.CALL_PHONE" />
意图:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:0377778888"));
startActivity(callIntent);
答案 10 :(得分:5)
你也可以使用它:
String uri = "tel:" + posted_by.replaceAll("[^0-9|\\+]", "");
答案 11 :(得分:5)
AndroidManifest.xml中的权限
<uses-permission android:name="android.permission.CALL_PHONE" />
完整代码:
private void onCallBtnClick(){
if (Build.VERSION.SDK_INT < 23) {
phoneCall();
}else {
if (ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
phoneCall();
}else {
final String[] PERMISSIONS_STORAGE = {Manifest.permission.CALL_PHONE};
//Asking request Permissions
ActivityCompat.requestPermissions(mActivity, PERMISSIONS_STORAGE, 9);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
boolean permissionGranted = false;
switch(requestCode){
case 9:
permissionGranted = grantResults[0]== PackageManager.PERMISSION_GRANTED;
break;
}
if(permissionGranted){
phoneCall();
}else {
Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();
}
}
private void phoneCall(){
if (ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:12345678900"));
mActivity.startActivity(callIntent);
}else{
Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();
}
}
答案 12 :(得分:4)
要使用意图进行呼叫活动,应请求适当的权限。
为此,您在AndroidManifest.xml文件中包括使用权限。
<uses-permission android:name="android.permission.CALL_PHONE" />
然后在您的活动中添加以下代码
if (ActivityCompat.checkSelfPermission(mActivity,
Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
//Creating intents for making a call
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
mActivity.startActivity(callIntent);
}else{
Toast.makeText(mActivity, "You don't assign permission.", Toast.LENGTH_SHORT).show();
}
答案 13 :(得分:3)
为避免这种情况,可以使用GUI输入权限。 Eclipse负责插入权限标记的位置,更常见的是不正确
答案 14 :(得分:1)
final public void Call(View view){
try {
EditText editt=(EditText)findViewById(R.id.ed1);
String str=editt.getText().toString();
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"+str));
startActivity(intent);
}
catch (android.content.ActivityNotFoundException e){
Toast.makeText(getApplicationContext(),"App failed",Toast.LENGTH_LONG).show();
}
答案 15 :(得分:1)
11-25 14:47:01.681: ERROR/AndroidRuntime(302): blah blah...requires android.permission.CALL_PHONE
^答案在于异常输出&#34; requires android.permission.CALL_PHONE
&#34; :)
答案 16 :(得分:1)
Collections.sort(myList);
答案 17 :(得分:1)
在Android中,您需要为Manifest文件添加权限。
6.保存清单文件,然后运行您的项目。 您的项目现在应该按预期运行。
答案 18 :(得分:1)
// Java
String mobileNumber = "99XXXXXXXX";
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL); // Action for what intent called for
intent.setData(Uri.parse("tel: " + mobileNumber)); // Data with intent respective action on intent
startActivity(intent);
// Kotlin
val mobileNumber = "99XXXXXXXX"
val intent = Intent()
intent.action = Intent.ACTION_DIAL // Action for what intent called for
intent.data = Uri.parse("tel: $mobileNumber") // Data with intent respective action on intent
startActivity(intent)
答案 19 :(得分:1)
在 Kotlin 中使用此代码
fun makeCall(context: Context, mob: String) {
try {
val intent = Intent(Intent.ACTION_DIAL)
intent.data = Uri.parse("tel:$mob")
context.startActivity(intent)
} catch (e: java.lang.Exception) {
Toast.makeText(context,
"Unable to call at this time", Toast.LENGTH_SHORT).show()
}
}
答案 20 :(得分:0)
用于通过拨号程序拨打电话(无需许可):
fun callFromDailer(mContext: Context, number: String) {
try {
val callIntent = Intent(Intent.ACTION_DIAL)
callIntent.data = Uri.parse("tel:$number")
mContext.startActivity(callIntent)
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(mContext, "No SIM Found", Toast.LENGTH_LONG).show()
}
}
从应用程序直接调用(需要权限):
fun callDirect(mContext: Context, number: String) {
try {
val callIntent = Intent(Intent.ACTION_CALL)
callIntent.data = Uri.parse("tel:$number")
mContext.startActivity(callIntent)
} catch (e: SecurityException) {
Toast.makeText(mContext, "Need call permission", Toast.LENGTH_LONG).show()
} catch (e: Exception) {
e.printStackTrace()
Toast.makeText(mContext, "No SIM Found", Toast.LENGTH_LONG).show()
}
}
权限:
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>