0

I can't seem to find a good class / API that can tell me when my phone is making a phone call. I know that I can detect when a phone call is being received by using TelephonyManager, but how do I detect if the user is calling someone?

Below is the code I have currently:

public class hiWorld extends Activity {

private int counter=0; 



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);




 if(tm.getCallState()==1) //ringing  
 {

     setContentView(R.layout.activity_hi_world);
     TextView tcounter = (TextView)findViewById(R.id.textbox1);

    tcounter.setText("calling");  

 }

 else if (tm.getCallState()==0) //idle 

 { 
     setContentView(R.layout.activity_hi_world);
     TextView tcounter = (TextView)findViewById(R.id.textbox1);

     tcounter.setText("not calling");
 }
Kara
  • 6,115
  • 16
  • 50
  • 57
fouadalnoor
  • 197
  • 2
  • 5
  • 14

1 Answers1

1

You will need to register an android.intent.action.NEW_OUTGOING_CALL Receiver for Receiving an outgoing Call broadcast in your application as:


AndroidManifest.xml
<receiver android:name=".OutgoingCallReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.NEW_OUTGOING_CALL" >
            </action>
        </intent-filter>
    </receiver>


OutgoingCallReceiver.java:
public class OutgoingCallReceiver extends BroadcastReceiver {


     @Override
     public void onReceive(Context context, Intent intent) {

    final String originalNumber =
    intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
    //START YOUR SERVICE HERE FOR COUNTING MINS
    Intent intent = new Intent(context, MyService.class);
    intent.putExtra("number", originalNumber);
    context.startService(intent);
    }
  } 


Permission Required :
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • @user1508897 : can you make me clear what you want? yes you can put context and intent variables in Broadcast receiver or you can also start an service or Activity from Broadcast Receiver for doing any other work as your project needs. – ρяσѕρєя K Jul 07 '12 at 15:40
  • Well, what I want to do is simply check if the user is making a call and count how many minutes that call lasts. I am really new at Java and Android so do bear with my noob questions :) – fouadalnoor Jul 07 '12 at 15:42
  • @user1508897 : then just Create an IntentService and Start it from Broadcast Receiver for counting minutes when any Outgoing call appear. – ρяσѕρєя K Jul 07 '12 at 15:44
  • @user1508897 : SEE MMY EDIT ANSWER.I YOU HAVE STILL ANY ISSUE THEN TELL MY I WILL MAKE YOU CLEAR – ρяσѕρєя K Jul 07 '12 at 15:48
  • Ok, so I am creating the IntenetService now. Does this look okay? http://paste.ideaslabs.com/show/pYcYOGYSSL – fouadalnoor Jul 07 '12 at 16:09
  • ok, but what do I put into the middle bit where I actually check for the phone call being made? I am thinking an if statement to see if the user is calling someone, but not sure how to do that exactly.. – fouadalnoor Jul 07 '12 at 16:12
  • @user1508897 : nothing put in Broadcast recvier just take number from Broadcast Recvier and send it to IntentService using putextra. and do't put any validation for checking user making a call or not because when user make a call then this Broadcastreciver fire – ρяσѕρєя K Jul 07 '12 at 16:17
  • Ok, so someone makes a call, broadcast receiver then starts up and turns the service on? – fouadalnoor Jul 07 '12 at 16:24
  • @user1508897 : yes yes dear!!!:). you got it what i want to say.so if this answer help you then mark it as answer for others help by clicking on right side right image.thanks so much. – ρяσѕρєя K Jul 07 '12 at 16:30
  • I'm still not sure...how do I start the service? – fouadalnoor Jul 07 '12 at 16:43