title: BroadcastReceiver and AlarmManager Android
body: <p>I am trying to use an alarm manager with BroadcastReceiver. I try to use the example given in <a href="http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html" rel="nofollow noreferrer">Tutorial: System Services and BroadcastReceiver</a> . but when I run the example after the end of the time the toast doesn't show up. the code is below:</p>
  
  <p>My main Activity:</p>
  
  <pre><code>public class AlarmActivity extends Activity {
  
  
      /** Called when the activity is first created. */
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_alarm);
      }
  
      public void startAlert(View view) {
          EditText text = (EditText) findViewById(R.id.time);
          int i = Integer.parseInt(text.getText().toString());
          Intent intent = new Intent(this, MyBroadcastReceiver.class);
          PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 234324243, intent, 0);
          AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
          alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
                  + (i * 1000), pendingIntent);
          Toast.makeText(this, "Alarm set in " + i + " seconds",
                  Toast.LENGTH_LONG).show();
      }
  
  
  }
  </code></pre>
  
  <p>my broadCastReceiver:</p>
  
  <pre><code>public class MyBroadcastReceiver {
      public void onReceive(Context context, Intent intent) {
          Toast.makeText(context, "Don't panic but your time is up!!!!.",
                  Toast.LENGTH_LONG).show();
          // Vibrate the mobile phone
          Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
          vibrator.vibrate(2000);
      }
  
  }
  </code></pre>
  
  <p>my main Layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" &gt;
  
      &lt;EditText
              android:id="@+id/time"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:hint="Number of seconds"
              android:inputType="numberDecimal" &gt;
      &lt;/EditText&gt;
  
      &lt;Button
              android:id="@+id/ok"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:onClick="startAlert"
              android:text="Start Counter" &gt;
      &lt;/Button&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>and the manifestfile:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.broadcastreceiver"
      android:versionCode="1"
      android:versionName="1.0" &gt;
  
      &lt;uses-sdk
          android:minSdkVersion="8"
          android:targetSdkVersion="18" /&gt;
      &lt;uses-permission android:name="android.permission.VIBRATE" &gt;
      &lt;/uses-permission&gt;
  
      &lt;application
          android:allowBackup="true"
          android:icon="@drawable/ic_launcher"
          android:label="@string/app_name"
          android:theme="@style/AppTheme" &gt;
          &lt;activity
              android:name="com.example.broadcastreceiver.AlarmActivity"
              android:label="@string/app_name" &gt;
              &lt;intent-filter&gt;
                  &lt;action android:name="android.intent.action.MAIN" /&gt;
  
                  &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
              &lt;/intent-filter&gt;
          &lt;/activity&gt;
           &lt;receiver android:name="com.example.android_alarm.MyBroadcastReceiver" &gt;
          &lt;/receiver&gt;
      &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>