如何从图书馆一个Android应用程序启动活动 [英] How to start an activity in an android Application from the Library

查看:200
本文介绍了如何从图书馆一个Android应用程序启动活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在工作室Android的Andr​​oid应用程序。而我添加了一个库到应用程序中。按钮,视图和活动在库中定义。当我按一下按钮,我需要导航到应用程序中定义的活动。

I have an Android application in Android Studio. And I've added a library into the application. The button, view, and activities are defined in the library. When I click on the button, I need to navigate to the activity defined in the application.

一般情况下,导航到另一个页面,我们使用的意图,像这样的:

Usually, to navigate to another page, we used the intent, like this:

Intent intent = new Intent(MainActivity.this, Activity.class);
startActivity(intent);

但是,这不是一个足够方法调用从库中应用的活性

But this is not a sufficient method to call the activity of the application from the library.

的问题是,该库和应用程序是独立的;他们有不同的套餐。所以在应用程序中的活动不能由库识别。

The problem is that the library and the application are independent; they have different packages. So the activity in the application cannot be recognized by the library.

我如何处理库和应用程序之间的通讯?

How do I handle communication between the library and the application?

推荐答案

这样做的正常方式是这样:

The normal way for doing this is to do this:

Intent intent = new Intent();
intent.setComponent(new ComponentName("com.my.package","com.my.package.activity.ActivityName");
startActivity(intent);

这是您的库中明确提及的活动。开始本活动能赶上那班当活动不存在于系统中会发生这种情况的ActivityNotFoundException时,应该确保。

This is an explicit reference to an activity within your library. You should ensure that when starting this Activity that you catch the ActivityNotFoundException as this can happen when the Activity does not exist in the system.

在理想情况下创建这个意图的时候,你应该确保您可以通过使用软件包管理系统的API解决这个问题。

Ideally when building this Intent you should insure that you can resolve it by using PackageManager APIs.

然而,你应该尽量避免硬编码包,但是当涉及到​​库中,有时你没有选择。

However you should try to avoid hardcoding packages, but when it comes to a library, sometimes you don't have a choice.

另外有一点要注意的是,库中,你需要确保活动导出,这样您可以在应用程序之外访问它。

Also one thing to note is that within the library you need to ensure that the Activity is exported so that you can access it outside of your application.

是否活性可以通过发动
  其他应用程序的组件 - 真如果可以,和假,如果
  不。如果假,活动只能通过部件推出
  相同的应用程序或应用程序具有相同的用户ID。该
  默认值取决于该活动是否包含意图过滤器。
  没有任何过滤器意味着活动只能调用
  通过指定确切的类名。这意味着,该活动是
  仅用于应用程序内部使用(因为别人不会
  知道类名)。因此,在这种情况下,默认值是假。
  另一方面,至少一个过滤器的presence意味着
  活性是用于外部使用的,所以默认值是
  真。

Whether or not the activity can be launched by components of other applications — "true" if it can be, and "false" if not. If "false", the activity can be launched only by components of the same application or applications with the same user ID. The default value depends on whether the activity contains intent filters. The absence of any filters means that the activity can be invoked only by specifying its exact class name. This implies that the activity is intended only for application-internal use (since others would not know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the activity is intended for external use, so the default value is "true".

这属性不是限制活动接触到的唯一途径
  其他应用程序。还可以使用权限来限制
  可以调用activity外部实体(见许可
  属性)。

This attribute is not the only way to limit an activity's exposure to other applications. You can also use a permission to limit the external entities that can invoke the activity (see the permission attribute).

参考

http://developer.android.com/guide/topics/清单/活动的element.html

这篇关于如何从图书馆一个Android应用程序启动活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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