开发者

How I read another Android application's file?

I have two applications.They have to communicate through a file. I know I can write a file on Android using Context.openFileOutput and pass it MODE_WORLD_READABLE|MODE_WORLD_WRITEABLE. But how can I find the file in another application?


Sorry , I have found the solution. In another application, you just need get appropriate Context, then开发者_如何转开发 you can do the same things.The API is Context.createPackageContext.


The solution is

in owner process

final SharedPreferences pref = getSharedPreferences("preferences", Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE | Context.MODE_MULTI_PROCESS);
final SharedPreferences.Editor edit = pref.edit();
edit.putInt(....)
edit.commit();

in other process

final Context remoteContext = createPackageContext(OWNER_PACKAGE_NAME,  Context.CONTEXT_IGNORE_SECURITY);
final SharedPreferences pref = remoteContext.getSharedPreferences("preferences", Context.MODE_WORLD_READABLE |  Context.MODE_WORLD_WRITEABLE | Context.MODE_MULTI_PROCESS);
pref.getInt(...)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜