Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The call to <code>getSystemService(...)</code> will not work before <code>onCreate()</code> is called by the Android framework. This happens when the service is started (i.e. by [Context#bindService(...)][1] or <a href="http://d.android.com/reference/android/content/Context.html#startService(android.content.Intent)" rel="noreferrer">Context#startService(...)</a>). I've seen the same NPE when trying to call <code>getSystemService()</code> from a constructor (i.e. before <code>onCreate()</code> is called).</p> <p>You're simply calling <code>(new TMLService()).ManageIncomingCall(incomingNumber)</code>, which doesn't allow Android to initialize your service, which is the root cause of this NPE.</p> <p>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 <a href="http://developer.android.com/guide/developing/tools/aidl.html" rel="noreferrer">AIDL</a>. It might be more complicated than you need for this (maybe?). </p> <p>I've heard that <a href="http://developer.android.com/reference/android/app/IntentService.html" rel="noreferrer">IntentService</a> 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.</p> <p>CallReceiver</p> <pre><code>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); } } </code></pre> <p>MyPhoneStateListener</p> <pre><code>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); } } } </code></pre> <p>TMLIntentService</p> <pre><code>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); } } } </code></pre> <p>AndroidManifest.xml</p> <pre><code>&lt;service android:name=".TMLIntentService"&gt; &lt;intent-filter&gt; &lt;action android:name="org.example.intentservice.ACTION_SILENCE_RINGER" /&gt; &lt;/intent-filter&gt; &lt;/service&gt; </code></pre> <p>[1]: <a href="http://d.android.com/reference/android/content/Context.html#bindService(android.content.Intent" rel="noreferrer">http://d.android.com/reference/android/content/Context.html#bindService(android.content.Intent</a>, android.content.ServiceConnection, int)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload