Since API 21 you can use
public AlarmManager.AlarmClockInfo getNextAlarmClock ()
http://developer.android.com/reference/android/app/AlarmManager.html#getNextAlarmClock()
Now, the code that you are trying to use:
(PendingIntent.getBroadcast(MainActivity.this, 0,
new Intent(MainActivity.this, AlarmReceiver.class), PendingIntent.FLAG_NO_CREATE) != null);
Bassically you are asking for a previus and existent intent called AlarmReceiver. But his AlarmReceiver refers to your own BroadcastReceiver.
As you can see in this line of the answer that you posted before:
boolean alarmUp = (PendingIntent.getBroadcast(context, 0,
new Intent("com.my.package.MY_UNIQUE_ACTION"),
PendingIntent.FLAG_NO_CREATE) != null);
They use "MY_UNIQUE_ACTION" to see if the intent exists.
Also in this site you can see a tutorial using that:
http://justcallmebrian.com/2010/04/27/using-alarmmanager-to-schedule-activities-on-android/
Unless you are able to access the android-system AlarmReceiver and see if the intent exists, you will not be able to ask for a 'generic' scheduled alarm. Is that what you are trying to do? If it's the case, are you sure that in case that you don't have any scheduled alarm the android-system AlarmReceiver intent was not created by the system anyway?
Seems we don't have control to those components like we like to.
An working example:
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.blablabla.testa" >
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver
android:name=".MySuperReceiver"
android:label="MySuperReceiverName" />
</activity>
</application>
MySuperReceiver.java
package com.blablabla.testa;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import java.util.Calendar;
public class MySuperReceiver extends BroadcastReceiver {
public static final String TAG = MySuperReceiver.class.getSimpleName();
public static final String ACTION_ALARM_RECEIVER = "ACTION_ALARM_RECEIVER";
private Calendar c = Calendar.getInstance();
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null)
if (ACTION_ALARM_RECEIVER.equals(intent.getAction())) {
Log.d(TAG, new Exception().getStackTrace()[0].getMethodName() + " " + c.getTime());
//do something here
}
}
}
And MainActivity.java:
package com.blablabla.testa;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
public static final String TAG = "TEST APP:";
AlarmManager alarmManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetAlarmService();
CreateAlarm();
CheckForAlarm();
CancelAlarms();
CheckForAlarm();
}
private void GetAlarmService()
{
alarmManager = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
Log.d(TAG, " GET Alarm Service ! ");
}
private void CreateAlarm()
{
long aroundInterval = 1*60*1000;
Intent intent = new Intent(getApplicationContext(), MySuperReceiver.class);
intent.setAction(MySuperReceiver.ACTION_ALARM_RECEIVER);//my custom string action name
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);//used unique ID as 1001
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), aroundInterval, pendingIntent);//first start will start asap
Log.d(TAG, " CREATE Alarm ! ");
}
private void CancelAlarms()
{
Intent intent = new Intent(getApplicationContext(), MySuperReceiver.class);//the same as up
intent.setAction(MySuperReceiver.ACTION_ALARM_RECEIVER);//the same as up
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 1001, intent, PendingIntent.FLAG_CANCEL_CURRENT);//the same as up
alarmManager.cancel(pendingIntent);//important
pendingIntent.cancel();//important
Log.d(TAG, " Cancel Alarm ! ");
}
private void CheckForAlarm()
{
//checking if alarm is working with pendingIntent #3
Intent intent = new Intent(getApplicationContext() , MySuperReceiver.class);//the same as up
intent.setAction(MySuperReceiver.ACTION_ALARM_RECEIVER);//the same as up
boolean isWorking = (PendingIntent.getBroadcast(getApplicationContext() , 1001, intent, PendingIntent.FLAG_NO_CREATE) != null);//just changed the flag
Log.d("TAG: TEST APP: ", "alarm is " + (isWorking ? "" : "not") + " working...");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}