Hello friends, welcome again to this new android studio tutorial. In this android studio tutorial we will be creating a simple app to detect outgoing calls. So if you are struggling to create an app that can detect the outgoing calls; you just came to the right place. So without wasting time lets start our android studio tutorial.
[easy_media_download url=”https://dl.dropboxusercontent.com/s/6zscflnh3w8x81u/android-studio-tutorial-detecting-outgoing-calls.zip?dl=0″ text=”Download Source”]
Creating Android Studio Project
- Open android studio and create a new project. I created DetectOutgoingCalls
- Now you will get your MainActivity.java and activity_main.xml
- We need a broadcast receiver that will detect the outgoing call
Creating Broadcast Receiver for Detecting Outgoing Calls
- Right click on your package name. Go to new ->Â java class and create a new java class.
- I just created CallDetector.java
1 2 3 4 5 6 7 8 9 10 |
package net.simplifiedcoding.detectoutgoingcalls; /** * Created by Belal on 9/13/2015. */ public class CallDetector { } |
- Inside this class we will create a new class for our broadcast receiver
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class CallDetector { public class OutgoingDetector extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); } } private Context ctx; private OutgoingDetector outgoingDetector; } |
- We will store the number received using Shared Preferences
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public class CallDetector { public static final String MY_PREF = "MY_PREF"; public static final String NUMBER_KEY = "NUMBER_KEY"; private SharedPreferences sharedPreferences; public class OutgoingDetector extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); sharedPreferences = ctx.getSharedPreferences(MY_PREF,Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(NUMBER_KEY,number); editor.commit(); } } private Context ctx; private OutgoingDetector outgoingDetector; public CallDetector(Context ctx) { this.ctx = ctx; outgoingDetector = new OutgoingDetector(); } } |
- Now we will create two methods, start and stop.
- In start we will register our broadcast receiver and in stop we will unregister it
- Including start stop method our full CallDetector class will be look as follow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package net.simplifiedcoding.detectoutgoingcalls; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.preference.PreferenceManager; import android.widget.Toast; /** * Created by Belal on 9/13/2015. */ public class CallDetector { public static final String MY_PREF = "MY_PREF"; public static final String NUMBER_KEY = "NUMBER_KEY"; private SharedPreferences sharedPreferences; public class OutgoingDetector extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER); sharedPreferences = ctx.getSharedPreferences(MY_PREF,Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString(NUMBER_KEY,number); editor.commit(); } } private Context ctx; private OutgoingDetector outgoingDetector; public CallDetector(Context ctx) { this.ctx = ctx; outgoingDetector = new OutgoingDetector(); } public void start() { IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL); ctx.registerReceiver(outgoingDetector, intentFilter); } public void stop(){ ctx.unregisterReceiver(outgoingDetector); } } |
- Now the problem is we cannot make a call without minimizing our app
- So if we minimize our app for making a call our app will not work
- So we have to do something so that our app will work on background
- We can overcome this problem by creating a service
- Right click on your package -> new -> java class and create a new java class. I just created CallDetectionService
- This class will extend the Service class, so we need to override some methods as well
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public class CallDetectionService extends Service { @Override public int onStartCommand(Intent intent, int flags, int startId) { return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { super.onDestroy(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } |
- Inside onStartCommand we will start our broadcast receiver and inside onDestroy we will stop our broadcast receiver
- So the final code for CallDetectionService.java will be
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package net.simplifiedcoding.detectoutgoingcalls; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.support.annotation.Nullable; /** * Created by Belal on 9/13/2015. */ public class CallDetectionService extends Service { private CallDetector callDetector; @Override public int onStartCommand(Intent intent, int flags, int startId) { callDetector = new CallDetector(this); int r = super.onStartCommand(intent, flags, startId); callDetector.start(); return r; } @Override public void onDestroy() { super.onDestroy(); callDetector.start(); } @Nullable @Override public IBinder onBind(Intent intent) { return null; } } |
- Now we need to add this service to our manifest
- Add the following code inside application tag in your manifest
1 2 3 4 5 6 7 |
<service android:name=".CallDetectionService" android:enabled="true" android:exported="false" > </service> |
- Inside manifest add the following permission as well
1 2 3 |
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" /> |
- Now the last part is to create a User Interface to make our app work.
Designing User Interface for Our Android Application
- Come to your activity_main.xml. We need to create the following layout
- Use the following xml code for creating above layout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" 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" tools:context=".MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Turn On Detection" android:id="@+id/button" android:layout_gravity="center_horizontal" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/textView" android:layout_gravity="center_horizontal" /> </LinearLayout> |
- Now come to your MainActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package net.simplifiedcoding.detectoutgoingcalls; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button button; private TextView textView; private boolean detecting = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button); textView = (TextView) findViewById(R.id.textView); button.setOnClickListener(this); } @Override public void onClick(View v) { } } |
- Inside onClick we will toggle the detection
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
@Override public void onClick(View v) { Intent intent = new Intent(this, CallDetectionService.class); if(!detecting){ detecting=true; button.setText("Turn off detection"); startService(intent); }else{ detecting=false; button.setText("Turn on detection"); stopService(intent); } } |
- Now in onResume method we will fetch the data from shared preferences
1 2 3 4 5 6 7 8 9 |
@Override public void onResume() { super.onResume(); SharedPreferences sharedPreferences = getSharedPreferences(CallDetector.MY_PREF,MODE_PRIVATE); String number = sharedPreferences.getString(CallDetector.NUMBER_KEY,"No Value Found"); textView.setText(number); } |
- Thats it now run your project. Click on the button then minimize your app and make a call. Now end the call and open the app you will see the number on the text view.
Check the Video of Tracking Outgoing Calls in Android
This is the video demonstration of the output of this android app.
So thats all for this android studio tutorial friends. Hope you enjoy this android studio tutorial. Thank You 🙂