Java StartService()不支持';t激活onStartCommand()函数(清单正确)

Java StartService()不支持';t激活onStartCommand()函数(清单正确),java,android,xml,service,manifest,Java,Android,Xml,Service,Manifest,我正在尝试使用StartService()激活我的服务,它应该位于onStartCommand()函数的旁边,但它没有。 我尝试了很多方法不使用onStartCommand()函数,但我需要服务中的意图信息。 代码如下: MainActivity-OnCreate: dbHelper = new DBHelper(this); listTasks = (ListView)findViewById(R.id.list_todo); loadTaskList();

我正在尝试使用StartService()激活我的服务,它应该位于onStartCommand()函数的旁边,但它没有。 我尝试了很多方法不使用onStartCommand()函数,但我需要服务中的意图信息。 代码如下:

MainActivity-OnCreate:

    dbHelper = new DBHelper(this);
    listTasks = (ListView)findViewById(R.id.list_todo);

    loadTaskList();

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
            eventIntent = new Intent(view.getContext(), NewEventActivity.class);
            startActivityForResult(eventIntent, ADD_EVENT_REQUEST);
        }
    });

    Intent serviceIntent = new Intent(this, MyService.class);
    serviceIntent.putExtra("dbHelper", (Parcelable)dbHelper);
    if (!isMyServiceRunning(MyService.class))
    {
        startService(serviceIntent);
    }

}

//gets any service class and check if its alive (running)
private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}
舱单: (在这里,我还尝试仅使用name=“.MyService”添加服务,但它没有改变任何内容)


startServiceThread()中出现崩溃,因为它无法从意图中获取信息

那么,在
onStartCommand()
之前,您可能不应该启动线程

无论如何,onStartCommand()不会在startService()之后调用


这是因为您正在
onCreate()
上崩溃。停止在
onCreate()
中崩溃,您应该会更幸运地使用
onStartCommand()

“但它不会”——您是如何确定这一点的?您是否尝试过摆脱不必要的
isMyServiceRunning()
代码?考虑到您是在
super.onCreate()
之前执行该代码,并且在您的情况下
startService
返回了什么?isMyServiceRunning()代码工作正常(可能不必要),但@commonware调用了函数startService()。startServiceThread()中出现崩溃,因为它无法从意图中获取信息。无论如何,onStartCommand()不会在startService()之后调用。在调试器中,可以执行startService(),但不能执行onStartCommand()。非常感谢!
DBHelper dbHelper;

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

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



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Bundle extras = intent.getExtras();
    if (extras != null) {

        dbHelper = (DBHelper)extras.get("dbHelper");
    }
 return super.onStartCommand(intent, flags, startId);
}
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    android:name=".MyApplication">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">

    </activity>
    <activity android:name=".NewEventActivity" />
    <activity android:name=".LockApp">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    </activity>
    <service android:name="com.example.user.project.MyService" />
</application>