Android的库项目 - 如何让背景? [英] Android library project - how to get context?

查看:90
本文介绍了Android的库项目 - 如何让背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直愉快地从不同版本的同一应用程序的重构code(付费/免费)到Android的库项目,使实际的应用程序可以简单地定制库,减少code重复。

I have been happily refactoring code from different versions of the same app (paid/free) into Android library projects so that the actual apps can simply customize the library and reduce code duplication.

有一件事,我开始怀疑是 getApplicationContext()库code手段里面呢?是否是相同的的ApplicationContext 的人会得到孩子的应用程序?当我访问会发生什么共享preferences 从库中项目的 getApplicationContext()代替原有的应用程序的 getApplicationContext()?请问共享preferences 文件相同或不同?

One thing I'm started to wonder is what getApplicationContext() inside the library code means? Is is the same ApplicationContext as one would get from the child apps? What happens when I access SharedPreferences from a library project's getApplicationContext() instead of the original app's getApplicationContext()? Will the SharedPreferences file be same or different?

如果我用的活性访问共享preferences什么?不要紧,该活动现在是一个图书馆的活动,而不是原来的应用程序?是共享preferences 一样吗?

What if I had used the activity to access SharedPreferences? Does it matter that the activity is now a library activity and not the original app? Is the SharedPreferences the same?

感谢澄清。

推荐答案

在该APK打包了,那么所有的类将属于主应用程序。

When the APK is packaged up then all classes will be belong to the main application.

调用getApplicationContext()。getPackageName(),它会返回应用程序的包名称,而不是库的包。

call getApplicationContext().getPackageName() and it will return the app's package name, and not the library's package.

我有一个免费/付费应用程序,并没有任何问题,当我提出我的班到一个库项目相同的设置。

I have the same setup for a free/paid application and no issues when I moved my classes into a library project.

然而,你必须检查你的XML文件(清单,窗口小部件等)使用你的库项目的完整包名。

However you have to check your xml files (manifest, widgets, etc.) to use the full package name of your library project.

这篇关于Android的库项目 - 如何让背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆