Exception on getSystemService(Context.AUDIO_SERVICE)

♀尐吖头ヾ 提交于 2019-12-01 12:11:53

The call to getSystemService(...) will not work before onCreate() is called by the Android framework. This happens when the service is started (i.e. by [Context#bindService(...)][1] or Context#startService(...)). I've seen the same NPE when trying to call getSystemService() from a constructor (i.e. before onCreate() is called).

You're simply calling (new TMLService()).ManageIncomingCall(incomingNumber), which doesn't allow Android to initialize your service, which is the root cause of this NPE.

In order to get it working, you'll have to start the service and then call a method on the service. To call a method, I think you have to expose it using AIDL. It might be more complicated than you need for this (maybe?).

I've heard that IntentService is an easier way to do stuff in a service without the complexity of AIDL. Here's a sample of how I think IntentService should work. Haven't tested it, but hopefully its useful to get started.

CallReceiver

public class CallReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        MyPhoneStateListener phoneListener = new MyPhoneStateListener(context);
        TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
    }

}

MyPhoneStateListener

public class MyPhoneStateListener extends PhoneStateListener {
    private final Context mContext;

    public MyPhoneStateListener(Context context) {
        this.mContext = context;
    }

    public void onCallStateChanged(int state, String incomingNumber){

        if (state == TelephonyManager.CALL_STATE_RINGING)
        {
            Log.d("DEBUG", "RINGING");

            // OPTION 1: Do it on the main thread (might be bad :) )
            //AudioManager audioManage = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
            //audioManage.setRingerMode(AudioManager.RINGER_MODE_SILENT);

            // OPTION 2: Use an IntentService (a bit easier than AIDL)
            Intent intent = new Intent(TMLIntentService.ACTION_SILENCE_RINGER);
            mContext.startService(intent);
        }
    }

}

TMLIntentService

public class TMLIntentService extends IntentService {
    public static final String ACTION_SILENCE_RINGER = "org.example.intentservice.ACTION_SILENCE_RINGER";

    @Override
    public void onHandleIntent(Intent intent) {
        if(ACTION_SILENCE_RINGER.equals( intent.getAction() ) {
            AudioManager audioManage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
            audioManage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        }
    }
}

AndroidManifest.xml

<service android:name=".TMLIntentService">
    <intent-filter>
        <action android:name="org.example.intentservice.ACTION_SILENCE_RINGER" />
     </intent-filter>
</service>

[1]: http://d.android.com/reference/android/content/Context.html#bindService(android.content.Intent, android.content.ServiceConnection, int)

Do you have the correct permission? If you are missing a perm, then the app will complain about this in the logs somewhere

public void ManageIncomingCall(String incomingNumber)  
{
    super.onCreate();
    AudioManager audioManage = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    audioManage.setRingerMode(AudioManager.RINGER_MODE_SILENT);
}

Why Are you calling super.onCreate() in a method other than onCreate()? That sounds really, really wrong.

E/AndroidRuntime( 356): Uncaught handler: thread main exiting due to uncaught exception

The first thing I'd do is surround the code in ManageIncomingCall() with a try/catch block. It might at least give an explanation as to what is going on.

E/AndroidRuntime(  356): java.lang.NullPointerException
E/AndroidRuntime(  356):    at android.content.ContextWrapper.getSystemService(ContextWrapper.java:335)
E/AndroidRuntime(  356):    at tml.v1.Service.TMLService.ManageIncomingCall(TMLService.java:94)

You are getting a NullPointerException at line 94 of TMLService.java, I am guessing that this is the line where you call:

audioManage.setRingerMode(AudioManager.RINGER_MODE_SILENT);

and I am guessing that audioManage is null.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!