我正在尝试检测我的报警模块。
在仪表测试中,我尝试使用系统警报服务并设置警报。
AlarmManager alarmMgr = (AlarmManager)
getInstrumentation().getContext().getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent("ACTION_TIMEOUT_REACHED", null,
getInstrumentation().getContext(),ALARM_RECEIVER.class);
intent.putExtra("PARAM_ID", "123");
PendingIntent alarmIntent=PendingIntent.getBroadcast(getInstrumentation().getContext(), 0, intent, 0);
alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + millisecondUntilExpire, alarmIntent);
运行测试后,我遇到以下错误
java.lang.SecurityException: Permission Denial: getIntentSender() from pid=27923, uid=10332, (need uid=10333) is not allowed to send as package org.abc.full.test
at android.os.Parcel.readException(Parcel.java:1546)
at android.os.Parcel.readException(Parcel.java:1499)
at android.app.ActivityManagerProxy.getIntentSender(ActivityManagerNative.java:4346)
at android.app.PendingIntent.getBroadcastAsUser(PendingIntent.java:511)
at android.app.PendingIntent.getBroadcast(PendingIntent.java:495)
at org.abc.TimeoutAlarmTest.java:68)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57 )
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1889)
我的问题是如何解决这个问题?这是因为我的包域与Manifest包声明不同?我的清单文件声明如下
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.abc.def">
而不是
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="org.abc.full.test">
答案 0 :(得分:1)
在仪表化测试方法中,设置报警服务时使用
getInstrument().getTargetContext();
而不是
getInstrument().getContext();
解决了我的问题。