Make call using a specified SIM in a Dual SIM Device (Java & Kotlin)

2
358
Android Studio Make Ussd Calls
Android Studio Make Ussd Calls

Hello, welcome back to another tutorial series on AndroidNoon. we would be talking about how you can make calls with a specific sim card in android studio. This is implemented using java, but you can easily convert it to kotlin. Our last ussd tutorial talked about how you can make ussd calls without specifying a sim card, so if you make the call, your phone would prompt you to select a sim card vvia dialog, but todays tutorial solves that issue. with this, you can make ussd calls directly via sim 1 or sim 2. After learning how to make ussd call in android studio, our next tutorial would be how to get ussd response.

 

  1. Add Call permission to the Androidmanifest.xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ussd.call">

<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"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

 

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:padding="20dp"
tools:showIn="@layout/activity_main">

<EditText
android:id="@+id/ed_inputUssd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:hint="Input Ussd Code"
/>
<Button
android:id="@+id/btnDialUssdCode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@id/ed_inputUssd"
android:text="Dial Ussd Code"/>
</android.support.constraint.ConstraintLayout>

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

 

MainActivity.java

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText edInputUssdCode;
Button btnDialUssdCode;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);




edInputUssdCode = (EditText) findViewById(R.id.ed_inputUssd);
btnDialUssdCode = (Button) findViewById(R.id.btnDialUssdCode);

btnDialUssdCode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

String UssdCode = edInputUssdCode.getText().toString();

//check if edittext is empty
if (UssdCode.equalsIgnoreCase("")) {

Toast.makeText(MainActivity.this, "Please Input ussd code", Toast.LENGTH_SHORT).show();
return;
}

//check if its a valid ussd code
if (UssdCode.startsWith("*") &amp;&amp; UssdCode.endsWith("#")) {

//we want to remove the last # from the ussd code as we need to encode it. so *555# becomes *555
UssdCode = UssdCode.substring(0, UssdCode.length() - 1);

String UssdCodeNew = UssdCode + Uri.encode("#");

Intent intent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + UssdCodeNew));

intent.putExtra("com.android.phone.extra.slot", 0); //For sim 1
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
} else {
getActivity().startActivity(intent);

}



} else {
Toast.makeText(MainActivity.this, "Please enter a valid ussd code", Toast.LENGTH_SHORT).show();
}


}
});


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

2 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here