通过 adb 启动意图服务失败



当我尝试通过adb启动IntentService时,我收到以下错误

E/AndroidRuntime( 2418): java.lang.RuntimeException: Unable to instantiate service 
com.myCompany.MyPackage.Service: java.lang.InstantiationException: 
com.myCompany.MyPackage.Service

当我启动正常的服务时,一切都很好。

无法通过 adb 启动意向服务的原因是什么?

public class NCService extends IntentService {
public NCService(String name) {
    super(name);
    // TODO Auto-generated constructor stub
}
NetworkStateReceiver nsr;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(), "onStartCommand", 0).show();
    nsr = new NetworkStateReceiver();
    return START_STICKY;
}
@Override
public void onCreate() {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(), "onCreate", 0).show();
    super.onCreate();
}
@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    Toast.makeText(getBaseContext(), "onDestroy", 0).show();
    super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}
@Override
protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub
}

}

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <service android:name=".NCService" />
    <receiver android:name=".NetworkStateReceiver" >
        <intent-filter>
            <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
        </intent-filter>
    </receiver>
</application>

问候西蒙

你的服务需要一个默认的 no-arg 构造函数,否则 Android 不知道什么参数作为名称参数传递:

public NCService() {
    super("MyNCService");
}

最新更新