在 Android 中不需要 android.permission.BIND_JOB_SERVICE 权限

Posted

技术标签:

【中文标题】在 Android 中不需要 android.permission.BIND_JOB_SERVICE 权限【英文标题】:Does not require android.permission.BIND_JOB_SERVICE permission in Android 【发布时间】:2020-03-22 03:47:00 【问题描述】:

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

IllegalArgumentException: Scheduled service ComponentInfocom.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 而不是ExampleJobIntentService 有什么问题? 我的意思是,你得到的日志错误来自ExampleIntentService,但你显示的代码是ExampleJobIntentService。你能显示正确的代码吗? 是的,你是对的。这是真正的问题。 【参考方案1】:

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

【讨论】:

以上是关于在 Android 中不需要 android.permission.BIND_JOB_SERVICE 权限的主要内容,如果未能解决你的问题,请参考以下文章

在 Android 中不需要 android.permission.BIND_JOB_SERVICE 权限

Android中不需要的填充或按钮周围的边距

新行 (\n) 在 android 中不支持?

如何清理我的Android模拟器中不需要的应用程序?

推送通知在android studio中不起作用

React Native Overflow Touchable在Android中不起作用