1

我有一个phonestatelistener:我怎样才能在这里开始服务?我试过了:

startService(new Intent(this, TTS.class));

它怎么对我有用

private PhoneStateListener mPhoneListener = new PhoneStateListener() {

          @SuppressLint("SdCardPath")
        public void onCallStateChanged(int state, String incomingNumber) {

           try {
            switch (state) {
            case TelephonyManager.CALL_STATE_RINGING:
                //i want to start service
                startService(new Intent(this, TTS.class));
4

1 回答 1

1

你打电话时

startService(new Intent(this, TTS.class));

的第一个参数new Intent()必须是 a Context。通常你从 aService或从 an调用它Activity,两者都是 extend Context

在您的情况下,您的this参数是PhoneStateListener不扩展的类Context。我假设这是一个私有内部类。您需要像这样指定外部类:

startService(new Intent(MyActivity.this, TTS.class));

如果你的外部类是一个活动,那么MyActivity应该是活动的名称。如果外部类是服务,那么MyActivity应该是服务的名称。

于 2012-09-25T13:00:35.787 回答