首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >前台服务意外停止

前台服务意外停止
EN

Stack Overflow用户
提问于 2015-08-14 16:59:29
回答 2查看 729关注 0票数 0

我正在android中创建一个长期运行的前台服务。我的服务运行了一段时间,然后突然停止。我的服务几乎在其工作的每一步都记录信息。下面是在我的服务停止的最后一个日志之后的logcat (第一个详细的条目是我的服务创建的最后一个日志):

代码语言:javascript
复制
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

我的服务是从我的活动的onStop方法开始的:

代码语言:javascript
复制
 @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");

下面是我服务中的一些代码:

代码语言:javascript
复制
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,以防stopService--即relevant.How --可以停止这样停止服务吗?此外,对于如何使我的代码更有资源效率或您认为我应该做的任何改进,您有什么建议吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-08-14 17:29:50

为什么要在Activities方法中停止前台服务?如果服务是在onStop中启动的,那么在活动生命周期中调用的下一个服务将是onDestory,从而在不久之后停止您的服务。

删除活动onDestroy中停止服务的代码。

您应该删除服务中的睡眠和循环,转而使用带有延迟启动的Handler。在处理程序代码的末尾,再次以另一个延迟启动处理程序。如果搜索碰巧变成了false,我看不出在哪里,您可以从处理程序中删除runnable。

票数 1
EN

Stack Overflow用户

发布于 2015-08-14 17:36:01

我猜问题是从调用startInForeground方法的位置开始的。在onStartCommand方法中调用该方法。

文件上说

已启动的服务可以使用startForeground(int,Notification) API将服务置于前台状态,其中系统认为它是用户主动意识到的东西,因此在内存不足时不可能杀死它。(理论上说,在当前前台应用程序的巨大内存压力下,该服务仍然有可能被杀死,但在实践中,这不应该是一个问题。)

请跟随文档链接了解更多细节。也请看一下这个问题的答案

在调用startInForeground方法时,服务尚未启动。它正在被创造。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32015199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档