简体   繁体   中英

Android, Background service is not starting

I am implementing a simple application executing a background service, I am not able to start the service, following is my Manifest & Code, the service class is properly resolved ( reading resources ), inheriting from IntentService ( and implementing the req methods ) doesn't resolve the problem as-well...

Why does the Background service doesn't start?
any help will be appreciated.

Manifest:

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

<uses-sdk
    android:minSdkVersion="16"
    android:targetSdkVersion="20" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver android:name="com.newcomp.Infrastructure.BootStarter" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <service
        android:name=".CaptureService"
        android:exported="true"
        android:process=":captureService" >
    </service>
</application>

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.USE_CREDENTIALS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

</manifest>

Service code:

package com.newcomp.vagent;

import android.app.IntentService;
import android.app.Service;
import android.content.Intent;
import android.R.*;
import android.os.IBinder;

import java.io.IOException;

import fi.iki.elonen.NanoHTTPD;

    public class CaptureService extends Service {
    public CaptureService() {
        //super("Capture Service");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return Service.START_NOT_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
}

Activity code: package com.newcomp.vagent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        final String strSvcName = getResources().getString(R.string.startup_service);
        //setContentView(R.layout.main);
        Toast.makeText(getBaseContext(), String.format("Hello from '%s'", strSvcName), Toast.LENGTH_LONG).show();
        Class cls = null;
        try {
            cls = getClassLoader().loadClass(strSvcName);
            startService(new Intent(this, cls));// Starts the service
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //finish();
    }
}

问题很简单,服务实际上正在启动,但是由于指定了android:process =“:captureService” ,所以它是在一个单独的进程上启动的,该进程未由调试器连接,或者特定地连接到了服务进程,或者省略了android:process =“:captureService”解决了问题。

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