android - 同时安装两个具有相同代码库的 Android 目标

标签 android android-library intentservice

我正在尝试使用库项目构建两个派生自同一代码库的不同 Android 应用。

这些应用程序正在为各种操作使用 Intent 服务,但是当我同时安装了两个使用相同代码库的应用程序时,这似乎失败了。

似乎只有我安装的应用程序的第一个版本有效,第二个版本似乎无法运行该服务。

有没有人有这方面的经验?如何解决?

编辑: 这些是 list

基地:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.codebase"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <supports-screens android:largeScreens="true" android:anyDensity="true" android:resizeable="true" android:smallScreens="true" android:normalScreens="true"></supports-screens>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application android:icon="@drawable/icon_launcher" android:label="CodeBase" android:process="@string/app_name" android:name="com.codebase.activity.Application" android:theme="@android:style/Theme.NoTitleBar" android:debuggable="false">
        <activity android:configChanges="orientation" android:name="com.codenase.activity.MainActivity" android:launchMode="singleTask">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <service android:name="com.codebase.service.UpdateService" android:process="@string/app_name">
            <intent-filter>
                <action
                    android:name="com.codebase.service.UpdateService" />
            </intent-filter>
        </service>

</manifest>

目标 1:

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10" />

    <supports-screens android:largeScreens="true" android:anyDensity="true" android:resizeable="true" android:smallScreens="true" android:normalScreens="true"></supports-screens>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application android:icon="@drawable/icon_launcher" android:label="Target One" android:name="com.codebase.activity.Application" android:theme="@android:style/Theme.NoTitleBar">
        <activity android:configChanges="orientation" android:name="com.codebase.activity.MainActivity" android:launchMode="singleTask">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>


        <service android:name="com.codebase.service.UpdateService" android:process="@string/app_name">
            <intent-filter>
                <action
                    android:name="com.codebase.service.UpdateService" />

            </intent-filter>
        </service>

        </activity>

    </application>

</manifest>

目标 2:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.trg2"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10" />

    <supports-screens android:largeScreens="true" android:anyDensity="true" android:resizeable="true" android:smallScreens="true" android:normalScreens="true"></supports-screens>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

    <application android:icon="@drawable/icon_launcher" android:label="Target 2" android:name="com.codebase.activity.Application" android:theme="@android:style/Theme.NoTitleBar">
        <activity android:configChanges="orientation" android:name="com.codebase.activity.MainActivity" android:launchMode="singleTask">
        <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="com.codebase.service.UpdateService">
            <intent-filter>
                <action
                    android:name="com.codebase.service.UpdateService" />
            </intent-filter>
        </service>

    </application>


</manifest>

最佳答案

首先,在这种情况下,无需在 AndroidManifest.xml 中为服务定义 intent-filter,如果您不需要从应用外部提供额外的访问点,则以下定义​​就足够了:

<service android:name="com.codebase.service.UpdateService"/>

然后使用 public Intent (Context packageContext, Class cls)开始服务:

Intent intent = new Intent(getBaseContext(), UpdateService.class);
startService(intent);

其次,如果您确实需要从您的应用程序外部提供额外的访问点,请不要在多个应用程序中使用相同的操作名称,因为它会在尝试将 Intent 传递给相应的服务时欺骗 Android 操作系统(如果您使用 public Intent (String action) 启动您的服务):

目标 1:

<service android:name="com.codebase.service.UpdateService" android:process="@string/app_name">
  <intent-filter>
    <action android:name="com.codebase.service.UpdateService.TARGET_1" />
  </intent-filter>
</service>

目标 2:

<service android:name="com.codebase.service.UpdateService" android:process="@string/app_name">
  <intent-filter>
    <action android:name="com.codebase.service.UpdateService.TARGET_2" />
  </intent-filter>
</service>

希望这对您有所帮助。

关于android - 同时安装两个具有相同代码库的 Android 目标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10142853/

相关文章:

java - 如何最好地将 StringBuilder 转换为 String[]?

android - 当 Maven 构建我的项目时,无法从库项目的 jar 中找到类

android - IntentService 中的队列大小是否有限制?

java - Android 服务变红

android - IntentService 创建的通知总是使用错误的 Intent

java - Android 在焦点消失时隐藏键盘,但在选择密码字段时不会隐藏键盘

java - 在 Firebase (Android) 中同时更新多个节点中的多个字段

android - android BottomNavigationView中的项目选择颜色

java - 如何使用 OkHttpClient MockWebSever 返回图像/字节[]?

android - Android嵌套库结构,从主应用程序访问嵌套库的方法