我正在实现一个执行后台服务的简单应用程序,我无法启动该服务,以下是我的Manifest&代码,服务类已正确解析(读取资源),继承自IntentService(并实现req方法)并不能解决问题...
为什么后台服务无法启动?
任何帮助将不胜感激。
清单:
<?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>
服务代码:
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();
}
}
活动代码: 包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();
}
}
答案 0 :(得分:0)
问题很简单,服务实际上已经启动但是由于 android:process =“:captureService”被指定它是在一个单独的进程上启动的,一个未被调试器连接的进程,特定地连接到服务进程,或者,省略 android:process =“:captureService”解决问题。