我为Unity编写了一个插件,但是在启动JobService时遇到此错误,这里是代码:
Baslangic.java
package com.alptugrul.background;
import android.app.Activity;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.app.job.JobService;
import android.content.ComponentName;
import android.content.Context;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
public class Baslangic extends UnityPlayerActivity{
public static Context contextAna;
public static Activity activity;
private static final int jobId = 1;
private static JobScheduler jobScheduler;
private static JobInfo jobInfo;
public static void setContext () {
contextAna = UnityPlayer.currentActivity.getApplicationContext();
activity = UnityPlayer.currentActivity;
ComponentName componentName = new ComponentName(contextAna,MJobScheduler.class);
JobInfo.Builder builder = new JobInfo.Builder(jobId,componentName);
builder.setPeriodic(5000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
builder.setPersisted(true);
jobInfo = builder.build();
jobScheduler = (JobScheduler) contextAna.getSystemService(JOB_SCHEDULER_SERVICE);
}
public static void mesajVer (String mesaj) {
Toast.makeText(contextAna,mesaj,Toast.LENGTH_LONG).show();
}
public static void baslat() {
jobScheduler.schedule(jobInfo);
Toast.makeText(contextAna,"Görev Planlandı",Toast.LENGTH_SHORT).show();
}
}
MJobScheduler.java:
package com.alptugrul.background;
import android.app.Activity;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.Context;
import android.widget.Toast;
import com.unity3d.player.UnityPlayer;
/**
* Created by Alpaslan on 15.3.2018.
*/
public class MJobScheduler extends JobService {
private MJobExecuter mJobExecuter;
public static Context contextAna;
public static Activity activity;
public static void setContext () {
contextAna = UnityPlayer.currentActivity.getApplicationContext();
activity = UnityPlayer.currentActivity;
}
@Override
public boolean onStartJob(final JobParameters jobParameters) {
mJobExecuter = new MJobExecuter(){
@Override
protected void onPostExecute(String s) {
Toast.makeText(contextAna,"Arkaplan yeni Mesaj",Toast.LENGTH_SHORT).show();
jobFinished(jobParameters,false);
}
};
mJobExecuter.execute();
return true;
}
@Override
public boolean onStopJob(JobParameters jobParameters) {
mJobExecuter.cancel(true);
return false;
}
}
和MJobExecuter.java:
package com.alptugrul.background;
import android.os.AsyncTask;
public class MJobExecuter extends AsyncTask<Void,Void,String> {
@Override
protected String doInBackground(Void... voids) {
return "Arkaplandan sa";
}
}
我在Unity中的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class deneme : MonoBehaviour {
private static string fullClassName = "com.alptugrul.background.Baslangic";
AndroidJavaClass pluginClass;
void Start () {
pluginClass = new AndroidJavaClass(fullClassName);
pluginClass.CallStatic ("setContext");
pluginClass.CallStatic ("mesajVer","Projen Çalışmaya Başladı!");
pluginClass.CallStatic ("baslat");
}
}
和我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alptugrul.background"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="25" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:theme="@style/UnityThemeSelector"
android:icon="@drawable/app_icon"
android:label="@string/app_name"
android:debuggable="true">
<service
android:name="com.alptugrul.background.MJobScheduler"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE" />
</application>
</manifest>
EDIT; 我构建我的apk但这个apk将无法安装。我将apk放到AVD上,但安装后没有应用程序是我的。