2016-08-03 17 views
2

這是我用來檢測來電的代碼,我在同一時間敬酒來電號碼,我想自動啓動我的應用程序,所以請幫助我做到這一點。提前感謝。 我之前也提到過這個問題的解決方案,並且提問者無法檢測到來電,答案顯示如何檢測來電,但我已經完成了上述代碼中顯示的內容,現在我想要代碼自動啓動我的應用程序,每當手機響起,但我找不到解決方案。如何在來電時自動啓動我的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); 
} 

} 
+0

不,它不是重複我已經提到這也回答了,因爲提問者無法檢測到傳入的cal ls和答案顯示瞭如何檢測來電,但我已經完成了上面的代碼,現在我希望代碼自動啓動我的應用程序,只要手機響起,但我找不到解決方案。 –

+0

@TarunTalreja您試過在清單中註冊廣播接收器嗎?即使您的應用程序沒有運行,這應該會觸發您的接收器中的代碼。 – Kariem

+0

'onReceive'。你有一個'Context',是嗎?你知道如何使用'startActivity'方法啓動任何Activity嗎? –

回答

相關問題