Android Intent.ACTION_CALL, Uri

2019-01-17 17:52发布

问题:

I am trying to use the Intent.Action class. I know how to use the ACTION_VIEW to display a URL but I wanted to use the Intent.ACTION_DIAL to call number when the application is launched. The documentation says you need to parse a URI into a string and then add it to the Intent I tried this:

Uri call = Uri.parse("7777777777");             
Intent surf = new Intent(Intent.ACTION_DIAL, call); 
startActivity(surf);

This doesn't work I get an error message saying:

Unfortunately, Project has stopped. I tried to debug the code and it seems to point me to the intent line not sure what I doing wrong if I just do this it works and brings up the dialer.

//Uri call = Uri.parse("7777777777");               
Intent surf = new Intent(Intent.ACTION_DIAL);   
startActivity(surf);

回答1:

tel

String number = "23454568678";
    Intent intent = new Intent(Intent.ACTION_CALL);
    intent.setData(Uri.parse("tel:" +number));
    startActivity(intent);

Use Permission

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


回答2:

To just open the dialer app (the user has to press the call button inside the dialer app; no additional permissions needed) use:

String number = "7777777777";
Uri call = Uri.parse("tel:" + number);             
Intent surf = new Intent(Intent.ACTION_DIAL, call); 
startActivity(surf);

To open the dialer app and do the call automatically (needs android.permission.CALL_PHONE) use:

String number = "7777777777";
Uri call = Uri.parse("tel:" + number);             
Intent surf = new Intent(Intent.ACTION_CALL, call); 
startActivity(surf);


回答3:

try this

String url="tel:777777777"
if (url.startsWith("tel:")) 
{ 
Intent intent = new Intent(Intent.ACTION_DIAL,
Uri.parse(url)); 
startActivity(intent);
}

add this to your AndroidManifest.xml file

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


回答4:

Try this also

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

Android Manifest

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


回答5:

try this

String no = "536171839";
Intent callintent = new Intent(android.intent.action.CALL);
callintent.setData(Uri.parse("tel:" +no));
startActivity(callintent);

add this to your AndroidManifest.xml file

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


回答6:

Try this :

 String toCall = "tel:" + number.getText().toString();

            startActivity(new Intent(Intent.ACTION_DIAL,

                    Uri.parse(toCall)));


回答7:

Another approach is to make an PendingIntent to be called later. This is specially util when you want to redirect the user directly to phone call from a notification Action.

String number = "551191111113";
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" +number));
PendingIntent pendingIntentForCall = PendingIntent.getActivity(mContext, 0 /* Request code */, intent,PendingIntent.FLAG_ONE_SHOT);

You can use it in notification as follow:

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(mContext)
                .setContentTitle(title)
                .setContentText(message)
                .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
                .setTicker(tickerText)
                .setColor(Color.BLACK)
                .setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_directions_bus_white_48dp))
                .setSmallIcon(R.mipmap.ic_directions_bus_white_24dp)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .addAction(new NotificationCompat.Action(R.mipmap.ic_directions_bus_white_24dp,"Call to " + number,pendingIntentForCall));


回答8:

If u have added

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

Check the permission of call on the phone for your application.



回答9:

For ACTION_DIAL you just need to create Intent object with that action as a first argument and Uri object as a second argument built from phone number written as a string. After that just call startActivity() method and pass previously created intent object as an argument. For example:

private String phoneNumber = "123456";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button dial_number = findViewById(R.id.button);
    dial_number.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + phoneNumber));
            startActivity(intent);
        }
    });
}