简体   繁体   English

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

[英]java.lang.RuntimeException: Unable to instantiate receiver : java.lang.ClassNotFoundException

  • I have tried to implement Auto Answer in my project using telephony manager and BroadCastReceiver . 我尝试使用电话管理器和BroadCastReceiver在我的项目中实现自动应答。
  • Its working fine But Unfortuantly the APP Crashes when i power on mobile again. 它工作正常,但不幸的是,当我再次打开手机电源时,APP崩溃了。 Herewith I have attached my code and manifest file also.kindly can any one help us for a solution 特此附上我的代码和清单文件。任何人都可以帮助我们寻求解决方案

Code : 代码:

AutoAnswerReceiver .java AutoAnswerReceiver .java

public class AutoAnswerReceiver extends BroadcastReceiver {

    SharedPreferences mPrefs;
    static String PREFS_NAMES = "AutoAnswer";
    @Override
    public void onReceive(Context context, Intent intent) {

        mPrefs = context.getSharedPreferences(PREFS_NAMES, 0);

        String AutoResult = mPrefs.getString("AutoAnswer", "FALSE");

        // Check phone state
        String phone_state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
        String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);

        if (phone_state.equals(TelephonyManager.EXTRA_STATE_RINGING) && AutoResult.equals("TRUE"))
        {
                context.startService(new Intent(context, AutoAnswerIntentService.class));

        }
        }

    **AutoAnswerIntentService**

public class AutoAnswerIntentService extends IntentService {

    public AutoAnswerIntentService() {
        super("AutoAnswerIntentService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Context context = getBaseContext();


        // Let the phone ring for a set delay
        try {
            Thread.sleep(Integer.parseInt("5") * 1000);
        } catch (InterruptedException e) {
            // We don't really care
        }


        // Make sure the phone is still ringing
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm.getCallState() != TelephonyManager.CALL_STATE_RINGING) {
            return;
        }

        // Answer the phone
        try {
            answerPhoneAidl(context);
        }
        catch (Exception e) {
            e.printStackTrace();
            Log.d("AutoAnswer","Error trying to answer using telephony service.  Falling back to headset.");
            answerPhoneHeadsethook(context);
        }

        // Enable the speakerphone

            enableSpeakerPhone(context);

        return;
    }

    private void enableSpeakerPhone(Context context) {
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
        audioManager.setSpeakerphoneOn(true);
    }

    private void answerPhoneHeadsethook(Context context) {
        // Simulate a press of the headset button to pick up the call
        Intent buttonDown = new Intent(Intent.ACTION_MEDIA_BUTTON);     
        buttonDown.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_HEADSETHOOK));
        context.sendOrderedBroadcast(buttonDown, "android.permission.CALL_PRIVILEGED");

        // froyo and beyond trigger on buttonUp instead of buttonDown
        Intent buttonUp = new Intent(Intent.ACTION_MEDIA_BUTTON);       
        buttonUp.putExtra(Intent.EXTRA_KEY_EVENT, new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_HEADSETHOOK));
        context.sendOrderedBroadcast(buttonUp, "android.permission.CALL_PRIVILEGED");
    }

    @SuppressWarnings("unchecked")
    private void answerPhoneAidl(Context context) throws Exception {
        // Set up communication with the telephony service (thanks to Tedd's Droid Tools!)
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        Class c = Class.forName(tm.getClass().getName());
        Method m = c.getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        ITelephony telephonyService;
        telephonyService = (ITelephony)m.invoke(tm);

        // Silence the ringer and answer the call!
        telephonyService.silenceRinger();
        telephonyService.answerRingingCall();
    }
}

Manifestfile 清单文件

<receiver android:name=".AutoAnswerReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE" />
    </intent-filter>
</receiver>
<receiver android:name=".AutoAnswerBootReceiver" android:enabled="true">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>
<service android:name="AutoAnswerIntentService" />

It works fine when app is running.But in case of power on stage it shows the error like 当应用程序运行时它可以正常工作。但是,如果在开机状态下显示错误,例如

ERROR 错误

03-30 09:54:22.013: E/AndroidRuntime(200): Uncaught handler: thread main exiting due to uncaught exception
03-30 09:54:22.023: E/AndroidRuntime(200): java.lang.RuntimeException: Unable to instantiate receiver com.slet.routemytrips.beta.AutoAnswerBootReceiver: java.lang.ClassNotFoundException: com.slet.routemytrips.beta.AutoAnswerBootReceiver in loader dalvik.system.PathClassLoader@43b7dfd8
03-30 09:54:22.023: E/AndroidRuntime(200):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2616)
03-30 09:54:22.023: E/AndroidRuntime(200):  at android.app.ActivityThread.access$3100(ActivityThread.java:119)
03-30 09:54:22.023: E/AndroidRuntime(200):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1913)
03-30 09:54:22.023: E/AndroidRuntime(200):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-30 09:54:22.023: E/AndroidRuntime(200):  at android.os.Looper.loop(Looper.java:123)
03-30 09:54:22.023: E/AndroidRuntime(200):  at android.app.ActivityThread.main(ActivityThread.java:4363)
03-30 09:54:22.023: E/AndroidRuntime(200):  at java.lang.reflect.Method.invokeNative(Native Method)
03-30 09:54:22.023: E/AndroidRuntime(200):  at java.lang.reflect.Method.invoke(Method.java:521)
03-30 09:54:22.023: E/AndroidRuntime(200):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-30 09:54:22.023: E/AndroidRuntime(200):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-30 09:54:22.023: E/AndroidRuntime(200):  at dalvik.system.NativeStart.main(Native Method)
03-30 09:54:22.023: E/AndroidRuntime(200): Caused by: java.lang.ClassNotFoundException: com.slet.routemytrips.beta.AutoAnswerBootReceiver in loader dalvik.system.PathClassLoader@43b7dfd8
03-30 09:54:22.023: E/AndroidRuntime(200):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
03-30 09:54:22.023: E/AndroidRuntime(200):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
03-30 09:54:22.023: E/AndroidRuntime(200):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
03-30 09:54:22.023: E/AndroidRuntime(200):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2609)
03-30 09:54:22.023: E/AndroidRuntime(200):  ... 10 more
03-30 09:54:22.083: I/Process(51): Sending signal. PID: 200 SIG: 3
03-30 09:54:22.102: I/dalvikvm(200): threadid=7: reacting to signal 3
03-30 09:54:22.102: E/dalvikvm(200): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

Here is the class file that's missing : 这是缺少的类文件:

Create a file named AutoAnswerBootReceiver.java 创建一个名为AutoAnswerBootReceiver.java的文件

package com.example.autoanswer;  // Just change the package name to yours

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AutoAnswerBootReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
                AutoAnswerNotifier notifier = new AutoAnswerNotifier(context);
                notifier.updateNotification();
        }

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.runtimeexception无法实例化接收器 - java.lang.runtimeexception unable to instantiate receiver java.lang.RuntimeException:无法实例化活动 ComponentInfo{}:java.lang.ClassNotFoundException - java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{}: java.lang.ClassNotFoundException java.lang.RuntimeException:无法实例化应用程序,java.lang.ClassNotFoundException: - java.lang.RuntimeException: Unable to instantiate application, java.lang.ClassNotFoundException: java.lang.RuntimeException:无法实例化活动ComponentInfo {...}:java.lang.ClassNotFoundException:未找到类 - java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{…}: java.lang.ClassNotFoundException: Didn't find class java.lang.RuntimeException: 无法实例化应用程序 com.todos.MyApplication: java.lang.ClassNotFoundException - java.lang.RuntimeException: Unable to instantiate application com.todos.MyApplication: java.lang.ClassNotFoundException java.lang.RuntimeException:无法实例化应用程序 com.projec.App:java.lang.ClassNotFoundException - java.lang.RuntimeException: Unable to instantiate application com.projec.App: java.lang.ClassNotFoundException 有关hadoop的问题“java.lang.RuntimeException:java.lang.ClassNotFoundException:” - Question on hadoop “java.lang.RuntimeException: java.lang.ClassNotFoundException: ” hadoop,java.lang.RuntimeException:java.lang.ClassNotFoundException错误 - hadoop, java.lang.RuntimeException: java.lang.ClassNotFoundException error java.lang.RuntimeException:无法实例化活动 - java.lang.RuntimeException: Unable to instantiate activity java.lang.RuntimeException:无法实例化活动 - java.lang.RuntimeException: Unable to instantiate activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM