title: Why does the device vibrate from an Activity, but not from within a Class (file)?
body: <p>I want my app to vibrate the device using my app's custom vibration pattern. I can do this from MainActivity, or any Activity, but I don't know why it isn't working from within a Java class (SmsReceiver.java).</p>
  
  <p>I thought that if I use <code>...context.getApplicationContext.getSystemService(Context.VIBRATOR_SERVICE);</code> it would get the MainActivity context and thus would be kind of like I was in the MainActivity and it would vibrate. But nope. It doesn't vibrate from the Receiver class.</p>
  
  <p>How do I vibrate the phone from the SmsReceiver.java class?</p>
  
  <p>SmsReceiver.java:</p>
  
  <pre><code>package com.app.name.app;
  
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.os.Bundle;
  import android.os.Vibrator;
  import android.telephony.SmsMessage;
  import android.util.Log;
  import android.widget.Toast;
  
  public class SmsReceiver extends BroadcastReceiver {
      private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
  
      @Override
      public void onReceive(Context context, Intent intent) {
          if (intent.getAction().equals(SMS_RECEIVED)) {
              Bundle bundle = intent.getExtras();
  
              if (bundle != null) {
                  // get sms objects
                  Object[] pdus = (Object[]) bundle.get("pdus");
                  if (pdus.length == 0) {
                      return;
                  }
                  // large message might be broken into many
                  SmsMessage[] messages = new SmsMessage[pdus.length];
                  StringBuilder sb = new StringBuilder();
  
                  for (int i = 0; i &lt; pdus.length; i++) {
                      messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                      sb.append(messages[i].getMessageBody());
                  }
  
                  String sender = messages[0].getOriginatingAddress();
                  Log.d("SNDR", sender);
                  String message = sb.toString();
  
                  // prevent any other broadcast receivers from receiving broadcast
                  abortBroadcast();
                  if(!message.isEmpty())
                  {
                      if(message == "!")
                      {
                          Vibrator v = (Vibrator)context.getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                          v.cancel();
  
                          long[] longs = { 0, 75, 15, 100, 15, 100, 15, 75, 15 };
                          v.vibrate(longs, -1);
                      }
                  }
  
                  Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
  
              }
          }
      }
  }
  </code></pre>
  
  <p>MainActivity.java, onButtonClick:</p>
  
  <pre><code>switch(id)
  {
      case R.id.action_search:
          Vibrator v = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
  
          long[] longs = { 0, 75, 15, 100, 15, 100, 15, 75, 15 };
          v.vibrate(longs, -1);
          break;
      case R.id.action_settings:
          break;
  }
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/23173322/creating-a-vibration-in-a-non-activity-class">This</a> related question does not solve this issue.</p>