0

虽然这已经回答了。我已经尝试过这些但没有运气。就我而言,我在同一个项目中使用 JobService 和 JobIntentService 进行测试和学习。JobService 工作正常,但是当我尝试 JobIntentService 时,它​​在我的情况下不起作用,我面临以下错误:

IllegalArgumentException: Scheduled service ComponentInfo{com.abdul_waheed.serviceandbackgroundtask/com.abdul_waheed.serviceandbackgroundtask.ExampleIntentService} does not require android.permission.BIND_JOB_SERVICE permission

尽管正如其他答案中所建议的那样,我需要在我的清单中添加权限,并且我已经给出了该权限,但仍然无法使其运行。下面是清单代码。

  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>

<!--If you don,t add this permission on PIE, foreground service exception will be thrown-->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>


<application
    android:name=".App"
    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">
    <activity android:name=".JobIntentServiceActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <service android:name=".ExampleJobService"
        android:permission="android.permission.BIND_JOB_SERVICE"/>
    <service android:name=".ExampleService"/>
    <service android:name=".ExampleIntentService"/>

    <service android:name=".ExampleJobIntentService"
        android:permission="android.permission.BIND_JOB_SERVICE"
        android:exported="false"/>

</application>

下面是我用于 JobIntentService 的代码以供参考或更好地理解

public class ExampleJobIntentService extends JobIntentService {

private static final String TAG = ExampleIntentService.class.getSimpleName();
/*
* Below method is like onHandleIntent of IntentService Class. This method runs on background thread
* automatically. We do not need to write wake lock code. It handled it automatically
* */

static void enqueuWork(Context context, Intent work) {
    enqueueWork(context,ExampleIntentService.class, 123, work);
}
@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreate");

}

@Override
protected void onHandleWork(@NonNull Intent intent) {
    Log.d(TAG, "onHandleWork");

    String input = intent.getStringExtra("input_extra");

    for (int i = 0; i < 10; i++) {
        Log.d(TAG, input + " - " + i);
        if (isStopped())
            return;
        SystemClock.sleep(1000);
    }
}

/*
* this will called when the has been stopped this will be called when it is using JobSchedular.
* This method is called when device requires memory or simple when it has been running since too long as Job has time limits
* which is around 10 minutes and after that they stop and defered
* */

@Override
public boolean onStopCurrentWork() {
    Log.d(TAG, "onStopCurrentWork");
    /*
    * default value is true==> that means when this methid is called should be resumed and yes it should in case of false not
    * to resume. If it started again it will start with same intent as it was passed in first attempt
    * */
    return super.onStopCurrentWork();
}

@Override
public void onDestroy() {
    super.onDestroy();
    Log.d(TAG, "onDestroy");
}

}

下面是负责调用我的服务或启动我的 JobIntentService 的方法代码

 public void enqueueWork(View view) {
    String input = etInput.getText().toString();
    Intent serviveIntent = new Intent(this, ExampleJobIntentService.class);
    serviveIntent.putExtra("input_extra", input);

    /*
    * If constraints needs to be set then it is not a suitable solution because it mimics as if it were IntentService class.
    * If constraints are required then JobSchedular is a better approach
    * */
    ExampleJobIntentService.enqueuWork(this, serviveIntent);


}

任何帮助将不胜感激。

4

1 回答 1

1

我刚刚注意到您是在打电话ExampleIntentService.class而不是ExampleJobIntentService.class在您的电话中,enqueueWork这可能是问题所在吗?

于 2019-12-04T11:00:02.523 回答