2013-03-27 31 views
3

如何在檢測到來電時啓動新的活動。在下面的代碼我想在CALL_STATE_RINGING狀態開始新的活動在服務運行期間創建新的活動

public String getCurrentCallState(final TelephonyManager mytelMgr) { 
     int callState = mytelMgr.getCallState(); 
     String callStateString = "NOTKNOWN"; 
     switch (callState) { 
      case TelephonyManager.CALL_STATE_IDLE: 
       callStateString = "IDLE"; 
       break; 
      case TelephonyManager.CALL_STATE_OFFHOOK: 
       callStateString = "OFFHOOK"; 
       break; 
      case TelephonyManager.CALL_STATE_RINGING: 
       callStateString = "RINGING"; 
       break; 
     } 
} 

現在,我做了一些改變。在* CALL_STATE_RINGING *狀態,但它無法正常工作。每當通話即將到來,我的應用程序正在退出。 其實我想在來電鈴聲時致電該活動。

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.telephony.PhoneStateListener; 
import android.telephony.TelephonyManager; 
import android.widget.Toast; 


public class CallHelper extends Activity 
{ 


    /** 
    * Listener to detect incoming calls. 
    */ 
    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 
       Intent i = new Intent(getApplicationContext(), out.class); 
       i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //must be provided 
       getApplicationContext().startActivity(i); 



      Toast.makeText(ctx, "Incoming: "+incomingNumber, Toast.LENGTH_LONG).show(); 


       break; 
      } 
     } 
    } 

    /** 
    * Broadcast receiver to detect the outgoing calls. 
    */ 
    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(); 
    } 

    /** 
    * Start calls detection. 
    */ 
    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); 
    } 

    /** 
    * Stop calls detection. 
    */ 
    public void stop() { 
     tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE); 
     ctx.unregisterReceiver(outgoingReceiver); 
    } 

} 

回答

2

有沒有區別 - 除了一個重要標誌 - 從Service甚至BroadcastReceiver啓動一個Activity時。請使用您 CALL_STATE_RINGING情況如下:

Intent i = new Intent(getApplicationContext(), YourActivity.class); 
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //must be provided 
getApplicationContext().startActivity(i); 

確保您的發射活動是在AndroidManifest過定義。