StartService()没有激活onStartCommand()函数(Manifest是正确的)

时间:2017-05-13 14:30:52

标签: java android xml service manifest

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

MainAcitivty - 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;
}

我的服务:

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);
}

清单: (这里我也尝试使用name =&#34; .MyService&#34;添加服务,但它没有改变任何东西)

<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>

1 个答案:

答案 0 :(得分:0)

  

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

那么也许你不应该在onStartCommand()之前启动线程。

  

无论如何,onStartCommand()不会在startService()

之后调用

那是因为你在onCreate()上崩溃了。停止在onCreate()中崩溃,你应该更好地使用onStartCommand()