Search code examples
androidandroid-intentalarm

AlarmClock intent says "No apps can perform this action."


I am trying to set an alarm in the alarm clock with an intent. I am using an Android One Phone (which has the unmodified OS) and have the Clock app installed (which came pre-installed) which allows setting alarm. Setting an alarm has worked in the past when I had used an AlarmManager and PendingIntent when I had to set the alarm in the background. That shows that my Clock app can respond to AlarmClock intents. But now when I am trying to send an intent from the foreground of my app, it says:

No apps can perform this action.

This is not from the stack trace, but a popup which is shown to users to choose which Clock app to choose to set the alarm (or which app to use in general for an intent)

Here's the Activity's onCreate() code where I am calling it:

public class MainActivity extends AppCompatActivity {
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        Utils.setAlarm(this, Utils.getLDT(epochTime).plusHours(8));
    }
}

Here's the Utils#setAlarm function that sends the intent:

public class Utils {

    public static void setAlarm(Context context, LocalDateTime alarmTimeDT) {

        Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
        intent.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
        intent.putExtra(AlarmClock.EXTRA_HOUR, alarmTimeDT.getHour());
        intent.putExtra(AlarmClock.EXTRA_MINUTES, alarmTimeDT.getMinute());
        intent.putExtra(AlarmClock.EXTRA_MESSAGE, "Good Morning");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    ...
}

This is the additional code that was used in the past for the same device and same Clock app. This is for cancelling an already set alarm, but the code used to set the old alarm was similar except for the cancelling part:

        //cancel old alarm
        AlarmManager alarmMgr = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
        PendingIntent alarmIntent = PendingIntent.getActivity(
                this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
        if (alarmIntent != null) {
            alarmMgr.cancel(alarmIntent);
        } else {
            Log.i(TAG, "intent is null");
        }

What am I doing wrong? Any help appreciated. The below comments is just questioning and asking for more clarification on the question, which were done as asked for. So you may skip reading them


Solution

  • No apps can perform this action.

    This happens when you don't have the required permission to set the alarm.

    From AlarmClock reference:

    Applications that wish to receive the ACTION_SET_ALARM and ACTION_SET_TIMER Intents should create an activity to handle the Intent that requires the permission com.android.alarm.permission.SET_ALARM.

    Request the SET_ALARM permission:

    <mainfest
        ...
        <uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
    
    </manifest>