从服务调用活动onCallStateChange

时间:2012-07-19 11:59:02

标签: android android-intent call

我在PhoneStateListener中创建了ServiceToast消息正常,但我想在来电时再运行Activity。但它不会开始。这堂课怎么了?这是一个主要Activity的代码,DialogAct是一个带有布局的空Activity

public class GSMListenerService extends Service {
private TelephonyManager tm;

 @Override
 public void onCreate() {
  super.onCreate();

  tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
  tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
 }

 private PhoneStateListener mPhoneListener = new PhoneStateListener() {
     public void onCallStateChanged(int state, String incomingNumber) {
         try {
             switch (state) {
             case TelephonyManager.CALL_STATE_RINGING:
                 Intent i = new Intent(GSMListenerService.this,DialogAct.class);
                 startActivity(i);
                 Toast.makeText(GSMListenerService.this, "CALL_STATE_RINGING: ", Toast.LENGTH_SHORT).show();

                 break;
             case TelephonyManager.CALL_STATE_OFFHOOK:
                 Toast.makeText(GSMListenerService.this, "CALL_STATE_OFFHOOK", Toast.LENGTH_SHORT).show();
                 break;
             case TelephonyManager.CALL_STATE_IDLE:
                 Toast.makeText(GSMListenerService.this, "CALL_STATE_IDLE", Toast.LENGTH_SHORT).show();
                 break;
             default:
                 Toast.makeText(GSMListenerService.this, "default", Toast.LENGTH_SHORT).show();
                 Log.i("Default", "Unknown phone state=" + state);
             }
        } catch (Exception e) {
            Log.i("Exception", "PhoneStateListener() e = " + e);
        }
    }
};

@Override
public void onDestroy() {
    super.onDestroy();
}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);
}

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
}

1 个答案:

答案 0 :(得分:1)

使用FLAG_ACTIVITY_NEW_TASK标志。

Intent intent = new Intent(GSMListenerService.this, DialogAct.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);