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()必要がありますContext。通常、これをaServiceまたはから呼び出します。Activityどちらも。を拡張しContextます。

あなたの場合、あなたのパラメータは拡張しないthisクラスです。これはプライベート内部クラスだと思います。次のように外部クラスを指定する必要があります。PhoneStateListenerContext

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

外部クラスがアクティビティである場合は、アクティビティMyActivityの名前にする必要があります。外部クラスがサービスの場合は、サービスMyActivityの名前にする必要があります。

于 2012-09-25T13:00:35.787 に答える