A J
A J

Reputation: 4652

Want to listen incoming call and out going calls

I want to listen incoming calls and outgoing calls and this process should run as service.I made a activity which is working fine to recognize incoming and outgoing calls but I need to change it to service so that it can run into background.I can't find out how to change it.My activity follows:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
    mTelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
}

class TeleListener extends PhoneStateListener
{
    public void onCallStateChanged(int state, String incomingNumber)
    {
        super.onCallStateChanged(state, incomingNumber);
        switch (state)
        {
        case TelephonyManager.CALL_STATE_IDLE:
            //CALL_STATE_IDLE;
            Toast.makeText(getApplicationContext(), "CALL_STATE_IDLE", 10000).show();
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //CALL_STATE_OFFHOOK;
            Toast.makeText(getApplicationContext(), "CALL_STATE_OFFHOOK", 10000).show();
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            //CALL_STATE_RINGING
            Toast.makeText(getApplicationContext(), "CALL_STATE_RINGING", 10000).show();
            break;
        default:
            break;
        }
    }

  }
}

And my Second question can I install a service without an activity into my phone.

Upvotes: 0

Views: 2685

Answers (2)

StoneBird
StoneBird

Reputation: 1940

You need to pass an intent to start a service. You can create an activity that only starts your service, and this activity does not have a view. Make your class extend Service rather than Activity, and create another activity that instantiates your service and executes startService(yourserviceInstance);

Upvotes: 2

Hoan Nguyen
Hoan Nguyen

Reputation: 18151

Just extends Service instead of Activity. You do need an activity to start a service. You can start your service at boot by register a ACTION_BOOT_COMPLETED and have your broadcast receiver start your service. However, from ICS upward you need an activity to launch your service at least once. After that the activity will never be needed again.

Upvotes: 3

Related Questions