0

我正在嘗試測試我的報警模塊。與PendingIntent.getBroadcast()相關的測試測試

在儀表測試中,我嘗試使用系統警報服務並設置警報。

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"> 

回答

1

在儀器化檢驗方法,設置報警服務使用時

getInstrument().getTargetContext(); 

,而不是

getInstrument().getContext(); 

solv我的問題。