Android start background app on incoming call. Almost done
<p>The three files typed below are all that I have. This works, but is not flawless. Here is ALL that I want to do.</p>

<p>When a person receives a phone call, grab the number and PING a url with get variables. Thats it. I need it to be as flawless and run in the background.</p>

<ol>
<li>RECIEVE CALL</li>
<li>PING URL</li>
<li>ALL SILENTLY</li>
</ol>

<p>Any help is appreciated :)</p>

<p><strong>EDIT:</strong> There are continuous misc. errors. It only works 50% of the time. Sometimes it will log but won't ping. It is also dependent on wether the phone is unlocked, locked, who ends the call first, when u picked it up.</p>

<p>Its just a mess. Seeing if anyone has an easy solution.</p>

<p><strong>ServiceReceiver.java</strong></p>

<pre><code>public class ServiceReceiver extends BroadcastReceiver {
private String TAG = "d3x";
@Override
public void onReceive(Context context, Intent intent) {

    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);


    Date dNow = new Date();
    SimpleDateFormat date = new SimpleDateFormat ("M/d/y");
    SimpleDateFormat time = new SimpleDateFormat ("H:m:s");
    String URL = "http://xxxxxxxxxxxxx.com/android/index.php?API=Su89A_X9yu8&amp;phoneNumber="+number+"&amp;phoneState="+state+"&amp;time="+time.format(dNow)+"&amp;date="+date.format(dNow)+"";

    if (TelephonyManager.EXTRA_STATE_RINGING.equals(state))
    {
        Log.d("MPR", "RINGING [" + number + "]");
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        try {
            request.setURI(new URI(URL));
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        }
        try {
            HttpResponse response = client.execute(request);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
        }
    }


    if (TelephonyManager.EXTRA_STATE_IDLE.equals(state))
    {
        Log.d("MPR", "IDLE [" + number + "]");
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        try {
            request.setURI(new URI(URL));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        try {
            HttpResponse response = client.execute(request);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (TelephonyManager.EXTRA_STATE_OFFHOOK.equals(state))
    {
        Log.d("MPR", "OFFHOOK [" + number + "]");
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        try {
            request.setURI(new URI(URL));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        try {
            HttpResponse response = client.execute(request);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  Log.i(TAG, "ServiceReceiver-&gt;onReceive();");
  MyPhoneStateListener phoneListener = new MyPhoneStateListener();
  TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneListener, PhoneStateListener.LISTEN_SERVICE_STATE);
    telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
}
</code></pre>

<p><strong>MyPhoneStateListener.java</strong></p>

<pre><code>public class MyPhoneStateListener extends PhoneStateListener {
private String TAG = "d3x";


@Override
public void onCallStateChanged(int state,String incomingNumber){
    switch(state){
    case TelephonyManager.CALL_STATE_IDLE:
          Log.i(TAG, "MyPhoneStateListener-&gt;onCallStateChanged() -&gt; CALL_STATE_IDLE "+incomingNumber);
          break;
    case TelephonyManager.CALL_STATE_OFFHOOK:
          Log.i(TAG, "MyPhoneStateListener-&gt;onCallStateChanged() -&gt; CALL_STATE_OFFHOOK "+incomingNumber);
          break;
    case TelephonyManager.CALL_STATE_RINGING:
          Log.i(TAG, "MyPhoneStateListener-&gt;onCallStateChanged() -&gt; CALL_STATE_RINGING "+incomingNumber);
          break;
    default:
       Log.i(TAG, "MyPhoneStateListener-&gt;onCallStateChanged() -&gt; default -&gt; "+Integer.toString(state));
       break;
    }    

}


@Override
public void onServiceStateChanged (ServiceState serviceState){
  switch(serviceState.getState()){
       case ServiceState.STATE_IN_SERVICE:
            Log.i(TAG, "MyPhoneStateListener-&gt;onServiceStateChanged() -&gt; STATE_IN_SERVICE");
            serviceState.setState(ServiceState.STATE_IN_SERVICE);
            break;
       case ServiceState.STATE_OUT_OF_SERVICE:
            Log.i(TAG, "MyPhoneStateListener-&gt;onServiceStateChanged() -&gt; STATE_OUT_OF_SERVICE");
            serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE);
            break;
       case ServiceState.STATE_EMERGENCY_ONLY:
          Log.i(TAG, "MyPhoneStateListener-&gt;onServiceStateChanged() -&gt; STATE_EMERGENCY_ONLY");
          serviceState.setState(ServiceState.STATE_EMERGENCY_ONLY);
          break;
       case ServiceState.STATE_POWER_OFF:
          Log.i(TAG, "MyPhoneStateListener-&gt;onServiceStateChanged() -&gt; STATE_POWER_OFF");
          serviceState.setState(ServiceState.STATE_POWER_OFF);
          break;
       default:
          Log.i(TAG, "MyPhoneStateListener-&gt;onServiceStateChanged() -&gt; default -&gt; "+Integer.toString(serviceState.getState()));
          break;
    }
}
}
</code></pre>

<p><strong>AndroidManifest.xml</strong></p>

<pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;manifest package="tk.d3xt3r01" android:versionCode="1" android:versionName="1.0"         xmlns:android="http://schemas.android.com/apk/res/android"&gt;
&lt;application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:debuggable="true"&gt;
    &lt;activity android:name=".IncomingPhone" android:label="@string/app_name"&gt;
        &lt;intent-filter&gt;
            &lt;action android:name="android.intent.action.MAIN" /&gt;
            &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
        &lt;/intent-filter&gt;
    &lt;/activity&gt;
  &lt;receiver android:name=".ServiceReceiver"&gt;
     &lt;intent-filter&gt;
        &lt;action android:name="android.intent.action.PHONE_STATE" /&gt;
     &lt;/intent-filter&gt;
  &lt;/receiver&gt;
&lt;/application&gt;
&lt;uses-sdk android:minSdkVersion="7" /&gt;
&lt;uses-permission android:name="android.permission.INTERNET"&gt;&lt;/uses-permission&gt;
&lt;uses-permission android:name="android.permission.READ_PHONE_STATE"&gt;&lt;/uses-permission&gt;
&lt;/manifest&gt; 
</code></pre>
