user3912282
user3912282

Reputation: 43

Making an Android service available to other apps

I have looked all over StackOverflow and other forums, and nothing I've seen so far has helped me. My company has an app with an activity and service, and one of our clients wants to be able to use the service part using their own app. So I'm trying to write a separate app to test how this would work, and I cannot get it to work. Here is the relevant part of the manifest.

<service android:name="com.gpssolutions.rtrover.RTRoverService"
         android:process=":RTRoverServiceProcess"
         android:exported="true"
         android:enabled="true">
</service>

Here is the code in the activity.

intent = new Intent();

intent.setComponent(new ComponentName(
  "com.gpssolutions.rtrover", "com.gpssolutions.rtrover.RTRoverService"
));

//intent.setClassName("com.gpssolutions.rtrover", "RTRoverService");
intent.putExtra("inpStr", input);
startService(intent);

I have tried only specifying the name of the service and the full path. I tried setComponent and setClassName. I have tried different combinations of XML settings including eliminating the process part. Nothing works.

If I run "adb shell service list", the service is not listed. When I try to start the service from the activity, I get the following:

W/ActivityManager: Unable to start service Intent { cmp=com.gpssolutions.rtrover/.RTRoverService (has extras) } U=0: not found"

Android version is 4.4.4.

Thank you.

Edit:

Here is my whole manifest for the app that contains the service the other app wants access to.

<?xml version='1.0' encoding='utf-8'?>
<manifest package="com.gpssolutions.RTRover"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0"
    android:versionCode="1"
    android:installLocation="auto"
>
    <application android:hardwareAccelerated="true"
        android:name="org.qtproject.qt5.android.bindings.QtApplication"
        android:icon="@drawable/app_rtrover"
        android:label="RTRover"
    >
        <activity android:configChanges="orientation|uiMode|screenLayout|screenSize|smallestScreenSize|layoutDirection|locale|fontScale|keyboard|keyboardHidden|navigation"
            android:name="com.gpssolutions.rtrover.RTRoverActivity"
            android:label="RTRover"
            android:icon="@drawable/app_rtrover"
            android:screenOrientation="portrait"
            android:launchMode="singleTop"
        >
           <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <meta-data android:name="android.app.lib_name" android:value="-- %%INSERT_APP_LIB_NAME%% --"/>
            <meta-data android:name="android.app.qt_sources_resource_id" android:resource="@array/qt_sources"/>
            <meta-data android:name="android.app.repository" android:value="default"/>
            <meta-data android:name="android.app.qt_libs_resource_id" android:resource="@array/qt_libs"/>
            <meta-data android:name="android.app.bundled_libs_resource_id" android:resource="@array/bundled_libs"/>
            <!-- Deploy Qt libs as part of package -->
            <meta-data android:name="android.app.bundle_local_qt_libs" android:value="-- %%BUNDLE_LOCAL_QT_LIBS%% --"/>
            <meta-data android:name="android.app.bundled_in_lib_resource_id" android:resource="@array/bundled_in_lib"/>
            <meta-data android:name="android.app.bundled_in_assets_resource_id" android:resource="@array/bundled_in_assets"/>
            <!-- Run with local libs -->
            <meta-data android:name="android.app.use_local_qt_libs" android:value="-- %%USE_LOCAL_QT_LIBS%% --"/>
            <meta-data android:name="android.app.libs_prefix" android:value="/data/local/tmp/qt/"/>
            <meta-data android:name="android.app.load_local_libs" android:value="-- %%INSERT_LOCAL_LIBS%% --"/>
            <meta-data android:name="android.app.load_local_jars" android:value="-- %%INSERT_LOCAL_JARS%% --"/>
            <meta-data android:name="android.app.static_init_classes" android:value="-- %%INSERT_INIT_CLASSES%% --"/>
            <!--  Messages maps -->
            <meta-data android:value="@string/ministro_not_found_msg" android:name="android.app.ministro_not_found_msg"/>
            <meta-data android:value="@string/ministro_needed_msg" android:name="android.app.ministro_needed_msg"/>
            <meta-data android:value="@string/fatal_error_msg" android:name="android.app.fatal_error_msg"/>
            <meta-data android:name="android.app.background_running" android:value="false"/>
        </activity>
        <service android:name="com.gpssolutions.rtrover.RTRoverService"
             android:process=":RTRoverServiceProcess"
             android:exported="true"
             android:enabled="true">
        </service>
    </application>
    <uses-sdk android:minSdkVersion="9" android:targetSdkVersion="14"/>
    <supports-screens android:largeScreens="true"
        android:normalScreens="true"
        android:anyDensity="true"
        android:smallScreens="true"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
</manifest>

Upvotes: 1

Views: 1653

Answers (1)

David Wasser
David Wasser

Reputation: 95578

In your manifest, you specify

package="com.gpssolutions.RTRover"

however, when your other app tries to start your Service, it creates the Intent with this package name: "com.gpssolutions.rtrover", in this code:

intent.setComponent(new ComponentName("com.gpssolutions.rtrover", 
                  "com.gpssolutions.rtrover.RTRoverService"));

NOTE: Package names are case-sensitive.

Try starting the service like this:

intent = new Intent();
intent.setComponent(new ComponentName("com.gpssolutions.RTRover",
              "com.gpssolutions.rtrover.RTRoverService"));
intent.putExtra("inpStr", input);
startService(intent);

Upvotes: 2

Related Questions