Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android Alarmmanager中从缓存读取文件_Android_Alarmmanager_Bufferedreader_Android Context - Fatal编程技术网

在Android Alarmmanager中从缓存读取文件

在Android Alarmmanager中从缓存读取文件,android,alarmmanager,bufferedreader,android-context,Android,Alarmmanager,Bufferedreader,Android Context,我想将文件保存到缓存中。为此,我使用了一个缓冲写入程序。 我使用以下方法获取路径: CacheDir.getPath()+“/”+“Example.txt” 其中CacheDir是CacheDir=getCacheDir() 现在我想在Alarmmanager/Broadcastreceiver中使用Reader方法读取此文件 我使用:localLoadUpString=readTextFile(CacheDir.getPath()+“/”+“Example.txt”) 在AlarmReceiv

我想将文件保存到缓存中。为此,我使用了一个缓冲写入程序。 我使用以下方法获取路径:

CacheDir.getPath()+“/”+“Example.txt”

其中CacheDir是
CacheDir=getCacheDir()

现在我想在Alarmmanager/Broadcastreceiver中使用Reader方法读取此文件

我使用:
localLoadUpString=readTextFile(CacheDir.getPath()+“/”+“Example.txt”)
在AlarmReceiver中,我通过以下方式获取文件CacheDir:
CacheDir=context.getCacheDir()

当我使用Log查看路径是否正确时,一切看起来都很好,但我的localLoadUpString从未从我的reader方法返回字符串(reader方法在其他(正常)活动中工作正常)

我使用了错误的上下文吗?

正确的上下文可能取决于您注册广播接收器的方式

  • 如果在AndroidManifest.xml中声明了它,则传递到onReceive()的上下文就是ApplicationContext
  • 如果在运行时通过context.registerReceiver()动态注册BroadcastReceiver,则传递到onReceive()的上下文与调用registerReceiver()的上下文相同
  • 在第2种情况下,始终可以调用context.getApplicationContext()来检索应用程序上下文


    由于我无法从何处感受到您在尝试做什么,我最初的建议是在您的onReceive()调用中尝试context.getApplicationContext().getCacheDir()。

    谢谢,这帮了我的忙。一个多小时后,我发现我的主要问题是缺少构造函数调用,所以我传输了一个空字符串,无法读取任何数据。但我用你的解决方案解决了另一个问题。非常感谢。