I wrote a sample for you and i tested it, i hope this help you.
I think you have to change AlarmManager.RTC to AlarmManager.RTC_WAKEUP in your app .
Notice 1 : if your phone shutdown or restart you have to reset your alarm(s) again when the phone bootup, so you have to save your alarm(s) in shared preferences or etc and reset when phone bootup.
Notice 2: Also you need a broadcast receiver for know when the phone booted up.
Notice 3: AlarmManager works in all Android versions perfectly.
MainActivity
public class MainActivity extends AppCompatActivity {
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmManager =(AlarmManager) getSystemService(ALARM_SERVICE);
findViewById(R.id.btnSetAlarm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 4);
calendar.set(Calendar.MINUTE, 33);
calendar.set(Calendar.SECOND, 10);
calendar.set(Calendar.AM_PM, Calendar.PM);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 243619,intent,0 );
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);
Toast.makeText(MainActivity.this, "Alarm set", Toast.LENGTH_LONG).show();
}
});
}
}
AlarmReceiver
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri==null)
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
RingtoneManager.getRingtone(context, alarmUri).play();
Toast.makeText(context, "Wake up!!! Wake up!!!", Toast.LENGTH_LONG).show();
}catch (Exception ex){
Log.d("AlarmReceiver", ex.getMessage());
}
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.alarmmanager">
<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">
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"></receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Feel free to ask any questions.