Kapil Rajput
Kapil Rajput

Reputation: 11545

How to call on a Number Directly from Android App

I want to open a Calling Screen Directly from my app, I searched a lot about on internet and getting no success. Code i am following to call a number from android app is as follows :

startActivity(new Intent(Intent.ACTION_CALL).setData(Uri.parse("tel:" + phoneNo)));

in Manifest :

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

but this code is showing Choose Action Window and I don't want to show this window :

enter image description here

Is it possible to open Phone call app directly rather than open Action Window ?

Upvotes: 0

Views: 1155

Answers (2)

nariman amani
nariman amani

Reputation: 293

you can set a package name for your intent and directly call an application

intent.setPackage("com.android.phone");               

Upvotes: 0

 Ekalips
Ekalips

Reputation: 1491

Unfortunately (or really not) you can't. This is security measure. You can't directly open some app, because in 70% cases (just my results after testing) it will give you Security error.

Some clarification

As mentioned in comments you can define package in Intent. But it's really unhealthy behavior. For example

    Intent sendIntent = new Intent(Intent.ACTION_VIEW);
    sendIntent.setType("message/rfc822");
    sendIntent.setData(Uri.parse("[email protected]"));
    sendIntent.setClassName("com.google.android.gm", "com.google.android.gm.ComposeActivityGmail");
    sendIntent.putExtra(Intent.EXTRA_EMAIL, getSelectedContactsEmails().toArray());
    sendIntent.putExtra(Intent.EXTRA_SUBJECT, "subj");
    sendIntent.putExtra(Intent.EXTRA_TEXT, "text");
    startActivityForResult(sendIntent,0);

This code will call directly GMail app. But it have unpredictable behavior . In my case I got crash on one Nexus 5, but all worked on another one. Also you need to add additional code to ensure, that package is installed.

In your case you need to call some phone app. Do you know Android device fragmentation? There are 10000+ different devices and all of them can have different packages for calling. So do you really need it?

In fact there is even special method, that will handle selection of app you need by system

startActivity(Intent.createChooser(emailIntent, "Send mail via ...")); //example

This code won't get you Security error ever, because user actions are perceived by system "as a truth"

Clarification 2

Yes, you can cycle through all packages that can accept your intent. Yes, you can even call random(or pre-defined) package and yes, it might work is some cases. But some doesn't mean all and there is a problem. And if there any chance that app will crash on some device, you need to avoid it by any means.

Upvotes: 1

Related Questions