I am creating a long running foreground service in android. My service runs for while and then unexpected stops. My service logs information at almost every step of its work. Below is the logcat for right after the last logs my service makes stops (The first verbose entry being the last log my service creates):
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:com.liverail.library.inventory.c@42ff57a8
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
My service gets started in the onStop method of my Activity:
@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");
Below is some of the code from my service:
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);
}
}
The onDestroy method of my activity calls stopService in case that is relevant.How can I stop my service from stopping like this? Additionally do you have any suggestions as to how to make my code more resource efficient or any improvements you think I should make.