Android AlarmClock causing force close

2019-09-12 04:38发布

问题:

I'm writing an app that sets an alarm, and here's the relevant code that's causing a force close:

Intent i = new Intent(AlarmClock.ACTION_SET_ALARM);
i.putExtra(AlarmClock.EXTRA_HOUR, hours);
i.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
i.putExtra(AlarmClock.EXTRA_SKIP_UI, true);
startActivity(i);

The startActivity(i) is causing the force close. I tried catching ActivityNotFoundException and displaying a Toast, but the force close is still happening.

I'm really new to Android programming, and I'm starting to think there are other forces at work here... does the manifest file need to be edited to account for Activities not native to this application (such as the Alarm Clock) that this application tries to start?

EDIT: I should have mentioned, I'm using Android 2.3.5 (API level 10).

回答1:

If your API is 9+,

Then use this permission in your manifest file,

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

EDIT:

Look at this page Android - AlarmClock



回答2:

This API works only for version 9+. What Android version you are using?

You can also call queryIntentActivities() (via the PackageManager) in order to check if anything will respond to your call before you actually call the startActivity() method.

Hope this helps!