问题从另一个包在Android中调用活动 [英] Problem calling activity from another package in Android

查看:130
本文介绍了问题从另一个包在Android中调用活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况: 我需要几个应用程序组合成一个apk文件的应用程序。比方说实现APP2到APP1

我做了什么: 复制APP2包到主APP1项目,我工作,所以我APP1有两个包。

APP2过这样的清单文件:

 <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.package.app2安卓版code =3安卓VERSIONNAME =1.2>

<应用机器人:标签=App2的机器人:图标=@可绘制/图标>
    <活动机器人:名称=活动1机器人:launchMode =singleTask>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>
            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;

<活动机器人:名称=活性2机器人:excludeFromRecents =真正的>< /活性GT;
< /用途>

<使用-权限的Andr​​oid:名称=android.permission.WAKE_LOCK>< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.VIBRATE>< /使用-许可>
< /舱单>
 

我原来App1的清单文件:

 <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.package.app1>
<应用机器人:图标=@可绘制/图标机器人:可调试=真正的>
    <活动机器人:名称=开始。机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>
            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <活动机器人:名称=com.package.app1.PlayerList/>
    <活动机器人:名称=com.package.app1.CreateNewPlayer/>
    <活动机器人:名称=com.package.app1.Profile/>
    <活动机器人:名称=com.package.app1.Braintrainer/>
< /用途>
< /舱单>
 

在code在APP1我用来调用APP2包活动:

 意向书我=新意图();
i.setClassName(com.package.app1,com.package.app2.Activity1);
startActivity(ⅰ);
 

问题: 如何修改我的APP1清单文件有APP2活动。

东西i`ve尝试: 它的工作原理,如果我创建APP2简单的HelloWorld测试类,调用使用相同的code和只包含这在APP1清单:

 <活动机器人:名称=com.package.app2.Test/>
 

但我无法弄清楚如何实施APP2清单文件到第一个。调用该活动时,每一个方式,我试着给任何错误,但崩溃。 App2的单独工作正常,所以问题并不在活动文件。

鸭preciate有这方面的想法。

解决方案

 意向书我=新意图();
i.setClassName(com.package.app1,com.package.app2.Activity1);
startActivity(ⅰ);
 

在黑暗中拍摄:

变更 com.package.app1 com.package.app2 。我所谓的做的,你正在试图什么,现在,我一直有规定,我想调用类的包。

好吧,PackageManager是不是你的解决方案,我看错了,以为你有两个应用程序,想要一个应用程序来调用其他。看起来你只是想要一个应用程序。

修改您的APP1的表现是这样的:

 <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=com.package.app1>
<应用机器人:图标=@可绘制/图标机器人:可调试=真正的>
    <活动机器人:名称=开始。机器人:标签=@字符串/ APP_NAME>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>
            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
    <活动机器人:名称=com.package.app2.Activity1/>
    <活动机器人:名称=com.package.app2.Activity2机器人:excludeFromRecents =真正的>< /活性GT;
    <活动机器人:名称=com.package.app1.PlayerList/>
    <活动机器人:名称=com.package.app1.CreateNewPlayer/>
    <活动机器人:名称=com.package.app1.Profile/>
    <活动机器人:名称=com.package.app1.Braintrainer/>
< /用途>
<使用-权限的Andr​​oid:名称=android.permission.WAKE_LOCK>< /使用-许可>
<使用-权限的Andr​​oid:名称=android.permission.VIBRATE>< /使用-许可>
< /舱单>
 

并再次尝试第一种方式。

Situation: I need to combine several apps into one .apk app. Lets say implement app2 into app1

What i have done: Copied app2 package into main app1 project which i am working, so my app1 has two packages.

app2 had this manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package.app2" android:versionCode="3" android:versionName="1.2">

<application android:label="App2" android:icon="@drawable/icon">
    <activity android:name="Activity1" android:launchMode="singleTask">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>        

<activity android:name="Activity2" android:excludeFromRecents="true"></activity>
</application>

<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
</manifest>

My original App1 manifest file:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.package.app1">
<application android:icon="@drawable/icon" android:debuggable="true">
    <activity android:name=".Start" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.package.app1.PlayerList" />
    <activity android:name="com.package.app1.CreateNewPlayer" />
    <activity android:name="com.package.app1.Profile" />                       
    <activity android:name="com.package.app1.Braintrainer" />
</application>
</manifest> 

The code in app1 i am using to call activity in app2 package:

Intent i = new Intent();
i.setClassName("com.package.app1", "com.package.app2.Activity1");                    
startActivity(i);

The question: How do i modify my app1 manifest file to have activities of app2.

Things i`ve tried: It works if i create simple HelloWorld test class in app2, call using the same code and just include this in the app1 manifest:

        <activity android:name="com.package.app2.Test" />

But i can not figure out how to implement the app2 manifest file into the first one. Every way i try give no errors but crashes when calling that activity. App2 alone works fine, so problem not in the activity file.

Appreciate any thoughts on this.

解决方案

Intent i = new Intent();
i.setClassName("com.package.app1", "com.package.app2.Activity1");                    
startActivity(i);

Shot in the dark:

change com.package.app1 to com.package.app2. I've called done what you're attempting right now, and I've always had to specify the package of the class I wanted to call.

Ok, PackageManager is not your solution, I misread and thought you had two apps and wanted one app to call the other. It looks like you just want one app.

Modify your app1's manifest like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.package.app1">
<application android:icon="@drawable/icon" android:debuggable="true">
    <activity android:name=".Start" android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.package.app2.Activity1" />
    <activity android:name="com.package.app2.Activity2" android:excludeFromRecents="true"></activity>
    <activity android:name="com.package.app1.PlayerList" />
    <activity android:name="com.package.app1.CreateNewPlayer" />
    <activity android:name="com.package.app1.Profile" />                       
    <activity android:name="com.package.app1.Braintrainer" />
</application>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
</manifest> 

And try the first way again.

这篇关于问题从另一个包在Android中调用活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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