Android App to call a number on button click

匿名 (未验证) 提交于 2019-12-03 01:29:01

问题:

I want to call a number given in a textbox but I'm getting the error:

The application 'xyz'(process com.android) has stopped unexpectedly

The following is my code. Where is it going wrong?

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

EDITED LogCat

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` 

回答1:

Make sure you have added the tag at a correct level in the AndroidManifest.xml file (outside the tag but within the tag):



回答2:

There are two intents to call/start calling: ACTION_CALL and ACTION_DIAL.

ACTION_DIAL will only open the dialer with the number filled in, but allows the user to actually call or reject the call. ACTION_CALL will immediately call the number and requires an extra permission.

So make sure you have the permission

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

in your AndroidManifest.xml



回答3:

The problem is that you didn't request permission from the user. If the device is running Android 6.0 (API level 23) and the app's targetSdkVersion is 23 or higher, the call permission will be considered as dangerous permission. Therefore you must get the right permission first. First of all, add

       ...    

into your AndroidManifest.xml file.

Then, I have a method is called by clicking a button.

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


回答4:

You might want to try the following approach:

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

I.e. adding the callIntent.setFlags(...) function call.



回答5:

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

and also add



回答6:

Try adding trim():

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


回答7:

You probably need to test it on a real device. I faced the same problem and it worked on the real device. Calling and GPS both sometimes dont work as expected in AVD.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!