Exception on getSystemService(Context.AUDIO_SERVICE)

后端 未结 5 790
渐次进展
渐次进展 2021-01-16 14:39

I wanted to create an app that drops an incoming call based on some settings, that seems to be impossible on Android 1.6. So I decided to write an app that changes the Ringe

相关标签:
5条回答
  • 2021-01-16 15:15

    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.

    0 讨论(0)
  • 2021-01-16 15:18
    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.

    0 讨论(0)
  • 2021-01-16 15:19

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

    0 讨论(0)
  • 2021-01-16 15:26

    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)

    0 讨论(0)
  • 2021-01-16 15:37
    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.

    0 讨论(0)
提交回复
热议问题