我有一个CountDownTimer服务,我在调用mainActivity的onCreate()方法时启动它。问题是,一旦我离开应用程序,该服务就会停止。我已经查看了this上的很多教程,但我仍然让服务停止了。这就是我得到的,即使在我退出应用程序后,我还能做些什么才能保持服务的正常运行?
mainActivity:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
//create new notifications based from settings
ni = new NotifyTimeInterface(this.getApplicationContext());
setTimes();
//register a receiver to update the "next notify text"
registerReceiver(bcsr, new IntentFilter("update_next_text"));
//start the notifications/adhan service, pass the prayer times
countdownServiceIntent.putExtra("timingsArray", timings);
startService(countdownServiceIntent);
}
@Override
protected void onResume()
{
super.onResume();
//should we get back from settings, redraw the main screen
this.onCreate(null);
}
@Override
public void onDestroy()
{
//if I do not stop the service I get a service leak
stopService(countdownServiceIntent);
unregisterReceiver(bcsr);
super.onDestroy();
}
在CountDownService中:
public class CountDownService extends Service
{
CountDownTime ctx;
Date n1;
Date n2;
Date n3;
Date n4;
Date n5;
Date nNext;
Intent soundServiceIntent;
Date[] times = null;
Intent nextTextIntent;
public CountDownService()
{
}
public void setTimes(Date f, Date d, Date a, Date m, Date i)
{
//set the times passed in from main to here
}
protected void onHandleIntent(Intent i)
{
try
{
times = (Date[]) i.getSerializableExtra("timingsArray");
}
catch (Exception e)
{
e.printStackTrace();
return;
}
setTimes(times[0], times[1], times[2], times[3], times[4]);
}
@Override
public void onCreate()
{
super.onCreate();
}
@Override
public void onDestroy()
{
super.onDestroy();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
soundServiceIntent = new Intent(this, SoundService.class);
onHandleIntent(intent);
getNextTime();
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
public void startTicking()
{
Date nowDate = Calendar.getInstance().getTime();
long difference = 0;
if(nextPrayerTime != null)
{
difference = nextNotifyTime.getTime() - nowDate.getTime();
}
if(difference > 0)
{
ctx = new CountDownTime(difference, 1000);
ctx.start();
}
}
public void getNextTime()
{
//next notification time is set here...
//afterwards, start the timer
startTicking();
}
private void addNotification()
{
NotificationCompat.Builder builder =
(android.support.v7.app.NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.mini_icon)
.setContentTitle("Notification!")
.setContentText(Calendar.getInstance().getTime().toString());
Intent notificationIntent = new Intent(this, MainScreen.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, builder.build());
return;
}
public class CountDownTime extends CountDownTimer
{
public CountDownTime(long millisInFuture, long countDownInterval)
{
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished)
{
String textString = ("Next notify in: " + String.format("%02d:%02d:%02d", (millisUntilFinished / (1000 * 60 * 60)),
((millisUntilFinished / (1000 * 60)) % 60), ((millisUntilFinished / 1000) % 60)));
nextTextIntent = new Intent("update_next_text");
nextTextIntent.putExtra("countdownTime", textString);
sendBroadcast(nextTextIntent);
}
@Override
public void onFinish()
{
startService(soundServiceIntent);
addNotification();
getNextTime();
}
}