Search code examples
androidbroadcastreceiverandroid-servicealarmmanager

(android) AlarmManager doesn't seem to be working


I'm trying to setup an alarm manager but it doesn't work, I'm currently using a test AlarmReceiver which should start my ringtone. I'm not sure what the issue is, is the setup different because I'm in a Fragment?

Its currently set to ring after 5 minutes, I read somewhere that was the minimum time, not sure if its true.

For the full source code I've uploaded the project onto github.

Below is the fragment:

public class TaskFragment extends Fragment {
...
 @Override
    public void onPause() {
        super.onPause();

        Intent intent = new Intent(getContext(), AlarmReceiver.class);
        intent.putExtra("time", mTask.getReminder().getTime());

        PendingIntent alarmIntent = PendingIntent.getBroadcast(getContext(), 10, intent, 0);

        AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(getContext().ALARM_SERVICE);
        alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 5 * 60 * 1000, alarmIntent);

        Log.i("Pending AlarmIntent", "Created");

Below is a test AlarmReceiver:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.util.Log;

import java.util.Date;

/**
 * Created by rmatos on 12/08/17.
 */

public class AlarmReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(final Context context, Intent intent) {
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        Ringtone ringtone = RingtoneManager.getRingtone(context, uri);
        ringtone.play();
}
}

Manifest:

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

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>                     <!-- TODO: Implement alarm manager that works when phone restarted -->

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".TaskListActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>

    <activity
        android:name=".TaskPagerActivity"
        android:parentActivityName=".TaskListActivity">
        <intent-filter>
            <action android:name="android.intent.action.SET_ALARM" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

        <receiver
            android:process=":remote"
            android:name=".AlarmReceiver"
            android:enabled="true"
            android:exported="true"/>
    </activity>


</application>


Solution

  • Just make changes in manifest.xml

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    
        <activity android:name=".TaskListActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
    
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    
        <activity
            android:name=".TaskPagerActivity"
            android:parentActivityName=".TaskListActivity">
            <intent-filter>
                <action android:name="android.intent.action.SET_ALARM" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    <receiver
                android:process=":remote"
                android:name=".AlarmReceiver"
                android:enabled="true"
                android:exported="true"/>
    
    </application>
    

    Hope this will help u..if not than pls let me know