title: How to vibrate phone in Service?
body: <p>I'm programming with android studio. I want a program that vibrates the phone. even if you don't open the app. so it should be in background and I think I have to write it in Service. But it throws an exception. How to fix it?</p>
  
  <p>MainActivity:</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      Intent i= new Intent(MainActivity.this,Silent.class);
      startService(i);
  
  
  }
  </code></pre>
  
  <p>Silent service:</p>
  
  <pre><code>public class Silent extends Service {
  public Silent() {
      AudioManager myAudioManager;
      myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
      myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
  
  }
  </code></pre>
  
  <p>it throws an exception:</p>
  
  <blockquote>
    <p>java.lang.NullPointerException on Silent.java.12</p>
  </blockquote>
  
  <p>it happens on this line: </p>
  
  <pre><code>myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
  </code></pre>