0

在我的 android 应用程序中,我将 Application 类子类化并启动一个服务。该服务有一个负责监控空闲时间的线程,如果应用程序保持空闲超过 1 分钟,服务会告诉我的应用程序,并且应用程序会锁定用户会话。

这是代码,即使我没有发送任何广播我也面临这个问题..

public class MainApp extends Application
{
    public void onCreate()
    {
        Log.i("Sharp:MainApp", "Application - OnCreate");
        super.onCreate();
        startInactivityMonitorService();
    }

    public void startInactivityMonitorService()
    {
            //start the activity monitoring service
            Log.i("Sharp:MainApp", "Starting the activity monitoring service...");
            Intent intent = new Intent(getApplicationContext(), InactivityMonitorService.class);
            intent.putExtra(InactivityMonitorService.IDLE_TIMEOUT, 15000);
            startService(intent);
            Log.i("Sharp:MainApp", "Completed starting the activity monitoring service.");      
    }
}

public class InactivityMonitorService extends Service
{
    public static String IDLE_TIMEOUT   = "IDLE_TIMEOUT";

    public int onStartCommand(Intent intent, int flags, int startId)
    {
       idleTimeout = intent.getExtras().getInt(InactivityMonitorService.IDLE_TIMEOUT);

       if (monitorThread == null) {
           monitorThread = new InactivityMonitorThread();
           monitorThread.start();
       }
    }


   private class InactivityMonitorThread extends Thread
   {
    public void run()
    {
        do {
                long idleDuration = System.currentTimeMillis() - lastUserInteractionTime;        

                //check if we have exceeded the idle timeout
                if (idleDuration > idleTimeout) {
                    //Send broadcast message
                }

                   SystemClock.sleep(1000);
        } while (!stopMonitoring);
    }
}
}

启动应用程序后,我退出它,将应用程序保持在后台,几秒钟后我的应用程序的 onCreate 被调用,它试图启动服务并以崩溃告终!

崩溃是由于服务的 onStartCommand 中的 NullPointerException 而发生的。以下是我收到的日志。我更关心的是,为什么我的应用程序会被重新创建?

以下 2 行为我提供了重新创建应用程序的证据。

夏普:MainApp(14627):应用程序 - OnCreate

Sharp:MainApp(14627): 启动活动监控服务...

10-20 12:07:48.365: I/SurfaceTextureClient(579): [STC::queueBuffer] (this:0x5e364890) fps:0.98, dur:1015.74, max:1015.74, min:1015.74
10-20 12:07:48.365: I/BufferQueue(130): [StatusBar](this:0x42610018,api:1) [queue] fps:0.98, dur:1016.44, max:1016.44, min:1016.44
10-20 12:07:48.366: W/Trace(579): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.366: W/Trace(579): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.370: I/BufferQueue(130): [StatusBar](this:0x42610018,api:1) [release] fps:0.99, dur:1008.45, max:1008.45, min:1008.45
10-20 12:07:48.371: I/SurfaceFlinger(130): [SurfaceFlinger] fps:14.868297,dur:1008.86,max:84.01,min:22.94
10-20 12:07:48.589: V/Provider/Settings(489):  from settings cache , name = read_external_storage_enforced_default , value = null
10-20 12:07:48.589: W/Trace(489): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.589: W/Trace(489): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.590: D/dalvikvm(131): threadid=2: exiting
10-20 12:07:48.590: D/dalvikvm(131): threadid=2: bye!
10-20 12:07:48.591: D/dalvikvm(131): threadid=3: exiting
10-20 12:07:48.591: D/dalvikvm(131): threadid=3: bye!
10-20 12:07:48.591: D/dalvikvm(131): threadid=4: exiting
10-20 12:07:48.591: D/dalvikvm(131): threadid=4: bye!
10-20 12:07:48.591: D/dalvikvm(131): pre gc
10-20 12:07:48.591: D/dalvikvm(131): Zygote::ForkAndSpecialize +
10-20 12:07:48.594: D/dalvikvm(131): Zygote::ForkAndSpecialize : 14627
10-20 12:07:48.594: D/dalvikvm(131): create interp thread : stack size=32KB
10-20 12:07:48.595: D/dalvikvm(131): create new thread
10-20 12:07:48.595: D/dalvikvm(131): new thread created
10-20 12:07:48.595: D/dalvikvm(131): update thread list
10-20 12:07:48.596: D/dalvikvm(14627): Zygote::ForkAndSpecialize : 0
10-20 12:07:48.596: D/dalvikvm(14627): zygote get new systemTid : 14627
10-20 12:07:48.596: D/dalvikvm(14627): Late-enabling CheckJNI
10-20 12:07:48.597: D/dalvikvm(14627): threadid=2: interp stack at 0x5a858000
10-20 12:07:48.598: D/dalvikvm(14627): threadid=3: interp stack at 0x5a960000
10-20 12:07:48.598: D/jdwp(14627): prepping for JDWP over ADB
10-20 12:07:48.598: D/dalvikvm(131): threadid=2: interp stack at 0x5a758000
10-20 12:07:48.598: D/dalvikvm(131): threadid=2: created from interp
10-20 12:07:48.598: D/dalvikvm(131): start new thread
10-20 12:07:48.598: D/dalvikvm(131): create interp thread : stack size=32KB
10-20 12:07:48.598: D/dalvikvm(131): create new thread
10-20 12:07:48.598: D/dalvikvm(131): new thread created
10-20 12:07:48.598: D/dalvikvm(131): update thread list
10-20 12:07:48.599: D/dalvikvm(131): threadid=2: notify debugger
10-20 12:07:48.599: D/dalvikvm(131): threadid=2 (ReferenceQueueDaemon): calling run()
10-20 12:07:48.599: D/jdwp(14627): ADB transport startup
10-20 12:07:48.599: D/dalvikvm(14627): Elevating priority from 0 to -8
10-20 12:07:48.599: D/dalvikvm(14627): threadid=4: interp stack at 0x5aa68000
10-20 12:07:48.600: D/jdwp(14627): JDWP: thread running
10-20 12:07:48.600: D/jdwp(14627): acceptConnection
10-20 12:07:48.600: D/ADB_SERVICES(284): Adding socket 19 pid 14627 to jdwp process list
10-20 12:07:48.600: D/jdwp(14627): trying to receive file descriptor from ADB
10-20 12:07:48.600: D/dalvikvm(14627): threadid=5: interp stack at 0x5d08f000
10-20 12:07:48.601: D/dalvikvm(14627): zygote get thread init done
10-20 12:07:48.601: D/dalvikvm(131): threadid=3: interp stack at 0x5a860000
10-20 12:07:48.601: D/dalvikvm(131): threadid=3: created from interp
10-20 12:07:48.601: D/dalvikvm(131): start new thread
10-20 12:07:48.601: D/dalvikvm(131): create interp thread : stack size=32KB
10-20 12:07:48.601: D/dalvikvm(131): create new thread
10-20 12:07:48.601: D/dalvikvm(131): new thread created
10-20 12:07:48.601: D/dalvikvm(131): update thread list
10-20 12:07:48.601: D/dalvikvm(131): threadid=3: notify debugger
10-20 12:07:48.602: D/dalvikvm(131): threadid=3 (FinalizerDaemon): calling run()
10-20 12:07:48.602: D/dalvikvm(14627): create interp thread : stack size=32KB
10-20 12:07:48.602: D/dalvikvm(14627): create new thread
10-20 12:07:48.602: D/dalvikvm(14627): new thread created
10-20 12:07:48.602: D/dalvikvm(14627): update thread list
10-20 12:07:48.602: D/dalvikvm(14627): threadid=6: interp stack at 0x5d097000
10-20 12:07:48.602: D/dalvikvm(14627): threadid=6: created from interp
10-20 12:07:48.603: D/dalvikvm(14627): start new thread
10-20 12:07:48.603: D/dalvikvm(14627): create interp thread : stack size=32KB
10-20 12:07:48.603: D/dalvikvm(14627): create new thread
10-20 12:07:48.603: D/dalvikvm(14627): new thread created
10-20 12:07:48.603: D/dalvikvm(14627): update thread list
10-20 12:07:48.603: D/dalvikvm(14627): threadid=6: notify debugger
10-20 12:07:48.603: D/dalvikvm(14627): threadid=6 (ReferenceQueueDaemon): calling run()
10-20 12:07:48.603: W/ADB_SERVICES(284): create_local_service_socket() name=jdwp:14627
10-20 12:07:48.603: W/ADB_SERVICES(284): looking for pid 14627 in JDWP process list return fds0(20) fds1(21)
10-20 12:07:48.603: W/ADB_SERVICES(284): trying to write to JDWP socket=19 pid=14627 count=1 out_fds=21
10-20 12:07:48.604: D/jdwp(14627): received file descriptor 39 from ADB
10-20 12:07:48.604: D/dalvikvm(14627): threadid=7: interp stack at 0x5d19f000
10-20 12:07:48.604: D/dalvikvm(14627): threadid=7: created from interp
10-20 12:07:48.604: D/dalvikvm(131): threadid=4: interp stack at 0x5a968000
10-20 12:07:48.605: D/dalvikvm(131): threadid=4: created from interp
10-20 12:07:48.605: D/dalvikvm(131): start new thread
10-20 12:07:48.605: D/dalvikvm(131): threadid=4: notify debugger
10-20 12:07:48.605: D/dalvikvm(131): threadid=4 (FinalizerWatchdogDaemon): calling run()
10-20 12:07:48.606: I/ActivityManager(489): Start proc com.acs.sharp.app for service com.acs.sharp.app/com.acs.android.fwk.background.InactivityMonitorService: pid=14627 uid=10099 gids={50099, 1028}
10-20 12:07:48.606: W/Trace(489): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.606: W/Trace(489): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.608: D/jdwp(14627): processIncoming
10-20 12:07:48.610: D/jdwp(14627): processIncoming
10-20 12:07:48.610: D/jdwp(14627): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x4000005A, flags=0x0, dataLen=0x8
10-20 12:07:48.612: D/dalvikvm(14627): start new thread
10-20 12:07:48.612: D/dalvikvm(14627): create interp thread : stack size=32KB
10-20 12:07:48.612: D/dalvikvm(14627): create new thread
10-20 12:07:48.612: D/dalvikvm(14627): new thread created
10-20 12:07:48.612: D/dalvikvm(14627): update thread list
10-20 12:07:48.612: D/jdwp(14627): processIncoming
10-20 12:07:48.612: D/jdwp(14627): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x17, id=0x4000005B, flags=0x0, dataLen=0xC
10-20 12:07:48.613: D/jdwp(14627): processIncoming
10-20 12:07:48.613: D/jdwp(14627): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x4000005C, flags=0x0, dataLen=0x8
10-20 12:07:48.614: D/jdwp(14627): processIncoming
10-20 12:07:48.614: D/jdwp(14627): handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x4000005D, flags=0x0, dataLen=0x8
10-20 12:07:48.614: D/dalvikvm(14627): threadid=7: notify debugger
10-20 12:07:48.614: D/dalvikvm(14627): threadid=7 (FinalizerDaemon): calling run()
10-20 12:07:48.615: D/dalvikvm(14627): threadid=8: interp stack at 0x5d2a7000
10-20 12:07:48.615: D/dalvikvm(14627): threadid=8: created from interp
10-20 12:07:48.615: D/dalvikvm(14627): start new thread
10-20 12:07:48.620: D/dalvikvm(14627): threadid=8: notify debugger
10-20 12:07:48.620: D/dalvikvm(14627): threadid=8 (FinalizerWatchdogDaemon): calling run()
10-20 12:07:48.623: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.626: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.629: D/jdwp(14627): sendBufferedRequest : len=0x3D
10-20 12:07:48.635: D/dalvikvm(14627): threadid=9: interp stack at 0x5d6ad000
10-20 12:07:48.636: D/dalvikvm(14627): threadid=10: interp stack at 0x5d7b5000
10-20 12:07:48.637: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.637: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.638: W/Trace(489): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.638: W/Trace(489): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.638: V/ActivityManager(489): Binding process pid 14627 to record ProcessRecord{423159c8 14627:com.acs.sharp.app/u0a10099}
10-20 12:07:48.638: V/ActivityManager(489): New death recipient com.android.server.am.ActivityManagerService$AppDeathRecipient@42b99a70 for thread android.os.BinderProxy@42b341b8
10-20 12:07:48.639: V/ActivityManager(489): New app record ProcessRecord{423159c8 14627:com.acs.sharp.app/u0a10099} thread=android.os.BinderProxy@42b341b8 pid=14627
10-20 12:07:48.639: W/Trace(489): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.639: W/Trace(489): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.642: I/ActivityManager(489): No longer want com.mediatek.atci.service (pid 12317): empty for 1800s
10-20 12:07:48.643: W/Trace(489): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.644: W/Trace(489): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.647: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.650: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.650: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.650: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.650: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.651: D/jdwp(14627): sendBufferedRequest : len=0x3D
10-20 12:07:48.664: D/dalvikvm(14627): open_cached_dex_file : /data/app/com.acs.sharp.app-1.apk /data/dalvik-cache/data@app@com.acs.sharp.app-1.apk@classes.dex
10-20 12:07:48.670: I/Sharp:MainApp(14627): Application - OnCreate
10-20 12:07:48.670: I/Sharp:MainApp(14627): Starting the activity monitoring service...
10-20 12:07:48.671: I/Sharp:MainApp(14627): Completed starting the activity monitoring service.
10-20 12:07:48.672: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.672: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.674: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.674: W/Trace(14627): Unexpected value from nativeGetEnabledTags: 0
10-20 12:07:48.675: D/AndroidRuntime(14627): Shutting down VM
10-20 12:07:48.675: W/dalvikvm(14627): threadid=1: thread exiting with uncaught exception (group=0x413569a8)
10-20 12:07:48.676: E/AndroidRuntime(14627): FATAL EXCEPTION: main
10-20 12:07:48.676: E/AndroidRuntime(14627): java.lang.RuntimeException: Unable to start service com.acs.android.fwk.background.InactivityMonitorService@415fa958 with null: java.lang.NullPointerException
10-20 12:07:48.676: E/AndroidRuntime(14627):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2822)
10-20 12:07:48.676: E/AndroidRuntime(14627):    at android.app.ActivityThread.access$1900(ActivityThread.java:156)
10-20 12:07:48.676: E/AndroidRuntime(14627):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1437)
10-20 12:07:48.676: E/AndroidRuntime(14627):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 12:07:48.676: E/AndroidRuntime(14627):    at android.os.Looper.loop(Looper.java:153)
10-20 12:07:48.676: E/AndroidRuntime(14627):    at android.app.ActivityThread.main(ActivityThread.java:5297)
10-20 12:07:48.676: E/AndroidRuntime(14627):    at java.lang.reflect.Method.invokeNative(Native Method)
10-20 12:07:48.676: E/AndroidRuntime(14627):    at java.lang.reflect.Method.invoke(Method.java:511)
10-20 12:07:48.676: E/AndroidRuntime(14627):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
10-20 12:07:48.676: E/AndroidRuntime(14627):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
10-20 12:07:48.676: E/AndroidRuntime(14627):    at dalvik.system.NativeStart.main(Native Method)
10-20 12:07:48.676: E/AndroidRuntime(14627): Caused by: java.lang.NullPointerException
10-20 12:07:48.676: E/AndroidRuntime(14627):    at com.acs.android.fwk.background.InactivityMonitorService.onStartCommand(InactivityMonitorService.java:108)
10-20 12:07:48.676: E/AndroidRuntime(14627):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2805)
10-20 12:07:48.676: E/AndroidRuntime(14627):    ... 10 more
4

1 回答 1

1

看起来问题是由于我的设备上运行的高级任务杀手 - 计划在 30 秒内终止应用程序和服务。我将取消勾选我的应用程序并进行严格的测试以得出结论。

于 2013-10-20T09:02:33.343 回答