How to start service in dialer (default or stock dialer) of android
<p>I want start a service when my dialer (default or stock dialer) open. My service just toasts a message. How to start the service for dialer. The <code>startService</code> command is working in <code>MainActivity</code>, but it is not working while a dialer is open.</p>

<p>My code is shown below: </p>

<p><strong>Manifest.xml</strong></p>

<pre><code> &lt;?xml version="1.0" encoding="utf-8"?&gt;    

&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chatheads"&gt;

&lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /&gt;
&lt;uses-permission android:name="android.permission.DIAL_ACTION" /&gt;
&lt;uses-permission android:name="android.permission.CALL_PHONE" /&gt;


    &lt;application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"&gt;
    &lt;activity android:name=".MainActivity"&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;service
        android:name=".MyService"
        android:enabled="true"
        android:exported="true" /&gt;

    &lt;activity android:name=".SecondActivity"&gt;&lt;/activity&gt;
&lt;/application&gt;

&lt;/manifest&gt; 
</code></pre>

<p><strong>MainActivity.java</strong></p>

<pre><code>package com.example.chatheads;

import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.DialerKeyListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
Button startService,stopService;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService=(Button)findViewById(R.id.startService);
    stopService=(Button)findViewById(R.id.stopService);




    if(getIntent().getAction().equals(Intent.ACTION_DIAL)) {
        Intent intent = new Intent(getBaseContext(), MyService.class);
        startService(intent);

    }

    startService.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(getApplication(), MyService.class));




        }
    });
    stopService.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(getApplication(), MyService.class));



        }
    });


  }
}
</code></pre>

<p><strong>Myservice.java</strong></p>

<pre><code>package com..service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
    super.onCreate();
    Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();

    }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    intent.getStringExtra("MY_MESSAGE");
    //stopSelf();
    return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();

}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

}
</code></pre>

<p>This code is not working. The service starting when the startService calling in MainActivity. But when a dialer open it not working.</p>