简体   繁体   中英

Does not require android.permission.BIND_JOB_SERVICE permission in Android

Although this has been answered. I have tried those but no luck. In my case, I am using JobService and JobIntentService in same project for testing and learning things. JobService is working fine but when I am trying JobIntentService it is not working in my case and I am facing below error:

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

Although as suggested in other answers, I am required to add permission in my manifest and I have already given that and still not able make it run. Below is the manifest code.

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

And below is the code which I am using for JobIntentService for reference or to get better understandin

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

}

Below is the method code that is reponsible for calling my service or starting my 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);


}

Any help would be highly appreciated.

I've just notice that you're calling ExampleIntentService.class instead of ExampleJobIntentService.class in your enqueueWork could that be the issue?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM