尽管已回答。我已经尝试过了,但是没有运气。就我而言,我在同一项目中使用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);
}
任何帮助将不胜感激。
我刚刚注意到您是在ExampleIntentService.class
中呼叫ExampleJobIntentService.class
而不是enqueueWork
,这可能是问题吗?