尽管Android服务可以

时间:2018-09-18 07:32:52

标签: android kotlin android-service job-scheduling android-jobscheduler

我创建了一个NetworkSchedulerService,它使用Kotlin扩展了JobService。 我当然在清单中注册了它,但是由于某些原因,AndroidStudio会用警告标记它:

the <service> project.services.network.NetworkSchedulerService is not registered in the manifest

当我运行该应用程序时,它崩溃并没有错误:

09-18 03:18:56.271 22289-22289/project E/AndroidRuntime: FATAL EXCEPTION: main
Process: project, PID: 22289
java.lang.RuntimeException: Unable to start activity ComponentInfo{project/project.ui.home.HomeScreenActivity}: java.lang.IllegalArgumentException: No such service ComponentInfo{projectt/project.services.network.NetworkSchedulerService}
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

货物清单注册:

    <service
        android:name="services.network.NetworkSchedulerService"
        android:exported="true"
        android:permission="android.permission.BIND_JOB_SERVICE"/>

我怎么称呼它(来自主要活动的onCreate()

fun scheduleJob() {
        val myJob = JobInfo.Builder(0, ComponentName(this, NetworkSchedulerService::class.java))
                .setRequiresCharging(true)
                .setMinimumLatency(1000)
                .setOverrideDeadline(2000)
                .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                .setPersisted(true)
                .build()

        val jobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
        jobScheduler.schedule(myJob)
}

我的NetworkSchedulerService班:

open class NetworkSchedulerService : JobService(), ConnectivityReceiver.ConnectivityReceiverListener {

    private val TAG = NetworkSchedulerService::class.java.simpleName
    private lateinit var mConnectivityReceiver: ConnectivityReceiver

    override fun onCreate() {
        super.onCreate()
        Log.i(TAG, "onCreate")
        mConnectivityReceiver = ConnectivityReceiver(this)
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        Log.i(TAG, "onStartCommand")
        return Service.START_NOT_STICKY
    }

    override fun onStopJob(params: JobParameters?): Boolean {
        Log.i(TAG, "onStopJob $mConnectivityReceiver")
        unregisterReceiver(mConnectivityReceiver)
        return true
    }

    override fun onStartJob(params: JobParameters?): Boolean {
        Log.i(TAG, "onStartJob $mConnectivityReceiver")
        registerReceiver(mConnectivityReceiver, IntentFilter(CONNECTIVITY_ACTION))
        return true
    }

    override fun onNetworkConnectionChanged(isConnected: Boolean) {
        val message = if (isConnected) "Good! Connected to Internet" else "Sorry! Not connected to internet"
        Toast.makeText(applicationContext, message, Toast.LENGTH_SHORT).show()
    }
}

我试图打开NetworkSchedulerService类(因为默认情况下它是静态的,因此它是最终的),试图清理项目以及许多其他事情。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

好吧,经过一整天都没有成功(在发布此问题之前),我发现了问题! 我不见了

var a = [
  {id:'1'},
  {id:'2'}
];
var b = [
  {id:'1',name:'a'},
  {id:'2',name:'b'},
  {id:'3',name:'c'}
]
console.log(checkobject());
function checkobject()
{
var checkid=true;
a.forEach(function(el)
{
 
   var check=b.findIndex(function(element) {
       return element.id===el.id;
   });
   if(check==-1)
   {
   checkid=false;
   return;
   }
    
});
  return checkid;
}
清单中的

。偶然发现的:)