我建立了一个Jobservice来做一些事情。出于测试目的,我让它每30秒运行一次。为了找到问题,我减少了服务,什么都不做(!)。
我知道,系统可以阻止工作;如果onStopJob方法返回true,它将重新启动。这种情况发生并且效果很好,虽然我很想知道,经常和在哪个时间(在半夜,电话只是躺在桌子上!)。
要检查所有这些,我使用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();
}
}
答案 0 :(得分:1)
经过长时间的研究,我发现以下问题是: 在我的
// do something here
我有一个异步任务。但是,根据Google的说法,这只适用于UI线程(请参阅:https://developer.android.com/reference/android/os/AsyncTask.html) 在我的JobSchedulerService中使用它会导致它在一段时间后被销毁 我现在正在使用(常规)单独的线程来做&#34;做某事&#34;在服务中,它有效。