如何在Android中使用意图打电话?

我正在使用以下代码在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)
374586 次浏览

一切都很好。

我只是在清单文件中的应用程序标记之前放置了调用权限标记

现在一切都很正常。

为了避免这种情况,可以使用GUI来输入权限。Eclipse会考虑在哪里插入权限标记,通常情况下不这样做是正确的

你可以使用Intent.ACTION_DIAL代替Intent.ACTION_CALL。这将显示已输入号码的拨号器,但允许用户决定是否实际拨打电话。ACTION_DIAL不需要CALL_PHONE权限。

你也可以用这个:

String uri = "tel:" + posted_by.replaceAll("[^0-9|\\+]", "");

这个演示对你有帮助…

在呼叫按钮上单击:

Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Phone_number"));
startActivity(intent);

舱单许可:

 <uses-permission android:name="android.permission.CALL_PHONE" />

2021年编辑

你应该把它写在你的清单文件,但同时你应该在运行时问。像这样的代码:

if (ContextCompat.checkSelfPermission(this,android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.CALL_PHONE),
REQUEST_CODE)


} else {


// else block means user has already accepted.And make your phone call here.


}

如果你想,你可以覆盖onRequestPermissionsResult给用户更好的体验,如果你写相同的代码与else块在这里,你的用户将不需要再次点击你的按钮,在你给予权限后,它将直接调用。

使用完整代码

          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);

在Android中,对于某些功能,您需要将权限添加到Manifest文件。

  1. 转到项目AndroidManifest.xml
  2. 单击Permissions选项卡
  3. 点击添加
  4. 选择使用权限
  5. 请看下面的快照enter image description here
< p > 6。保存清单文件,然后运行项目。

更优雅的选择:

String phone = "+34666777888";
Intent intent = new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phone, null));
startActivity(intent);
11-25 14:47:01.681: ERROR/AndroidRuntime(302): blah blah...requires android.permission.CALL_PHONE

^答案在于异常输出"requires android.permission.CALL_PHONE":)

在你的意图中使用ACTION_DIAL动作,这样你就不需要任何权限了。您需要ACTION_CALL权限的原因是在用户没有任何操作的情况下拨打电话。

Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0987654321"));
startActivity(intent);

只是简单的联机程序,不需要任何额外的权限:

private void dialContactPhone(final String phoneNumber) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phoneNumber, null)));
}

清单中的请求权限

<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();
}
    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();
}

重要提示:

如果你使用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);

这样做不需要许可。

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();
}
}
 if(ContextCompat.checkSelfPermission(
mContext,android.Manifest.permission.CALL_PHONE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) mContext, new
String[]{android.Manifest.permission.CALL_PHONE}, 0);
} else {
startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "Your Number")));
}

对于使用intent进行调用活动,您应该请求适当的权限。

为此,在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();
}

从拨号器调用(不需要权限):

   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>
// 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)

在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()
}
}

弦机=“9553290143”;;

意图意图=新的意图(意图。ACTION_DIAL, Uri.fromParts("tel", phone, null));

startActivity(意图);