如何在来电时自动启动我的Android应用程序?

时间:2016-08-03 17:26:34

标签: android

这是我用来检测来电的代码,我正在同时烘烤传入号码,我想自动启动我的应用程序,所以请帮我这样做。谢谢提前。 我之前也提到了这个问题的解决方案,并且提问者无法检测到来电,答案显示如何检测来电,但我已经完成了,如上面的代码所示,现在我想要代码每当手机响铃时自动启动我的应用程序,但我找不到解决方案。

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);
}

}

1 个答案:

答案 0 :(得分:1)

使用简单的startActivity进行收入调用可以正常工作

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

或使用broadcastReceiver

  

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