简体   繁体   中英

How do I get a module installed as a service - Android

I'm attempting to add the rtsp server portion of libstreaming to my application and run it as a service, but even though my application builds and runs fine, the service part isn't being installed and I would appreciate any help figuring out why. I think I'm missing a step.

I added libstreaming to my existing application as a module, and libstreaming is listed as a dependency. It all compiles and starts fine, but context.startService() returns null for a ComponentName. According to the docs , this means the service "does not exist", so I think I'm missing something, but I don't know what. I'm building in Android Studio, then using shift+F9 to install my app and start debugging, this must install the application but not the service part? Any help would be greatly appreciated!

Here's how I'm configured:

在此处输入图片说明

Dependency:

在此处输入图片说明

My code to start the service:

ComponentName cName = null;
try
{
    // Start the RTSP server.  Returns name of the component that is started, or is already running
    cName = this.startService(new Intent(this, RtspServer.class));
}
catch(Exception e)
{
    postUIMessage("onCreate() startService() exception: " + e.toString());
}
if(cName != null)
{
    postUIMessage("Service started:  " + cName.toString());
}
else
{
    postUIMessage("Service NOT started:  " + cName.toString());
}

My AndroidManifest.xml file:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.matt.mattvideoreceiver" >
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS"></uses-permission>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />

    <service android:name="net.majorkernelpanic.streaming.rtsp.RtspServer"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I found the problem, here's the solution in case it helps someone else. In my AndroidManifest.xml file, the line:

<service android:name="net.majorkernelpanic.streaming.rtsp.RtspServer"/>

MUST go inside the

<Application>

section. Once I moved that line inside the application section context.startService() now returns the service name, indicating that the service is running.

The service isn't sending video as I expected, BUT that's a different problem and at least it's now running and that answers this question. :)

Here's the working AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.matt.mattvideoreceiver" >
    <uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
    <uses-permission android:name="android.permission.CAMERA"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.INTERACT_ACROSS_USERS"></uses-permission>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />



    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <service android:name="net.majorkernelpanic.streaming.rtsp.RtspServer"/>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM