androidstudio服务在Androidx中不工作
活动代码androidstudio服务在Androidx中不工作,android,jobservice,Android,Jobservice,活动代码 JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); ComponentName componentName = new ComponentName(this, EJobService.class); JobInfo info = new JobInfo.Builder(123, componentName)
JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
ComponentName componentName = new ComponentName(this, EJobService.class);
JobInfo info = new JobInfo.Builder(123, componentName)
.setRequiresCharging(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED)
.setPersisted(true)
.setPeriodic(5*1000,500)
.build();
int resultCode = scheduler.schedule(info);
if (resultCode == JobScheduler.RESULT_SUCCESS) {
Log.d("Job Started", "Job scheduled");
} else {
Log.d("Tag", "Job scheduling failed");
}
服务代码
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class EJobService extends JobService {
private static final String TAG = "EJobService";
private boolean jobCancelled = false;
@Override
public boolean onStartJob(JobParameters params) {
getLocation();
return true;
}
private void doBackgroundWork(final JobParameters params) {
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
Log.d(TAG, "run: " + i);
if (jobCancelled) {
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Log.d(TAG, "Job finished");
jobFinished(params, false);
}
}).start();
}
@Override
public boolean onStopJob(JobParameters params) {
Log.d(TAG, "Job cancelled before completion");
jobCancelled = true;
return true;
}
public void getLocation() {
LocationManager locationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 5, this);
} catch (SecurityException e) {
e.printStackTrace();
}
}
@RequiresApi(api=Build.VERSION\u CODES.LOLLIPOP)
公共类EJobService扩展了JobService{
私有静态最终字符串TAG=“EJobService”;
私有布尔jobCancelled=false;
@凌驾
公共布尔值onStartJob(JobParameters参数){
getLocation();
返回true;
}
专用void dobackground(最终作业参数参数){
新线程(newrunnable()){
@凌驾
公开募捐{
对于(int i=0;i<10;i++){
日志d(标签“运行:+i”);
如果(工作取消){
返回;
}
试一试{
睡眠(1000);
}捕捉(中断异常e){
e、 printStackTrace();
}
}
Log.d(标记“作业完成”);
jobFinished(参数,false);
}
}).start();
}
@凌驾
公共布尔onStopJob(JobParameters参数){
日志d(标签“完成前取消的作业”);
jobCancelled=true;
返回true;
}
public void getLocation(){
LocationManager LocationManager=(LocationManager)getApplicationContext().getSystemService(Context.LOCATION\u服务);
试一试{
locationManager.RequestLocationUpdate(locationManager.NETWORK_提供程序,5000,5,本);
}捕获(安全异常e){
e、 printStackTrace();
}
}
AndroidManifest.Xml
<service
android:name=".EJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
我正在将此代码用于作业计划程序服务,但问题是服务未运行。请告诉我问题出在哪里。我在Oreo版本中使用过此代码,该代码工作正常,因此请告诉我问题出在哪里
Getlocation();崩溃应用程序您的代码工作正常,只需检查控制台中的输出。您的代码工作正常,只需检查控制台中的输出。确切的问题是什么?在此处发布logcat,您的要求是什么?即使在logcat中也不会显示错误无应用程序崩溃无服务输出。服务未从acti调用vity。我也尝试过上下文更改。这在oreo中运行良好。
doBackgroundWork
不应该在onStartJob中吗?您没有调用您的doBackgroundWork(params)
在您的onStartJob
回调中,我这样做了,之后我删除了doBackgroundWork并尝试显示toast。但不显示toast两种方法都不起作用。这在oreo版本中效果很好。但我不知道为什么相同的代码在androidx中不起作用。确切的问题是什么?在这里发布logcat,您的要求是什么?没有错误然而,即使在logcat中,没有应用程序崩溃,服务也没有输出。服务没有从活动调用。我也尝试了上下文更改。这在oreo中运行良好。doBackgroundWork
不应该在onStartJob中吗?您没有调用您doBackgroundWork(params)
在你的onStartJob
回调中,我这样做了,之后我删除了doBackgroundWork并尝试显示toast。但不显示toast两种方法都不起作用。这在oreo版本中效果很好。但我不知道为什么同样的代码在androidx中不起作用。