JobIntentService未能正确启动

4
在包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"/>

然而,当我运行程序时,它似乎并没有启动服务。我可以看到日志信息“调用成功”,但是我无法看到来自onHandleWork函数的日志信息。我是否以错误的方式启动了服务?


你在修复这个问题吗? - rafeek
如果您使用了JobIntentService,请移除onBind和onCreate方法。 - Saleh Mosleh
4个回答

1
在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 Pie),它可以工作。
编辑:
以防万一,我在这里添加如何在启动服务后调用我的服务的说明。
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);

这个答案救了我! :-) - Dave Nottage

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

0

考虑以这种方式开始编写:

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

0
如果您覆盖了 onCreate() 方法,那么 onHandleWork() 方法将不会被调用。只需删除 onCreate() 方法即可。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接