Example usage for android.support.v4.app JobIntentService enqueueWork

List of usage examples for android.support.v4.app JobIntentService enqueueWork

Introduction

In this page you can find the example usage for android.support.v4.app JobIntentService enqueueWork.

Prototype

public static void enqueueWork(@NonNull Context context, @NonNull ComponentName component, int jobId,
        @NonNull Intent work) 

Source Link

Document

Like #enqueueWork(Context,Class,int,Intent) , but supplies a ComponentName for the service to interact with instead of its class.

Usage

From source file:cz.maresmar.sfm.plugin.RunPlanReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    if (ActionContract.BROADCAST_PLAN_RUN.equals(intent.getAction())) {
        int jobId = intent.getIntExtra(ActionContract.EXTRA_JOB_ID, 0);
        Intent intentToPlan = intent.getParcelableExtra(ActionContract.EXTRA_INTENT_TO_DO);
        ComponentName componentName = intentToPlan.getComponent();

        JobIntentService.enqueueWork(context, componentName, jobId, intentToPlan);
    }//from  ww  w .j  av a2  s  . c om
}

From source file:cz.maresmar.sfm.service.plugin.PluginUtils.java

/**
 * Starts plugin according to API level//from w  ww .  j  a  v  a2  s .c om
 *
 * @param context Some valid context
 * @param intent  Intent of plugin to be started
 * @see JobIntentService#enqueueWork(Context, ComponentName, int, Intent)
 */
public static void startPlugin(@NonNull Context context, @NonNull Intent intent) {
    // Test if the plugin exists
    PackageManager manager = context.getPackageManager();
    if (manager.queryIntentServices(intent, 0).size() != 1) {
        Timber.e("Plugin %s not found", intent.getComponent());
        throw new IllegalArgumentException("Plugin not found " + intent.getComponent());
    }

    // Finds jobId for selected plugin
    String pluginName = ProviderContract.buildString(intent.getComponent());
    int jobId;
    if (!mPluginsIds.containsKey(pluginName)) {
        jobId = mPluginsIds.size();
        mPluginsIds.put(pluginName, jobId);
    } else {
        jobId = mPluginsIds.get(pluginName);
    }

    // Starts plugin according to API level
    if ((Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
            || (context.getPackageName().equals(intent.getComponent().getPackageName()))) {
        JobIntentService.enqueueWork(context, intent.getComponent(), jobId, intent);
    } else {
        // On Android >= O with external plugin use BroadcastContract.BROADCAST_PLAN_RUN to
        // start plugin, because planning of external APK's service is not allowed

        Intent plan = new Intent();
        // Explicitly select a package to communicate with
        plan.setPackage(intent.getComponent().getPackageName());
        plan.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);

        plan.setAction(ActionContract.BROADCAST_PLAN_RUN);
        plan.putExtra(ActionContract.EXTRA_JOB_ID, jobId);
        plan.putExtra(ActionContract.EXTRA_INTENT_TO_DO, intent);
        context.sendBroadcast(plan);
    }
}