编写一个Android库 - 利用其自己的自定义应用程序类(getApplication) [英] Writing an Android library - Using its own custom Application class (getApplication)

查看:323
本文介绍了编写一个Android库 - 利用其自己的自定义应用程序类(getApplication)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个Android库,我想使用自定义的应用类,这将是可访问所有图书馆的活动(使用 getApplication 法)。

I'm writing an Android library and I wish to use a custom Application class, which will be accessible from all of the library's activities (using the getApplication method).

问题是,当我称之为 getApplication 方法,从我的图书馆的活动之一,它用我的图书馆返回应用程序类的应用程序(而不是自定义类我在图书馆的Andr​​oid清单中声明)。

The problem is when I call the getApplication method from one of my library's activities, it returns the application class of the app using my library (and not the custom class I declared in the library's Android manifest).

我现在的猜测(根据这个答案和<一个href=\"http://developer.android.com/guide/developing/projects/projects-eclipse.html#ReferencingLibraryProject\"相对=nofollow>官方文档)是用我的图书馆还必须声明自定义应用类在他们的Andr​​oid清单应用程序 - 这是一个问题的解决方案由于该应用程序也可以有自己的自定义类(和一个单一的应用程序不能有多个自定义应用类,对吧?)。

My current guess (based on this answer and the official documentation) is that the application using my library must also declare the custom Application class in their Android manifest - which is a problematic solution, since that application can also have its own custom class (and a single app cannot have more than one custom Application class, right?).

谁能提供更好的解决方案? (或确认这个解决方案是正确的)

Can anyone offer a better solution? (or verify that this solution is accurate)

任何帮助将AP preciated ..

Any help will be appreciated..

推荐答案

没有错,你的自定义应用程序的实施和AndroidManifest.xml中。在code。使用您的自定义应用程序的正确方法是的铸造

Nothing wrong with your custom Application implementation and AndroidManifest.xml. The correct way of using your custom Application in code is casting.

假设你有一个扩展android.app.Application,让所有MyApplication,而不是应用程序MyApplication的:

Suppose you have a MyApplication that extends android.app.Application, to get MyApplication instead of Application:

Boolean runInTest = ((MyApplication) getApplication()).isRunInTet();

希望有所帮助。

这篇关于编写一个Android库 - 利用其自己的自定义应用程序类(getApplication)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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