如何在关闭屏幕时保持CPU运行?

时间:2015-11-28 01:36:57

标签: android cpu android-powermanager

Fragment中有一个耗时的任务(线程)。它工作正常。但是,当我关闭屏幕时,我发现CPU无法工作,因此任务无法正常工作。

我在Activity中使用import sys import re import datetime import time startTime = time.time() cnt = 0 cnt2 = 0 failCnt = 0 reCompile = re.compile("\s([^\s]*?)=\"(.*?)\"") delimiterCharacterOut = "," def writeCSVLine(line): x = reCompile.findall(line) a = dict((row[0], row[1]) for row in x) try: a['ts1'] = str(int(int(a['ts'])/1000)) x = str(datetime.datetime.fromtimestamp(float(a['ts1'])))[0:19] b = a['ts'] + ",\"" + x + "\"," + a['t'] + "," + a['lt'] + ",\"" + a['s'] + "\",\"" + a['lb'] + "\"," + a['rc'] + ",\"" + a['rm'] + "\",\"" + a['tn'] + "\",\"" + a['dt'] + "\"," + a['by'] + ",\"" + a['sc'] + "\"," + a['ec'] + ",\"" + a['ng'] + "\"," + a['na'] + ",\"" + a['hn'] + "\"," + a['in'] + "\n" except: return -1 o.write(b) return 1 print "Splitting JTL file" try: runArgv = sys.argv #Save the command line jtlInfile = str(sys.argv[1]) #Name of JTL input file cvsOutfile = str(sys.argv[2]) # Name of CVS output file reFilter = str(sys.argv[3]) # Filter the labels (lb) for the filter except: print "Error: Input format: <input file> <output file> <Filter by regular expression>" raise try: f = open(jtlInfile, "r") o = open(cvsOutfile, "w") except: raise print "Filtering on regular expression : " + reFilter cmpFilter = re.compile(reFilter) # o.write("timestamp" + ",\""+ "datetime" + "\n") o.write("timeStamp" + ",\"" + "datetime" + "\"," + "elapsed" + "," + "Latency" + ",\"" + "success" + "\",\"" + "label" + "\"," + "responseCode" + ",\"" + "responseMessage" + "\",\"" + "threadName"+ "\",\"" + "dataType" + "\"," + "bytes" + ",\"" + "SampleCount" + "\"," + "ErrorCount" + ",\"" + "grpThreads" + "\"," + "allThreads" + ",\"" + "Hostname" + "\"," + "IdleTime" + "\n") for line in f: try: if cmpFilter.search(line): returnVal = writeCSVLine(line) if returnVal<0: failCnt += 1 else: cnt2 += 1 except: print 'Error in line : ', cnt, line raise cnt += 1 endTime = time.time() print "Time taken : ", str(endTime-startTime) print "Lines processed : ", cnt print "Lines that passed the filter : ", cnt2 print "Lines skipped (error?) : ", failCnt f.close() o.close() ,但也没有使用片段。 同时添加PowerManager

<uses-permission android:name="android.permission.WAKE_LOCK" />

我看过CPU了 enter image description here

1 个答案:

答案 0 :(得分:1)

通过服务运行您的代码,如果您在活动中运行它,它将在活动进入后台时停止。改为使用服务。

http://developer.android.com/training/run-background-service/create-service.html http://developer.android.com/guide/components/services.html