JobIntentService无法正常启动

时间:2018-03-16 10:21:35

标签: android service jobservice

在com.example.project.packageA包中我有一个扩展JobIntentService定义的类,如下所示:

public class MyService extends JobIntentService {

    static final int JOB_ID = 1000;
    static final String TAG =MyService.class.getSimpleName();

    public static void enqueueWork(Context context, Intent work) {
        enqueueWork(context, MyService.class, JOB_ID, work);
    }

    @Override
    public int onStartCommand(@Nullable Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);
    }

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

        @Override
    public void onDestroy() {
        super.onDestroy();
    }

    /**
     * We have received work to do.  The system or framework is already
     * holding a wake lock for us at this point, so we can just go.
     */
    @Override
    protected void onHandleWork(Intent intent) {
        Log.i(TAG, "Started onHandleWork");
        String value = intent.getExtras().getString("Key");
        Log.i(TAG, value);
    }

}

在另一个包中: com.example.project.packageB;我想把这个服务称为后台启动,所以我这样做了:

Intent it = new Intent();
it.setComponent(new ComponentName("com.example.project", "com.example.project.packageA.MyService"));
it.putExtra("Key", "Value");
MyService.enqueueWork(context, it);
Log.d(TAG, "Call successful");

我还在清单文件中包含以下权限:

<service
    android:name="com.example.project.MyService"
    android:permission="android.permission.BIND_JOB_SERVICE"
    android:exported="true"/>

然而,当我运行我的程序时,它看起来并不像服务启动。我可以看到日志消息&#34;呼叫成功&#34;但我无法看到来自onHandleWork功能的日志消息。我是以错误的方式开始服务吗?

4 个答案:

答案 0 :(得分:0)

您不需要在意图中使用组件名称。尝试删除此行:

it.setComponent(new ComponentName("com.example.project", "com.example.project.packageA.MyService"));

此外,您的代码应如下所示:

Intent it = new Intent();
it.putExtra("Key", "Value");
MyService.enqueueWork(context, it);
Log.d(TAG, "Call successful");

答案 1 :(得分:0)

考虑像这样开始;

Intent it = new Intent(context, MyService.class);
it.putExtra("key", "value");
MyService.enqueueWork(context, it);

答案 2 :(得分:0)

在更高版本的Android中,Google引入了Doze功能以优化设备的电池寿命。 (不确定他们从哪个版本开始引入),您需要将此添加到忽略电池优化的意图中

//禁用电池优化

Intent intent = new Intent(this,SeriesService.class);
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    if (pm.isIgnoringBatteryOptimizations(packageName))
            intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
    else {      intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
            intent.setData(Uri.parse("package:" + packageName));
    }
startService(intent);

我在OnePlus 5(运行Android派)上进行了尝试,并且可以正常工作

编辑

以防万一我在启动服务后添加了如何调用服务的信息

Context con = getView().getContext();//activity context
Intent intent = new Intent(con,SeriesService.class);
intent.setAction(SOME_ACTION);
intent.putExtra(KEY,data);
SeriesService.enqueueWork(con,intent);

答案 3 :(得分:0)

如果重写onCreate()方法,则永远不会调用onHandleWork()方法。只需擦除onCreate()方法即可。