android - 从服务调用 Activity onCallStateChange

标签 android android-intent call

我在 Service 中创建了 PhoneStateListenerToast 消息工作正常,但我想在来电时运行另一个 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;
}
}

最佳答案

使用FLAG_ACTIVITY_NEW_TASK旗帜。

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

关于android - 从服务调用 Activity onCallStateChange,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11560399/

相关文章:

android - Intent.ACTION_CALL 启动 skype 通话而不是 "normal"电话

android - http 方案 Intent 过滤器,用于从网络链接到应用程序

android - 如何检测传入的 Skype/Viber 调用

delphi - 属性未写入,仅在 Delphi XE2 中读取

android - 将数据列表推送到 firebase 不起作用

android - Google Analytics V2 未从 Android 应用记录统计信息

java - 当您的应用程序通过通知打开时,是否可以从打开的位置返回到上一个应用程序?安卓

java - Android 抽屉导航 fragment

java - 有没有办法明确告诉 Android 系统将我的 Intent 解析为当前 Intent 操作的默认应用程序/Activity ?

特定时间段后的 C++ 调用函数 - 无提升