Call intent in Android

别来无恙 提交于 2019-12-17 19:24:38

问题


How can I make call by pressing button? I get my number as a string from EditText. Here is my sample code:

String phone = editPhone.getText().toString();
btnPhone.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                call();
            }
        });
public void call() {
    try {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(phone));
        startActivity(callIntent);
    } catch (ActivityNotFoundException activityException) {
         Log.e("myphone dialer", "Call failed", e);
    }
}

I added all permissions to manifest file.

but I am getting NullPointerexception


回答1:


This simple approach should work for you.

Ex.

public class CallActivity extends Activity{
   String phone = "";

   onCreate()
   {
        btnPhone.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View arg0) { 
                phone = editPhone.getText().toString(); 
                call(); 
            } 
        });    
   }

   public void call() {   
            Intent callIntent = new Intent(Intent.ACTION_CALL);          
            callIntent.setData(Uri.parse("tel:"+phone));          
            startActivity(callIntent);  
   }
}

You might be using String variable phone out of scope.




回答2:


I think you missed the "tel:" part in the URI.

Replace the following..

Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse(phone));
        startActivity(callIntent);

with

Intent callIntent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phone));
       startActivity(callIntent);

or

Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:"+phone));
            startActivity(callIntent);



回答3:


see below code it may help you.

for call

EditText num = (EditText)findViewById(R.id.number_edit);
String uri = "tel:" + num.trim();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);

for open dialer

Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
dial.setData(Uri.parse("tel:"));
startActivity(dial);



回答4:


String PhoneNo="+923341234567"
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + PhoneNo));
startActivity(intent);

and add a permission in manifest

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



回答5:


Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse(phone));
startActivity(callIntent);

above is the only method you use for calling a phone number in android, when you click on the call button a 'dilerpad' activty is started with perpopulated phone number, and the call will go if you press the call button on the dialer pad.




回答6:


Try this

EditText num = (EditText)findViewById(R.id.phone_number);
String uri = "tel:" + num.trim();
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(uri));
startActivity(intent);



回答7:


Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" +"93");
intent.setData(Uri.parse(uri));
startActivity(intent);


来源:https://stackoverflow.com/questions/10510395/call-intent-in-android

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