如何在nativescript-vue中制作JobService

时间:2019-03-17 00:34:55

标签: android vue.js nativescript nativescript-vue jobservice

我在google中搜索了几个小时,但是我只有3个页面遇到了这个问题,没有一个帮助了我。

我在tns上输入:5.3.0

TestJobService.js

(function getAllUsers() {
    $.ajax({
        url: 'staff/getAllUsers.php',
        success: function(data) {
            try {
                const list = JSON.parse(data); // Parse JSON from string to object
                const selectEl = $('.getAllUsers').empty(); // Clear the content of both `select` elements
                for ( let i=0; i<list.length; i++ ) { // Loop through each item in the JSON array
                    $('<option />') // Create an `option` element
                        .attr('value', list[i].id) // Set attribute `value` to `option` element
                        .text(list[i].name) // Set `option` text (this function also escapes any special characters, which prevents potential XSS attacks)
                        .appendTo(selectEl); // Add `option` element to both `select` elements
                }
            } catch (e) {
                // Report any errors with the JSON parsing process to the developer console.
                console.error(e);
            }
        },
        error: function(jqXHR, textStatus, errorThrown) {
            // Track any errors received from the server for debugging purposes
            console.error(textStatus, errorThrown);
        }
        complete: function() {
            // Schedule the next request when the current one's complete
            setTimeout(getAllUsers, 5000);
        },
        dataType: 'json' // Expect a `json` back from server
    });
}());

Home.vue

android.app.job.JobService.extend("org.nativescipt.ITPalert.TestJobService", {
 onStartJob() {
  console.log("Job execution ...");
  // here you can do whatever you want
  //this.jobFinished(params, true); //this ends the job if successful, if not `return false;`
  return true;
 },

 onStopJob() {
  console.log("Stopping job ...");
  return true; //returning true makes the task reschedule
 },
});

,在我的AndroidManifest.xml中,我有:

startBackground() {
    const context = utils.ad.getApplicationContext();
    const component = new android.content.ComponentName(context, org.nativescipt.ITPalert.TestJobService.class);
    const builder = new android.app.job.JobInfo.Builder(1, component);
    builder.setRequiredNetworkType(android.app.job.JobInfo.NETWORK_TYPE_ANY);
    builder.setMinimumLatency(1000 * 60)
    builder.setOverrideDeadline(1000 * 100)
    builder.setPersisted(true)

    const jobScheduler = context.getSystemService(android.content.Context.JOB_SCHEDULER_SERVICE);
    const service = jobScheduler.schedule(builder.build());
    console.log(`Job Scheduled: ${jobScheduler.schedule(builder.build())}`);
  },

我在webpack.config.js中注册了该工作:

<service android:name="org.nativescipt.ITPalert.TestJobService"
                    android:permission="android.permission.BIND_JOB_SERVICE"
                    android:enabled="true"
                    android:exported="false" />

我得到了这个错误:

  const appComponents = [
    "tns-core-modules/ui/frame",
    "tns-core-modules/ui/frame/activity",
    resolve(__dirname, "app/Jobs/TestJobService.js"),
 ];

我使用--bundle和--hmr构建了我的项目。

1 个答案:

答案 0 :(得分:0)

我需要运行:tns平台干净的android,因为我的AndroidManifest.xml没有在构建中传播...

如果可以帮助某人,我在这里留下问题。