4

This is the code i am using for detecting incoming calls and i am toasting the incoming number at the same time, i want to launch my application automatically so please help me do this.Thanks in advance. I have referred to the solution of this question before also and in that the questioner was not able to detect incoming calls and the answer showed how to detect the incoming calls but i have already done that as shown in the code above and now i want code to launch my application automatically whenever the phone rings but i could not find solution to this there.

public class CallHelper extends Activity {

String incomingNo;

private class CallStateListener extends PhoneStateListener {
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            // called when someone is ringing to this phone

            incomingNo = incomingNumber;
            Toast.makeText(ctx, 
                    "Incoming no: "+incomingNo,
                    Toast.LENGTH_SHORT).show();


            break;
        }

    }
}


public class OutgoingReceiver extends BroadcastReceiver {
    public OutgoingReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        Toast.makeText(ctx, 
                "Outgoing: "+number, 
                Toast.LENGTH_LONG).show();
    }

}

private Context ctx;
private TelephonyManager tm;
private CallStateListener callStateListener;

private OutgoingReceiver outgoingReceiver;

public CallHelper(Context ctx) {
    this.ctx = ctx;
    callStateListener = new CallStateListener();
    outgoingReceiver = new OutgoingReceiver();

}
public String returnData(){
    String incoming = incomingNo;
    start();
    callStateListener = new CallStateListener();
    Log.i("check","this is return data returning :"+incoming);
    return incoming;
}

public void start() {
    tm = (TelephonyManager) ctx.getSystemService(Context.TELEPHONY_SERVICE);
    tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
    ctx.registerReceiver(outgoingReceiver, intentFilter);
}

public void stop() {
    tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
    ctx.unregisterReceiver(outgoingReceiver);
}

}
Tarun Talreja
  • 163
  • 1
  • 12
  • No it is not a duplicate i have referred to that answer before also and in that the questioner was not able to detect incoming calls and the answer showed how to detect the incoming calls but i have already done that as shown in the code above and now i want code to launch my application automatically whenever the phone rings but i could not find solution to this there. – Tarun Talreja Aug 03 '16 at 19:09
  • @TarunTalreja You tried registering broadcast receivers in the manifest? This should trigger the code in your receivers EVEN if your application is not running. – Kariem Aug 03 '16 at 19:22
  • `onReceive`. You have a `Context`, yes? Do you know how to start any Activity using the `startActivity` method? – OneCricketeer Aug 03 '16 at 19:32
  • @Kariem yes i have registered broadcast receivers in manifest...look in my code i have used toast to display the incoming and outgoing no and when my app doesn't run then also the toast is seen – Tarun Talreja Aug 04 '16 at 14:42
  • @cricket_007 i tried using the startActivity() method but it doesnot work....as incoming call is received even if my application is closed and on using startActivity() method it shows my application has stopped – Tarun Talreja Aug 04 '16 at 16:10
  • "my application has stopped" -- Which means you are getting an exception, and therefore there is a logcat that you should be able to find, and add to the question – OneCricketeer Aug 04 '16 at 16:22
  • Intent intent = new Intent(ctx, SendNoti.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ctx.startActivity(intent); This code helped me do it – Tarun Talreja Aug 05 '16 at 14:05

1 Answers1

1

On the income call with a simple startActivity can work

    protected void goToSplash() { 
startActivity(new Intent(context, Splash.class)); 
}

or with a broadcastReceiver

https://developer.android.com/reference/android/content/BroadcastReceiver.html

Jason
  • 231
  • 3
  • 14