2015-08-14 64 views
1

我在android中創建一個長時間運行的前臺服務。我的服務運行時,然後意外停止。我的服務幾乎在其工作的每一步都記錄了信息。下面是最後的日誌我的服務,使停止後的logcat的右(第一次詳細條目是最後的日誌我的服務創造):前臺服務意外停止

08-14 01:21:09.232 V/Screen Checking(30000): Bitmaps created sameAs check should be happening now 
08-14 01:21:09.592 D/WifiStateMachine( 845): handleMessage: E msg.what=131155 
08-14 01:21:09.592 D/WifiStateMachine( 845): processMsg: ConnectedState 
08-14 01:21:09.592 D/WifiStateMachine( 845): processMsg: L2ConnectedState 
08-14 01:21:09.592 D/WifiNative-wlan0( 845): doString: SIGNAL_POLL 
08-14 01:21:09.602 D/WifiStateMachine( 845): handleMessage: X 
08-14 01:21:09.912 I/Vold ( 391): [LGE][VOLD][NetlinkHandler.cpp][onEvent()] subsys:cpu, action:0 
08-14 01:21:09.932 D/BubblePopupHelper(1155): isShowingBubblePopup : false 
08-14 01:21:10.662 I/ThermalEngine( 412): Sensor:pa_therm0:39000 mC 
08-14 01:21:10.722 D/libc ( 396): _dns_getaddrinfo: iptype =1 
08-14 01:21:10.732 D/libc ( 396): _dns_getaddrinfo: query_ipv4=1, query_ipv6=0 
08-14 01:21:10.992 E/Parcel ( 466): Reading a NULL string not supported here. 
08-14 01:21:10.992 E/Parcel ( 466): Reading a NULL string not supported here. 
08-14 01:21:10.992 E/Parcel ( 466): Reading a NULL string not supported here. 
08-14 01:21:10.992 E/Parcel ( 466): Reading a NULL string not supported here. 
08-14 01:21:11.002 D/PhoneApp(1479): getIsInUseVoLTE : false 
08-14 01:21:11.012 D/PhoneApp(1479): getIsInUseVoLTE : false 
08-14 01:21:11.022 V/LGATCMDService(1247): onSignalStrengthsChanged signalStrength=SignalStrength: 99 0 -93 -115 -88 -75 3 99 2147483647 2147483647 2147483647 2147483647 2147483647 cdma 8 level=4 
08-14 01:21:11.022 D/PhoneApp(1479): getIsInUseVoLTE : false 
08-14 01:21:11.032 D/PhoneApp(1479): getIsInUseVoLTE : false 
08-14 01:21:11.042 D/PhoneApp(1479): getIsInUseVoLTE : false 
08-14 01:21:11.042 V/LGATCMDService(1247): broadcastRssiInfo() - Start RSSI : -93 ECIO : -115 
08-14 01:21:11.062 V/TelephonyAutoProfiling(31998): [getValue] FEATURE key : lgu_lte_single_device, value : null 
08-14 01:21:11.062 D/BubblePopupHelper(1155): isShowingBubblePopup : false 
08-14 01:21:11.062 D/PhoneApp(1479): getIsInUseVoLTE : false 
08-14 01:21:11.062 D/PhoneInterfaceManager(1479): [PhoneIntfMgr] sigLevel = 4 
08-14 01:21:11.062 D/PhoneApp(1479): getIsInUseVoLTE : false 
08-14 01:21:11.102 I/Vold ( 391): [LGE][VOLD][NetlinkHandler.cpp][onEvent()] subsys:cpu, action:0 
08-14 01:21:11.312 I/Vold ( 391): [LGE][VOLD][NetlinkHandler.cpp][onEvent()] subsys:cpu, action:0 
08-14 01:21:11.902 V/AudioFlinger( 405): Audio hardware entering standby, mixer 0xb5196008, suspend count 0 
08-14 01:21:11.902 V/audio_hw_primary( 405): out_standby: enter: usecase(0: deep-buffer-playback) 
08-14 01:21:12.192 V/audio_hw_primary( 405): stop_output_stream: enter: usecase(0: deep-buffer-playback) 
08-14 01:21:12.192 V/audio_hw_primary( 405): disable_audio_route: enter: usecase(0) 
08-14 01:21:12.192 V/audio_hw_primary( 405): disable_audio_route: reset mixer path: deep-buffer-playback 
08-14 01:21:12.192 V/audio_hw_primary( 405): disable_audio_route: exit 
08-14 01:21:12.192 D/hardware_info( 405): hw_info_append_hw_type : device_name = speaker 
08-14 01:21:12.192 V/audio_hw_primary( 405): disable_snd_device: snd_device(2: speaker) 
08-14 01:21:12.212 V/audio_hw_primary( 405): stop_output_stream: exit: status(0) 
08-14 01:21:12.212 V/audio_hw_primary( 405): out_standby: exit 
08-14 01:21:12.212 V/AudioFlinger( 405): releaseWakeLock_l() AudioOut_2 
08-14 01:21:12.212 V/AudioFlinger( 405): thread 0xb5196008 type 0 TID 663 going to sleep 
08-14 01:21:12.602 D/WifiStateMachine( 845): handleMessage: E msg.what=131155 
08-14 01:21:12.602 D/WifiStateMachine( 845): processMsg: ConnectedState 
08-14 01:21:12.602 D/WifiStateMachine( 845): processMsg: L2ConnectedState 
08-14 01:21:12.602 D/WifiNative-wlan0( 845): doString: SIGNAL_POLL 
08-14 01:21:12.632 D/WifiStateMachine( 845): handleMessage: X 
08-14 01:21:12.952 D/BubblePopupHelper(1155): isShowingBubblePopup : false 
08-14 01:21:13.952 D/BubblePopupHelper(1155): isShowingBubblePopup : false 
08-14 01:21:15.642 D/WifiStateMachine( 845): handleMessage: E msg.what=131155 
08-14 01:21:15.642 D/WifiStateMachine( 845): processMsg: ConnectedState 
08-14 01:21:15.642 D/WifiStateMachine( 845): processMsg: L2ConnectedState 
08-14 01:21:15.642 D/WifiNative-wlan0( 845): doString: SIGNAL_POLL 
08-14 01:21:15.662 D/WifiStateMachine( 845): handleMessage: X 
08-14 01:21:15.662 I/ThermalEngine( 412): Sensor:pa_therm0:39000 mC 
08-14 01:21:15.962 D/BubblePopupHelper(1155): isShowingBubblePopup : false 
08-14 01:21:16.972 D/BubblePopupHelper(1155): isShowingBubblePopup : false 
08-14 01:21:18.032 I/LiveRailSDK-101(29678): LiveRail init SDK version 2.4.0 
08-14 01:21:18.032 V/LiveRailSDK-101(29678): Adapters not included 
08-14 01:21:18.032 I/LiveRailSDK-101(29678): >> setDebug(false) 
08-14 01:21:18.032 I/LiveRailSDK-101(29678): AdView attached to window 
08-14 01:21:18.032 I/LiveRailSDK-101(29678): >> initAd() 
08-14 01:21:18.042 V/LiveRailSDK-101(29678): cleanup 
08-14 01:21:18.062 V/LiveRailSDK-101(29678): onSizeChanged() w=800 h=480 from oldw=0 oldh=0 
08-14 01:21:18.092 V/LiveRailSDK-101(29678): Ingest Run-time Parameters 
08-14 01:21:18.092 V/LiveRailSDK-101(29678): Found wifi connection LR_CONNECTIONTYPE=2 
08-14 01:21:18.102 D/LiveRailSDK-101(29678): Created new SLOT based inventory pod 
08-14 01:21:18.102 D/LiveRailSDK-101(29678): Pod position set to: 1 
08-14 01:21:18.102 I/LiveRailSDK-101(29678): Instantiated new inventory pod slot:[email protected] 
08-14 01:21:18.112 V/LiveRailSDK-101(29678): Delivery url=http://ad4.liverail.com/ 
08-14 01:21:18.112 V/LiveRailSDK-101(29678): CookieStore contains 312 cookies. 
08-14 01:21:18.112 V/LiveRailSDK-101(29678): Delivery timeout=10000 user-agent: Mozilla/5.0 (Linux; Android; Device) LiveRailSDK/2.4.0 (AB/com.juteralabs.perktv;AV/2.6.2;BV/65) 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): Delivery Run-time Parameters: 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_ADMAP=in::0 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_ADTYPE=3 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_ADUNIT=in 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_APPBUILD=65 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_APPNAME=Perk TV 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_APPVERS=2.6.2 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_BUNDLE=com.juteralabs.perktv 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_CARRIER=Chameleon 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_CONNECTIONTYPE=2 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_DEVICEID=6B37D6B834B51A08AC72F06522849AEA72362D72 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_DURATION=33 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_FORMAT=video/mp4;video/3gpp;video/webm 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_HEIGHT=480 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_IDFA=ae34a5b4-ed8a-4d82-8522-60059042ba4a 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_IDFA_FLAG=1 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_MAKE=LGE 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_MODEL=LGLS620 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_MUTED=1 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_OS=Android 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_OSVERS=4.4.2 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_POD_CURRENT=1 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_PUBLISHER_ID=13955 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_SCHEMA=liverail 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_SDK=android 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_SDK_VERSION=2.4.0 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_TAGS=8675307,livetv 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_VERTICALS= 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_VIDEO_POSITION=0 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_VIEWABLE=1 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): LR_WIDTH=800 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): AdManager specific parameters: 
08-14 01:21:18.112 D/LiveRailSDK-101(29678): layout(width=800 height=480) volume=0/15 

我的服務得到在我的活動的的onStop方法啓動

@Override 
protected void onStop() { 
    super.onStop(); 
    mServiceIntent = new Intent(this, Scan.class); 
    this.startService(mServiceIntent); 
    Log.v("MainActivity", "onStop called startService foregroundService should be starting"); 

下面是一些從我的服務代碼:

public class Scan extends Service { 
final int notificationID = 1; 
Notification foregroundNotification; 
NotificationManager notificationManager; 
ExecutorService executorService; 
public IBinder onBind(Intent mServiceIntent){return null;} 

@Override 
public void onCreate(){ 
startInForeground(); 
notificationManager = (NotificationManager)    this.getSystemService(Context.NOTIFICATION_SERVICE); 
executorService = Executors.newSingleThreadExecutor(); 
} 

@Override 
public int onStartCommand(Intent mServiceIntent,int flags, int startId) { 
//super.onStartCommand(mServiceIntent, flags, startId); 
executorService.execute(new Runnable() { 
    public void run() { 

     Log.v("Screen Checking", "Runnable successfully called"); 


     File First_Shot; 
     File Second_Shot; 
     boolean search = true; 


     Bitmap sec; 
     Bitmap current; 

     try { 
      Thread.sleep(30000); 
      while (search == true) { 


       Process capture = Runtime.getRuntime().exec("su -c screencap -p /storage/external_SD/first.png"); 
       capture.waitFor(); 
       Log.v("Screen Checking", "First screenshot taken 10 second wait"); 
       capture.destroy(); 
       Thread.sleep(10000); 
       Process getit = Runtime.getRuntime().exec("su -c screencap -p /storage/external_SD/second.png"); 
       getit.waitFor(); 
       Log.v("Screen Checking", "Second screenshot taken"); 
       getit.destroy(); 


       First_Shot = new File("/storage/external_SD/first.png"); 
       Log.v("Screen Checking", "File Exists and File can be read" + First_Shot.exists() + First_Shot.canRead()); 
       Second_Shot = new File("/storage/external_SD/second.png"); 
       Log.v("Screen Checking", "File Exists and File can be read" + Second_Shot.exists() + Second_Shot.canRead()); 

       current = BitmapFactory.decodeFile("/storage/external_SD/first.png"); 
       sec = BitmapFactory.decodeFile("/storage/external_SD/second.png"); 
       Log.v("Screen Checking", "Bitmaps created sameAs check should be happening now"); 
       if (current.sameAs(sec) == true) { 


        Log.v("Screen Checking", "inside touch if they matched"); 
Process p = Runtime.getRuntime().exec("su -c input tap 400 314"); 
p.waitfor; 
Log.v("Screen Checking", command successfully executed.") 


       } else if (current.getConfig() == sec.getConfig() && current.getHeight() == sec.getHeight() && current.getWidth() == sec.getWidth()) { 
        Log.v("Screen Checking", "inside touch else-if Image Data was not the same"); 
       } else { 
        Log.v("Screen Checking", "Image Configuration or Dimensions were not the same see logs with the tag 'otherImageData'"); 
        Log.v("otherImageData", "Current Config followed by Sec config" + current.getConfig() + sec.getConfig()); 
        Log.v("otherImageData", "Current Height followed by Sec height" + current.getHeight() + sec.getHeight()); 
        Log.v("otherImageData", "Current Width followed by sec width" + current.getWidth() + sec.getWidth()); 

       } 
      } 
     } catch (InterruptedException | IOException e) { 
     } 
    } 
}); 
Log.v("Screen Checking", "onStartCommand Return just called"); 



return START_STICKY; 
} 

@Override 
public void onDestroy(){ 
Log.v("Screen Checking", "onDestroy for the service was called, the executorService has called shutdownNow and super.onDestroy has been called."); 
executorService.shutdownNow(); 
super.onDestroy(); 
} 

void startInForeground(){ 
int notificationIcon = R.drawable.notificationimage; 
String text = "It has begun"; 
long notificationTimeStamp = System.currentTimeMillis(); 
foregroundNotification = new Notification(notificationIcon, text, notificationTimeStamp); 
String title = "Title"; 
String body = "Body"; 
Intent mainActivityIntent = new Intent (this, MainActivity.class); 
PendingIntent pendingmainActivityIntent = PendingIntent.getActivity(this, 0, mainActivityIntent, 0); 
foregroundNotification.setLatestEventInfo(this, title, body, pendingmainActivityIntent); 
startForeground(notificationID, foregroundNotification); 


} 
    void setStatusText(String statusText){ 
    foregroundNotification.tickerText = statusText; 
    notificationManager.notify(notificationID, foregroundNotification); 
} 

} 

我的活動的方法的onDestroy的情況下調用stopService是relevant.How我能阻止stoppi我的服務像這樣?另外,你有什麼建議可以讓我的代碼更加節省資源,或者你認爲我應該改進的任何改進。

回答

1

爲什麼要停止Activity onDestroy方法中的前臺服務?如果服務在onStop中啓動,則在活動生命週期中調用的下一個將在onDestory上,從而在不久後停止服務。

刪除在您的活動onDestroy中停止服務的代碼。

您應該刪除服務中的休眠和循環,並使用帶有延遲啓動的處理程序。在處理程序代碼結束時,再次啓動處理程序,並延遲一段時間。如果搜索發生錯誤,我看不到在哪裏,您可以從處理程序中刪除可運行的程序。

+0

只要保持服務運行,就行了,所以謝謝。然而,我遇到了另一個問題,當服務將兩個圖像標識爲相同時,它會在if語句內記錄第一個日誌,但永遠不會從第二個日誌開始,並且永遠不會恢復循環。也就是說,if語句中的第一個日誌表示圖像匹配始終是要記錄的最後一個日誌。讓我相信,服務在if語句內部的命令後面停滯不前。你能幫我解決這個問題嗎? – Jon

+0

也許這將有助於:http://stackoverflow.com/questions/5483830/process-waitfor-never-returns並看看引用的文章。 –

0

我的猜測是問題來自您撥打startInForeground方法的位置。在onStartCommand方法中調用該方法。

文檔說

一個啓動的服務可以使用startForeground(INT,通知)API把服務前臺狀態,當系統認爲它是一些用戶正在積極瞭解並因此當內存不足時不會成爲殺戮的候選人。 (它仍然是理論上可能的服務下,從目前的前臺應用程序極端的內存壓力被殺死,但在實踐中,這不應該是一個問題。)

按照the doc link瞭解更多詳情。也請看this question

的答案你打電話給你的startInForeground方法,你的服務還沒有開始。它正在被創建。

+0

我認爲當通知出現並保持在狀態欄中時,服務會在前臺成功啓動。不過,我會盡力確保 – Jon

+0

@Jon,如果你得到了notif,那肯定是這個服務在前臺。我的猜測是錯誤的。 – innoSPG