java.lang.RuntimeException:无法实例化接收器

时间:2013-03-09 06:48:29

标签: android broadcastreceiver telephonymanager phone-state-listener

我正在开发一个应用程序,该应用程序会进入用户的消息,电话簿查找和呼叫状态。

应用程序运行得非常好,因为当我接到电话时,我的应用程序崩溃了。

这是我的一些用户报告的错误。

> java.lang.RuntimeException: Unable to instantiate receiver
> me.radhakrishna.buddyreader.CustomPhoneStateListener:
> java.lang.InstantiationException: can't instantiate class
> me.radhakrishna.buddyreader.CustomPhoneStateListener; no empty
> constructor at
> android.app.ActivityThread.handleReceiver(ActivityThread.java:2105) at
> android.app.ActivityThread.access$1500(ActivityThread.java:123) at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
> at android.os.Handler.dispatchMessage(Handler.java:99) at
> android.os.Looper.loop(Looper.java:137) at
> android.app.ActivityThread.main(ActivityThread.java:4450) at
> java.lang.reflect.Method.invokeNative(Native Method) at
> java.lang.reflect.Method.invoke(Method.java:511) at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
> at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) at
> dalvik.system.NativeStart.main(Native Method) Caused by:
> java.lang.InstantiationException: can't instantiate class
> me.radhakrishna.buddyreader.CustomPhoneStateListener; no empty
> constructor at java.lang.Class.newInstanceImpl(Native Method) at
> java.lang.Class.newInstance(Class.java:1319) at
> android.app.ActivityThread.handleReceiver(ActivityThread.java:2100)
> ... 10 more

这是我的

CustomPhoneStateListener.java

文件

package me.radhakrishna.buddyreader;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract.PhoneLookup;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;

public class CustomPhoneStateListener extends PhoneStateListener {
    final MainActivity main = new MainActivity();
    //private static final String TAG = "PhoneStateChanged";
    Context context; //Context to make Toast if required 
    public CustomPhoneStateListener(Context context) {
        super();
        this.context = context;
    }

    public void phonePlayer(String incomingNumber){
        Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(incomingNumber));
        Cursor cursor= context.getContentResolver().query(uri, new String[]{PhoneLookup.DISPLAY_NAME},null,null,null);

        if (cursor.getCount()>0){
            cursor.moveToFirst();
            String contactName = cursor.getString(0);
            cursor.close();

            String enteredText = "Incoming call from "+contactName;
            String words = enteredText.toString();
            main.speakWords(words);
        }else{
            String enteredText = "Incoming call from "+incomingNumber;
            String words = enteredText.toString();
            main.speakWords(words);
        }
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        super.onCallStateChanged(state, incomingNumber);

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            //when Idle i.e no call
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            //when Off hook i.e in call
            //Make intent and start your service here
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            //when Ringing
            main.stopReading();
            phonePlayer(incomingNumber);
            break;
        default:
            break;
        }
    }
}

这是我的TextMessageReceiver.java

TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);

我的清单文件

<receiver android:name=".CustomPhoneStateListener">
    <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>

任何人都可以帮助我吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

PhoneStateListener不是接收者,因此您无法将其放入清单。

如果手机状态发生变化,Android会发送android.intent.action.PHONE_STATE广播。

所以你应该:

public class CustomPhoneStateReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
    }
}

将该接收器放入清单文件中。

<receiver android:name=".CustomPhoneStateReceiver">
    <intent-filter>
            <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>