我正在开发一个应用程序,它可以处理用户的消息,电话簿查找和呼叫状态。
应用程序完美运行,就像我接到电话一样。
这是某些用户报告的错误。
> 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>
谁能帮我 ?提前致谢。
最佳答案
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>
关于android - java.lang.RuntimeException:无法实例化接收者,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15308122/