Android Fake Call Application Tutorial

Android Fake Call Application Tutorial

There are some situations where by we are in a place and we are waiting for something to happen so that you can use it as an excuse to leave the environment. We might be waiting for our phone to ring but it will never ring.

Android Fake call application is an app you can use to set a time when you want your phone to ring. This tutorial is for education purpose that is learning how to develop android application with this case study.

I am not in any way responsible to whatever way you decide to use it for. If you are interested to learn how to develop this app, you should continue to read this tutorial.

You can also learn how to develop an android Torch Light application by following one of my tutorials

Before we start, the first thing I will do is to list the environment and tools I used in this android tutorial but feel free to use whatever environment or tools you are familiar with.

Windows 7

Android Studio

Samsung Galaxy Fame Lite

Min SDK 14

Target SDK 19

To create a new android application project, following the steps as stipulated below.

Go to File menu

Click on New menu

Click on Android Application

Enter Project name: AndroidFlashLightApp

Package: com.inducesmile.androidflashlightapp

Keep other default selections.

Continue to click on next button until Finish button is active, then click on Finish Button

Once you are done with creating your project, make sure you change the package name if you did not use the same package.

Manifest.xml File

Since we are going to access and make use of some hardware resources in our device like Alarm, we are going to use some android permissions. The list is as follows but note that you can remove the one you free is not necessary

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="inducesmile.com.androidfakecallapplication" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CALL_PHONE" ></uses-permission>
    <uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".FakeRingingActivity"
            android:label="@string/title_activity_fake_ringing" >
        </activity>
        <receiver android:name="inducesmile.com.androidfakecallapplication.FakeCallReceiver" />

    </application>

</manifest>

 Activity_main.xml Layout File

In the activity_main.xml layout file, we are going to design our layout with other View controls. In our android fake call application, we will use a RadioButtonGroup with three RadioButton to set option for the amount of time that will elapse before the phone will start ringing.

We will also add two EditText controls which will be use to get the name and phone number a user wants to fake a call to.

Finally, we will add a Button View to our layout. When the button is click, it will take the selected time and passed it to the AlarmManager.

The code for our activity_main.xml layout file is shown below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/bacckground"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/fakename"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="18dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:paddingLeft="10dp"
        android:hint="@string/fake_name"
        android:ems="10"
        android:inputType="textPersonName" >
    </EditText>

    <EditText
        android:id="@+id/fakenumber"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/fakename"
        android:layout_below="@+id/fakename"
        android:layout_marginTop="32dp"
        android:ems="10"
        android:hint="@string/fake_number"
        android:layout_marginRight="10dp"
        android:paddingLeft="10dp"
        android:inputType="phone" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/fakenumber"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp"
        android:contentDescription="@null"
        android:src="@drawable/icons" />

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="22dp"
        android:checkedButton="@+id/radio1"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:textColor="@color/white"
            android:text="@string/thirtysecs" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:text="@string/onemin" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:text="@string/fivemins" />
    </RadioGroup>

    <Button
        android:id="@+id/fakecalls"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/radioGroup1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:textSize="20sp"
        android:textColor="@color/green"
        android:textStyle="bold"
        android:background="@drawable/fakebutton"
        android:text="@string/make_call" />

</RelativeLayout>

 String.xml file

The project string.xml file has also been updated with many string literals that are use in the layout files. The updated version of the string.xml file is shown below.

<resources>
    <string name="app_name">AndroidFakeCallApplication</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="thirtysecs">10 secs</string>
    <string name="onemin">30 secs</string>
    <string name="fivemins">1 min</string>
    <string name="make_call">Call</string>
    <string name="fake_name">Enter name</string>
    <string name="fake_number">Enter phone number</string>
    <string name="title_activity_fake_ringing">FakeRingingActivity</string>
    <string name="incomingcall">Incoming Call</string>
    <string name="fakecallname">Genny Nnaji</string>
    <string name="fakecallnumber">080371558000</string>

</resources>

 MainActivty.java File

In the MainActivity class, we will instantiate all our View controls. We will get the select time and pass the time value to the AlarmManager. The Alarm Manager will broadcast the information to FakeCallReceiver class which is an android BroadcastReceiver class.

The code in the MainActivity.java class is shown below

package inducesmile.com.androidfakecallapplication;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.Calendar;


public class MainActivity extends ActionBarActivity {

    private EditText fakeName;
    private EditText fakeNumber;

    private RadioGroup radioGroup;
    private RadioButton firstOption;
    private RadioButton secondOption;
    private RadioButton thirdOption;

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

        fakeName = (EditText)findViewById(R.id.fakename);
        fakeNumber = (EditText)findViewById(R.id.fakenumber);

        radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        firstOption = (RadioButton)findViewById(R.id.radio0);
        secondOption = (RadioButton)findViewById(R.id.radio1);
        thirdOption = (RadioButton)findViewById(R.id.radio2);

        Button fakeCallButton = (Button)findViewById(R.id.fakecalls);
        fakeCallButton.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                int selectedTime = 0;

                int radioSelected = radioGroup.getCheckedRadioButtonId();
                int radioTimeSelected = getSelectedAnswer(radioSelected);

                if(radioGroup.getCheckedRadioButtonId() == -1){
                    Toast.makeText(MainActivity.this, "You must select a time", Toast.LENGTH_SHORT).show();
                    return;
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());
                calendar.add(Calendar.SECOND, 10);
                long currentFakeTime =  calendar.getTimeInMillis();

                String fakeNameEntered = fakeName.getText().toString();
                String fakeNumberEntered = fakeNumber.getText().toString();
                if(fakeNameEntered.equals("") || fakeNumberEntered.equals("")){
                    Toast.makeText(MainActivity.this, "You must both name and number", Toast.LENGTH_SHORT).show();
                    return;
                }
                System.out.println("Fake name" + fakeNameEntered);
                System.out.println("Fake number" + fakeNumberEntered);

                setUpAlarm(currentFakeTime, fakeNameEntered, fakeNumberEntered);
            }
        });

    }
    @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);
    }
    private int getSelectedAnswer(int radioSelected){
        int answerSelected = 0;
        if(radioSelected == R.id.radio0){
            answerSelected = 10;        }
        if(radioSelected == R.id.radio1){
            answerSelected = 30;
        }
        if(radioSelected == R.id.radio2){
            answerSelected = 60;
        }
        return answerSelected;
    }

    public void setUpAlarm(long selectedTimeInMilliseconds, String fakeName, String fakeNumber){

        AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, FakeCallReceiver.class);

        intent.putExtra("FAKENAME", fakeName);
        intent.putExtra("FAKENUMBER", fakeNumber);

        PendingIntent fakePendingIntent = PendingIntent.getBroadcast(this, 0,  intent, PendingIntent.FLAG_CANCEL_CURRENT);
        alarmManager.set(AlarmManager.RTC_WAKEUP, selectedTimeInMilliseconds, fakePendingIntent);
        Toast.makeText(getApplicationContext(), "Your fake call time has been set", Toast.LENGTH_SHORT).show();

        Intent intents = new Intent(this, MainActivity.class);
        startActivity(intents);

    }


}

 FakeCallReceiver Class

The FakeCallReceiver class is a BroadcastReceiver that is trigger by the AlarmManager. When it is triggered, it will obtain the user input values for name and phone from android bundle and pass it to FakeRingingActivity class. The code sample for the FakeCallReceiver is shown below

package inducesmile.com.androidfakecallapplication;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class FakeCallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String getFakeName = intent.getStringExtra("FAKENAME");
        String getFakePhoneNumber = intent.getStringExtra("FAKENUMBER");

        Intent intentObject = new Intent(context.getApplicationContext(), FakeRingingActivity.class);
        intentObject.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intentObject.putExtra("myfakename", getFakeName);
        intentObject.putExtra("myfakenumber", getFakePhoneNumber);
        context.startActivity(intentObject);
    }

}

Activity_fake_ringing.xml

When it is time for the fake call to start ringing the Alarm Manager will trigger the BroadcastReciever class which in turn opens the FakeRingingActivity. The code in activity_fake_ringing.xml layout file is similar to the activity_main.xml layout file. The code is shown below.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:background="@drawable/bacckground"
    tools:context="inducesmile.com.androidfakecallapplication.FakeRingingActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:textStyle="bold"
        android:text="@string/incomingcall"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:contentDescription="@null"
        android:src="@drawable/nnaji" />

    <TextView
        android:id="@+id/chosenfakename"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/imageView1"
        android:textColor="@color/white"
        android:layout_marginTop="20dp"
        android:textSize="20sp"
        android:text="@string/fakecallname" />

    <TextView
        android:id="@+id/chosenfakenumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/chosenfakename"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:textColor="@color/white"
        android:textStyle="bold"
        android:textSize="22sp"
        android:text="@string/fakecallnumber" />

    <Button
        android:id="@+id/answercall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="26dp"
        android:layout_marginLeft="29dp"
        android:contentDescription="@null"
        android:background="@drawable/acceptcall" />

    <Button
        android:id="@+id/rejectcall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/answercall"
        android:layout_alignParentRight="true"
        android:layout_marginRight="32dp"
        android:contentDescription="@null"
        android:background="@drawable/rejectcall" />

</RelativeLayout>

 FakeRingingActivity

The FakeRingingActivity activity class is similar to the MainActivity class with the exception that FakeRingingActivity class uses it two Button Views to accept or reject call. The code for this class is shown below.

package inducesmile.com.androidfakecallapplication;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.MediaPlayer;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class FakeRingingActivity extends ActionBarActivity {

    private String networkCarrier;
    private MediaPlayer mp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fake_ringing);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        TextView fakeName = (TextView)findViewById(R.id.chosenfakename);
        TextView fakeNumber = (TextView)findViewById(R.id.chosenfakenumber);

        final TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        networkCarrier = tm.getNetworkOperatorName();

        TextView titleBar = (TextView)findViewById(R.id.textView1);
        if(networkCarrier != null){
            titleBar.setText("Incoming call - " + networkCarrier);
        }else{
            titleBar.setText("Incoming call");
        }

        String callNumber = getContactNumber();
        String callName = getContactName();

        fakeName.setText(callName);
        fakeNumber.setText(callNumber);

        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
        mp = MediaPlayer.create(getApplicationContext(), notification);
        mp.start();

        Button answerCall = (Button)findViewById(R.id.answercall);
        Button rejectCall = (Button)findViewById(R.id.rejectcall);

        answerCall.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                mp.stop();
            }
        });
        rejectCall.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                mp.stop();
                Intent homeIntent= new Intent(Intent.ACTION_MAIN);
                homeIntent.addCategory(Intent.CATEGORY_HOME);
                homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(homeIntent);
            }
        });
    }

    @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_fake_ringing, 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);
    }

    private String getContactNumber(){
        String contact = null;
        Intent myIntent = getIntent();
        Bundle mIntent = myIntent.getExtras();
        if(mIntent != null){
            contact  = mIntent.getString("myfakenumber");
        }
        return contact;
    }

    private String getContactName(){
        String contactName = null;
        Intent myIntent = getIntent();
        Bundle mIntent = myIntent.getExtras();
        if(mIntent != null){
            contactName  = mIntent.getString("myfakename");
        }
        return contactName;
    }

}

You can download the code for this tutorial below. If you are having hard time downloading the tutorials, kindly contact me.

Remember to subscribe with your email so that you will be among the first to receive our new post once it is published

OTHER INTERESTING POSTS:

4 Comments

Add a Comment