Reputation: 4773
I'm very new to Android Studio as well as android programming. I've just read about the first common Intent tutorial, it's about the AlarmClock
. I tried writing a simple code to test that Intent but it's not working with a SecurityException
thrown, the stack trace looks like this:
05-20 15:36:25.010 2342-2342/com.viphalong.helloandroid E/AndroidRuntime: FATAL EXCEPTION: main Process: com.viphalong.helloandroid, PID: 2342 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.viphalong.helloandroid/com.viphalong.helloandroid.MainActivity}: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.SET_ALARM cmp=com.android.deskclock/.HandleApiCalls (has extras) } from ProcessRecord{ad271560 2342:com.viphalong.helloandroid/u0a52} (pid=2342, uid=10052) requires com.android.alarm.permission.SET_ALARM at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.intent.action.SET_ALARM cmp=com.android.deskclock/.HandleApiCalls (has extras) } from ProcessRecord{ad271560 2342:com.viphalong.helloandroid/u0a52} (pid=2342, uid=10052) requires com.android.alarm.permission.SET_ALARM at android.os.Parcel.readException(Parcel.java:1465) at android.os.Parcel.readException(Parcel.java:1419) at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:2096) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1419) at android.app.Activity.startActivityForResult(Activity.java:3424) at android.app.Activity.startActivityForResult(Activity.java:3385) at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842) at android.app.Activity.startActivity(Activity.java:3627) at android.app.Activity.startActivity(Activity.java:3595) at com.viphalong.helloandroid.MainActivity.createAlarm(MainActivity.java:22) at com.viphalong.helloandroid.MainActivity.onCreate(MainActivity.java:14) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)
Here is the manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.viphalong.helloandroid">
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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>
And here is the main code:
package com.viphalong.helloandroid;
import android.content.Intent;
import android.provider.AlarmClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//try creating a simple alarming
createAlarm("Welldone", 0, 1);
}
void createAlarm(String msg, int hours, int minutes){
Intent it = new Intent(AlarmClock.ACTION_SET_ALARM);
it.putExtra(AlarmClock.EXTRA_MESSAGE, msg)
.putExtra(AlarmClock.EXTRA_HOUR, hours)
.putExtra(AlarmClock.EXTRA_MINUTES, minutes);
if(it.resolveActivity(getPackageManager()) != null){
startActivity(it);
}
}
}
As you can see I put the line
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
in the manifest file. But the exception is still thrown making me really confusing.
Upvotes: 2
Views: 3101
Reputation: 8003
Try Adding both of the permissions..
<uses-permission android:name="android.permission.SET_ALARM"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
I have faced a similar issue...Resolved by adding both of the permission..
Upvotes: 10
Reputation: 49
if your are using android M then need to check runtime permission, because in android M can remove permission.
Upvotes: 0
Reputation: 56
What is the Android version are you trying to implement it on. I faced similar issue before when I was testing on Android 6.0. I had to manually go to the app permission and switch on the permission for the same.
Alternately you can check the below link. https://developer.android.com/training/permissions/requesting.html
This will help you to check if the user has granted permission for the app.
Hope this helps you. Have a Nice day!!
Upvotes: 1