BroadcastReceiver and AlarmManager Android
Asked Answered
C

4

5

I am trying to use an alarm manager with BroadcastReceiver. I try to use the example given in Tutorial: System Services and BroadcastReceiver . but when I run the example after the end of the time the toast doesn't show up. the code is below:

My main Activity:

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();
    }


}

my broadCastReceiver:

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);
    }

}

my main Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >

    <EditText
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="Number of seconds"
            android:inputType="numberDecimal" >
    </EditText>

    <Button
            android:id="@+id/ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="startAlert"
            android:text="Start Counter" >
    </Button>

</LinearLayout>

and the manifestfile:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.broadcastreceiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.VIBRATE" >
    </uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.broadcastreceiver.AlarmActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <receiver android:name="com.example.android_alarm.MyBroadcastReceiver" >
        </receiver>
    </application>

</manifest>
Calicle answered 29/1, 2014 at 11:28 Comment(3)
Could you please update the link to the real tutorial and not the main page? Hard to find the right example you mentioned without that link.Occiput
@user3156767, did you ever get this to work? I have tried everything, and still mine does not work.Hemia
Thanks for this, I didn't realize the alarm receiver needed to be in the manifest as well.Despatch
D
6

The receiver must extend from BroadcastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {
    // ...
}

Also be sure that receiver name in manifest file is correct.

Disparage answered 29/1, 2014 at 11:41 Comment(0)
P
2

Your MyBroadcastReceiver class is wrong, edit your MyBroadcastReceiver class to the following:

public class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override
    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);    
    }
}

You need to add extends BroadcastReceiver and override onReceive() method.

Platino answered 29/1, 2014 at 11:43 Comment(0)
W
0

You just forgot to override OnReceive() method. public void onReceive(Context context, Intent intent) is predefined method of BroadcastReceiver, wheneven you are using it, You must @Override it as follows,

public class MyBroadcastReceiver extends BroadcastReceiver
{
    @Override   // You forgot this line 
    public void onReceive(Context context, Intent intent) 
    {
          Toast.makeText(context, "Don't panik but your time is up!!!!.",Toast.LENGTH_LONG).show();
    }
}
Weekender answered 29/1, 2014 at 12:3 Comment(0)
F
0

You have created a startAlert function which u didn't call anywhere.So first call that method in onCreate and then you will receive a toast.

Frog answered 1/5, 2017 at 6:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.