Android应用程序呼叫一个号码的按钮点击

1l5u6lss  于 2023-03-11  发布在  Android
关注(0)|答案(7)|浏览(151)

我想拨打文本框中给出的号码,但收到错误消息:
应用程序“xyz”(进程com.android)已意外停止
下面是我的代码,哪里出错了?

EditText txtPhn;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Button callButton = (Button)findViewById(R.id.btnCall);
    txtPhn = (EditText)findViewById(R.id.txtPhnNumber);
    callButton.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            try {
                    Intent callIntent = new Intent(Intent.ACTION_CALL);
                    callIntent.setData(Uri.parse("tel:"+txtPhn.getText().toString()));
                    startActivity(callIntent);
                } catch (ActivityNotFoundException activityException) {
                    Log.e("Calling a Phone Number", "Call failed", activityException);
                }
        }
    });
}

已编辑日志类别

03-09 11:23:25.874: ERROR/AndroidRuntime(370): FATAL EXCEPTION: main
03-09 11:23:25.874: ERROR/AndroidRuntime(370): 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{40738d70 370:org.krish.android/10034} (pid=370, uid=10034) requires android.permission.CALL_PHONE
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.os.Parcel.readException(Parcel.java:1322)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.os.Parcel.readException(Parcel.java:1276)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1351)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1374)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.app.Activity.startActivityForResult(Activity.java:2827)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.app.Activity.startActivity(Activity.java:2933)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at org.krish.android.caller$1.onClick(caller.java:29)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.view.View.performClick(View.java:2485)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.view.View$PerformClick.run(View.java:9080)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.os.Handler.handleCallback(Handler.java:587)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.os.Looper.loop(Looper.java:123)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at java.lang.reflect.Method.invokeNative(Native Method)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at    java.lang.reflect.Method.invoke(Method.java:507)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-09 11:23:25.874: ERROR/AndroidRuntime(370):     at dalvik.system.NativeStart.main(Native Method)`enter code here`
xggvc2p6

xggvc2p61#

确保已将<uses-permission ... />标记添加到AndroidManifest.xml文件中的正确级别(在<application ... />标记的 * 外部 *,但在<manifest ... />标记内部):

<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dbm.pkg"
    android:versionCode="1"
    android:versionName="1.0">

    <!-- NOTE! Your uses-permission must be outside the "application" tag
               but within the "manifest" tag. -->

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

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name">

        <!-- Insert your other stuff here -->

    </application>

    <uses-sdk android:minSdkVersion="9" />
</manifest>
disbfnqx

disbfnqx2#

呼叫/开始呼叫有两种意图:ACTION_CALLACTION_DIAL
ACTION_DIAL只会打开填有号码的拨号器,但允许用户实际呼叫或拒绝呼叫。ACTION_CALL将立即呼叫该号码,并需要额外的权限。
所以一定要确保你有权限

uses-permission android:name="android.permission.CALL_PHONE"

在您AndroidManifest.xml

ddrv8njm

ddrv8njm3#

问题是您没有向用户请求权限。如果设备运行的是Android 6.0(API级别23),并且应用的targetSdkVersion为23或更高版本,则调用权限将被视为危险权限。因此,您必须首先获得正确的权限。首先,添加

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.android.app.myapp" >
      <uses-permission android:name="android.permission.CALL_PHONE" />
      ... 
  </manifest>

到你的AndroidManifest.xml文件中
然后,我有一个方法是通过点击按钮来调用的。

public void onCall(View view) {
          Intent callIntent = new Intent(Intent.ACTION_CALL); //use ACTION_CALL class
          callIntent.setData(Uri.parse("tel:0800000000"));    //this is the phone number calling
          //check permission
          //If the device is running Android 6.0 (API level 23) and the app's targetSdkVersion is 23 or higher,
          //the system asks the user to grant approval.
          if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
              //request permission from user if the app hasn't got the required permission
              ActivityCompat.requestPermissions(this,
                      new String[]{Manifest.permission.CALL_PHONE},   //request specific permission from user
                      10);
              return;
          }else {     //have got permission
              try{
                  startActivity(callIntent);  //call activity and make phone call
              }
              catch (android.content.ActivityNotFoundException ex){
                  Toast.makeText(getApplicationContext(),"yourActivity is not founded",Toast.LENGTH_SHORT).show();
              }
          }
      }
ghhkc1vu

ghhkc1vu4#

您可能需要尝试以下方法:

Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+txtPhn.getText().toString()));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(callIntent);

即添加callIntent.setFlags(...)函数调用。
另外,不要忘记在清单文件中添加CALL_PHONE权限:

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

n1bvdmb65#

API 24

Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:+94720999666"));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    startActivity(intent);

并且还添加了

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

ufj5ltwl6#

尝试添加修剪():

callIntent.setData(Uri.parse("tel:"+txtPhn.getText().toString().trim()));
cwdobuhd

cwdobuhd7#

你可能需要在真实的的设备上测试它。我遇到了同样的问题,它在真正的设备上工作。通话和GPS有时都不能像预期的那样在AVD中工作。

相关问题