I want to make an app in which on a button click there will open an alarm activity whether the app is in background or killed or screen locked or not.
ChatGPT provides me these below lines of code but that doesn't do the exact thing I want.
public class MainActivity extends AppCompatActivity {
private AlarmManager alarmManager;
private PendingIntent alarmIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize AlarmManager
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
// Create an intent to your BroadcastReceiver
Intent intent = new Intent(this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
// Set the alarm time (for example, 10 seconds from now)
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, 10);
// Set the alarm using AlarmManager
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}
}