Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Get Call State2


Android Call State BroadCastReceiver Example

activity_main.xml

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:paddingBottom=“@dimen/activity_vertical_margin”
  6.     android:paddingLeft=“@dimen/activity_horizontal_margin”
  7.     android:paddingRight=“@dimen/activity_horizontal_margin”
  8.     android:paddingTop=“@dimen/activity_vertical_margin”
  9.     tools:context=“.MainActivity” >
  10.     <TextView
  11.         android:layout_width=“wrap_content”
  12.         android:layout_height=“wrap_content”
  13.         android:text=“@string/hello_world” />
  14. </RelativeLayout>

Activity class

File: MainActivity.java
  1. package com.example.callstatebroadcastreceiver;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. public class MainActivity extends Activity {
  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.activity_main);
  10.     }
  11.     @Override
  12.     public boolean onCreateOptionsMenu(Menu menu) {
  13.         // Inflate the menu; this adds items to the action bar if it is present.
  14.         getMenuInflater().inflate(R.menu.main, menu);
  15.         return true;
  16.     }
  17. }

IncommingCallReceiver

File: IncommingCallReceiver.java
  1. package com.example.callstatebroadcastreceiver;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.telephony.TelephonyManager;
  6. import android.widget.Toast;
  7.  public class IncommingCallReceiver extends BroadcastReceiver{
  8.       Context context;
  9.       @Override
  10.       public void onReceive(Context context, Intent intent){
  11.           try{
  12.            String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
  13.               if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)){
  14.                    Toast.makeText(context, “Phone Is Ringing”, Toast.LENGTH_LONG).show();
  15.               }
  16.               if(state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
  17.                    Toast.makeText(context, “Call Recieved”, Toast.LENGTH_LONG).show();
  18.               }
  19.               if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)){
  20.                    Toast.makeText(context, “Phone Is Idle”, Toast.LENGTH_LONG).show();
  21.               }
  22.           }
  23.           catch(Exception e){e.printStackTrace();}
  24.       }
  25. }

Comments

comments