我试图让用户登录时保持我的服务在前台运行。我使用Android Developer website上的代码以及我自己的代码。
@Override
public void onCreate() {
super.onCreate();
System.out.println("KeepAliveService onCreate");
phoneState = PhoneState.getInstance();
mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNM.cancel(ONGOING_NOTIFICATION_ID); // in case of crash the icon is not removed
Intent notifIntent = new Intent(this, incomingReceivedActivity);
notifIntent.setAction(Intent.ACTION_MAIN);
notifIntent.addCategory(Intent.CATEGORY_LAUNCHER);
mNotifContentIntent = PendingIntent.getActivity(this, ONGOING_NOTIFICATION_ID, notifIntent, PendingIntent.FLAG_CANCEL_CURRENT);
mNotif = createNotification(this, mNotifContentIntent);
mWifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
startWifiLock();
instance = this;
// Retrieve methods to publish notification and keep Android
// from killing us and keep the audio quality high.
if (android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.ECLAIR) {
try {
mSetForeground = getClass().getMethod("setForeground", mSetFgSign);
} catch (NoSuchMethodException e) {
Log.v(TAG, "Couldn't find foreground method", e);
}
} else {
try {
mStartForeground = getClass().getMethod("startForeground", mStartFgSign);
mStopForeground = getClass().getMethod("stopForeground", mStopFgSign);
} catch (NoSuchMethodException e) {
Log.v(TAG, "Couldn't find startGoreground or stopForeground", e);
}
}
startForegroundCompat(ONGOING_NOTIFICATION_ID, mNotif);
if (!mTestDelayElapsed) {
// Only used when testing. Simulates a 5 seconds delay for launching service
mHandler.postDelayed(new Runnable() {
@Override public void run() {
mTestDelayElapsed = true;
}
}, 5000);
}
//make sure the application will at least wakes up every 5 mn
Intent intent = new Intent(this, KeepAliveHandler.class);
mkeepAlivePendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
((AlarmManager) this.getSystemService(Context.ALARM_SERVICE)).setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP
, SystemClock.elapsedRealtime()+300000
, 300000
, mkeepAlivePendingIntent);
}
使用这段代码,onDestory会立即被调用。我需要能够始终保持服务运行。我添加了打印输出以查看问题可能发生的地方,但到目前为止我无法弄明白。
logcat的:
05-27 17:35:20.777: I/System.out(20600): KeepAliveService onCreate
05-27 17:35:20.785: V/KeepAliveService(20600): !!!!!!!!!!!!!!startForegroundCompat!!!!!!!!!!!!!!!!
05-27 17:35:20.785: V/KeepAliveService(20600): !!!!!!!!!!!!!!invokeMethod!!!!!!!!!!!!!!!!
05-27 17:35:20.785: V/KeepAliveService(20600): !!!!!!!!!!!!!!notifyWrapper!!!!!!!!!!!!!!!!
05-27 17:35:20.871: D/dalvikvm(20600): GC_FOR_ALLOC freed 552K, 6% free 11353K/11952K, paused 12ms, total 13ms
05-27 17:35:21.066: D/dalvikvm(20600): GC_FOR_ALLOC freed 316K, 3% free 11872K/12224K, paused 17ms, total 20ms
05-27 17:35:21.207: D/dalvikvm(20600): GC_FOR_ALLOC freed 412K, 4% free 12430K/12896K, paused 25ms, total 26ms
05-27 17:35:21.332: D/dalvikvm(20600): GC_FOR_ALLOC freed 698K, 6% free 12925K/13660K, paused 19ms, total 19ms
05-27 17:35:21.543: D/dalvikvm(20600): GC_FOR_ALLOC freed 783K, 6% free 13501K/14320K, paused 25ms, total 26ms
05-27 17:35:21.605: D/dalvikvm(20600): GC_FOR_ALLOC freed 60K, 6% free 13517K/14320K, paused 24ms, total 25ms
05-27 17:35:21.605: I/dalvikvm-heap(20600): Grow heap (frag case) to 14.161MB for 972816-byte allocation
05-27 17:35:21.636: D/dalvikvm(20600): GC_FOR_ALLOC freed <1K, 6% free 14466K/15272K, paused 28ms, total 28ms
05-27 17:35:22.191: I/System.out(20600): KeepAliveService onDestroy
05-27 17:35:22.191: V/KeepAliveService(20600): !!!!!!!!!!!!!!stopForegroundCompat!!!!!!!!!!!!!!!!
05-27 17:35:22.191: V/KeepAliveService(20600): !!!!!!!!!!!!!!invokeMethod!!!!!!!!!!!!!!!!
在致电stopService
之前,我怎样才能使服务(服务而非服务)服务到最后?
我查看了以下堆栈溢出问题,到目前为止没有任何帮助我
Service.onDestroy() is called directly after creation, anyway the Service does its work
Android service killed immediately after start, despite calling startForeground()
答案 0 :(得分:0)
我所做的只是在我的Login onDestroy回调中删除了我的stopService
在
@Override
public void onDestroy(){
super.onDestroy();
Log.v(TAG, "!!!!!!!!!!!!!!Login onDestroy!!!!!!!!!!!!!!!!");
stopService(new Intent(ACTION_MAIN).setClass(this, KeepAliveService.class));
}
在
@Override
public void onDestroy(){
super.onDestroy();
}