Android - Creating Intents: Phone
Hello all,
I really enjoyed the Udacity Android Basics by Google lesson Practice with Intents so I am taking some time to fully explore each of the intents in the Android Common Intents documentation. The one I have most recently looked at is Phone.
First I just created a simple button to activate the intent which looks like this:
And the code in activity_main.xml looks like this:
<Button
android:id="@+id/phone"
android:layout_width="@dimen/button_width"
android:layout_height="@dimen/button_height"
android:layout_gravity="center"
android:layout_margin="@dimen/margin"
android:onClick="callPhone"
android:text="Phone"/>
Then I created an activity in MainActivity.java to call the intent when the button is clicked. To access the phone dialer, all you need to do is include this code.
public void callPhone(View view) {
Intent intent = new Intent(Intent.ACTION_DIAL);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
In order to access the dialer with a phone number already entered add this extra line of code: intent.setData(Uri.parse("tel:01234567891")); so that the Java code now looks like this:
public void callPhone(View view) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:01234567891"));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
There are some extra details about How to properly format a Uri for a telephone number which is linked on the Android Developer Website. However, I found that just writing the number out with no spaces works absolutely fine.
To automatically call the number as soon as you press the button change ACTION_DIAL to ACTION_CALL in the java code so it now looks like this:
public void callPhone(View view) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:01234567891"));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
The final thing you need is to get permission to make a call. To do this you need to go to Apps > Manifest > AndroidManifest.xml…
The code originally looks like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.intentspractise">
<application android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You need to add this extra line of code INSIDE the section but OUTSIDE all other sections of this file. That new line looks like this:
<uses-permission android:name="android.permission.CALL_PHONE" />
The updated file should therefore look like this, with comments:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.intentspractise">
<!--The line below has been changed to give this app permission to call a phone
N.B. The permission line HAS to be put here, inside the manifest section but outside the other
sections -->
<uses-permission android:name="android.permission.CALL_PHONE" />
<application android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
UPDATE:
When I came to refining this app I discovered that a major error was that this intent required that the permission was checked when run. To do this I had to significantly change the code. At the end of it, it looked like this:
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_CALL = 1;
Intent callIntent;
Button mCallButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
private void init() {
mCallButton=(Button)findViewById(R.id.phone);
mCallButton.setOnClickListener(new View.OnClickListener(){
@Override public void onClick(View v){
callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:01234567891"));
if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
android.support.v4.app.ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, REQUEST_CALL);
} else {
startActivity(callIntent);
}
}
});
}
The top line: public class MainActivity extends AppCompatActivity { does not need to be added, that is there to show where to put the global variables. Also, you do not need to create a new onCreatemethod, it is just there to show that init(); needs to go in there. For a full demo of how to do this check the following YouTube video about self permission checks by Android Master. You will notice that some of my code is slightly different to that shown in the video. I am guessing that is due to updates that have occurred since the video was made. I was also helped by this article about resolving manifest permission.
UPDATE ENDS
I welcome any comments or questions about this, especially corrections of anything I can improve. For example, I have not tried using the voicemail Uri scheme.