gpt4 book ai didi

Android Jobservice 被销毁

转载 作者:行者123 更新时间:2023-11-30 01:10:58 25 4
gpt4 key购买 nike

我建立了一个 Jobservice 来做一些事情。出于测试目的,我让它每三十秒运行一次。为了找到问题,我将服务减少到什么都不做(!)。我知道,系统可以停止工作;如果 onStopJob 方法返回 true 它将重新启动。这种情况会发生并且效果很好,虽然我想知道发生的频率和时间(在半夜,手机刚好放在 table 上!)。
为了检查所有这些,我正在使用 SharedPreferences,跟踪停止和重新启动。
然而,我发现,这项工作不仅停止了,而且有时甚至被完全摧毁了。然后我在 OnDestroy 方法中设置了一个重启例程。这是行不通的。作业正在重新启动,但立即又被销毁了。所以我有一些问题:
为什么它首先被销毁?
为什么不能重新启动它?
我该怎么做才能让它更稳定?
在 Android 5.0.1 下的 Galaxy S4 和 6.0 下的 XPeria Z2 上试用
针对 MinSDK 5.0、TargetSDK 6.0 编译。
这是我的 JobSchedulerService 的代码:

public class  JobSchedulerService extends JobService implements
DataApi.DataListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener
{

private boolean stoppedByUser = false;
public DateFormat formatter;
String stoptimeText;
public GregorianCalendar apptTime;
public Context myCnt;

@Override
public boolean onStartJob(JobParameters params){
doJob(this);
jobFinished(params, false );
return true;
}

public void doJob(Context context){
myCnt = context;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(myCnt);
SharedPreferences.Editor editor = prefs.edit();
stoptimeText = prefs.getString("stopped","not yet stopped");
editor.putString("started", "Job was running at " + timeNow());
editor.apply();
// do something here
}

@Override
public boolean onStopJob(JobParameters params){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
stoppedByUser = prefs.getBoolean("stoppedByUser", false);
SharedPreferences.Editor editor = prefs.edit();
stoptimeText += "Job stopped autom. at " + timeNow();
editor.putString("stopped", stoptimeText);
editor.apply();
if (stoppedByUser){
stoppedByUser = false;
editor.putBoolean("stoppedByUser", false);
stoptimeText += "OnStopJob called by user-stop at " + timeNow();
editor.putString("stopped", stoptimeText);
editor.apply();
return false;
}
else return true;
}

@Override
public void onDestroy() {
super.onDestroy();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
stoppedByUser = prefs.getBoolean("stoppedByUser", false);
SharedPreferences.Editor editor = prefs.edit();
stoptimeText += "Job was destroyed at "+ timeNow();
editor.putString("stopped", stoptimeText);
editor.apply();
if (!stoppedByUser){
JobInfo.Builder builder = new JobInfo.Builder(1,
new ComponentName(getPackageName(), JobSchedulerService.class.getName()));
builder.setPeriodic(30 * 1000);
builder.setPersisted(true);
stoptimeText += "Job rest. after dstr. at " + timeNow();
editor.putString("stopped", stoptimeText);
editor.putString("started", "Auto update started after destroy! Check if really running!");
editor.apply();
}
else {
stoptimeText += "OnDestroy called by user-stop at " + timeNow();
editor.putString("stopped", stoptimeText);
editor.apply();
}
}

public String timeNow(){
apptTime = (GregorianCalendar) GregorianCalendar.getInstance(TimeZone.getTimeZone("UTC"));
formatter = new SimpleDateFormat("EEE, dd./HH:mm", Locale.ENGLISH);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
return formatter.format(apptTime.getTimeInMillis());
}

@Override
public void onDataChanged(DataEventBuffer dataEvents) {
}

@Override
public void onConnected(Bundle connectionHint) {
}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
}

@Override
public void onConnectionSuspended(int arg0) {
}
}

这是我的 MainActivity(缩写):

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

myCnt = this;

initGoogleApiClient();

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
if (tabLayout != null)
tabLayout.setupWithViewPager(viewPager);

mJobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);

//.......

public void startJob(){
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("started", "auto updates started by user!");
editor.putBoolean("stoppedByUser", false);
editor.apply();

JobSchedulerService jss = new JobSchedulerService(); // used to "first run" the job, so there is no waiting time for the user
jss.doJob(myCnt);

JobInfo.Builder builder = new JobInfo.Builder(1,
new ComponentName(getPackageName(), JobSchedulerService.class.getName()));
builder.setPeriodic(30 * 1000);
builder.setPersisted(true);

if (mJobScheduler.schedule(builder.build()) <= 0) {
Toast.makeText(MainActivity.this, "Failure starting Jobservice!", Toast.LENGTH_LONG).show();
}
}

最佳答案

经过长时间的研究,我发现以下是问题所在:在我的

// do something here  

我有一个异步任务。但是,根据 Google 的说法,这仅适用于 UI 线程(请参阅:https://developer.android.com/reference/android/os/AsyncTask.html)。
在我的 JobSchedulerService 中使用它导致它在一段时间后被销毁。
我现在使用(常规)单独的线程在服务中“做某事”,并且它有效。

关于Android Jobservice 被销毁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38432238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com