【问题标题】:StartService() doesn't active onStartCommand() function (Manifest is correct)StartService() 没有激活 onStartCommand() 函数(清单是正确的)
【发布时间】:2017-05-14 03:24:16
【问题描述】:

我正在尝试使用 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=".MyService" 添加服务,但它没有改变任何东西)

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

【问题讨论】:

  • “但它没有”——你是怎么确定的?您是否尝试过摆脱不必要的isMyServiceRunning() 代码?考虑到您在super.onCreate() 之前执行该代码,您是否可能在startServiceThread() 中崩溃?
  • 你的情况是什么startService返回?
  • isMyServiceRunning() 代码工作正常(可能没有必要),但@CommonsWare 调用了函数 startService()。它在 startServiceThread() 中崩溃,因为它无法从意图中获取信息。无论如何,onStartCommand() 不会在 startService() 之后调用。在调试器中,startSerivce() 会被执行,但 onStartCommand() 不会。

标签: java android xml service manifest


【解决方案1】:

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

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

反正 onStartCommand() 不会在 startService() 之后调用

那是因为你在onCreate() 上崩溃了。停止在onCreate() 中崩溃,您应该在onStartCommand() 中获得更好的运气。

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2020-04-25
  • 2018-12-05
  • 1970-01-01
  • 1970-01-01
  • 2019-11-05
  • 1970-01-01
  • 2013-02-20
  • 1970-01-01
  • 2016-07-16
相关资源
最近更新 更多