我在2.3.3到4.1的任何仿真器和实际设备上运行我的服务(写logcat到文件).一切都好.
在此设备上生成正确的日志:
--------- beginning of /dev/log/main--------- beginning of /dev/log/systemI/ActivityManager( 3386): START {act=androID.intent.action.MAIN cat=[androID.intent.category.HOME] flg=0x10000000 cmp=com.androID.launcher/com.androWAcivtMaae( 36:Ivldakgnme W/ActivityManager( 3386): Duplcaefns eus orcitRod45c6 o.vnotsolet.tiiyilgI/ActivityManager( 3386): displayed com.androID.calculator2/.Calculator: +9s374ms<br>..........
但是当我在4.1.2(三星(谷歌)Nexus S(soju,crespo),SDK 16,flash 485486,构建JZO54K)或2.3.6上运行服务时,我的服务停在日志中的一行之后.
在此设备上生成错误的日志:
--------- beginning of /dev/log/main
只打印一行,什么都没有….服务留在内存中,但不能正常工作……
这是我的代码
AndroIDManifest
<?xml version="1.0" enCoding="utf-8"?><manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.my.logcatt" androID:versionCode="1" androID:versionname="0.5"> <uses-sdk androID:minSdkVersion="10" /> <uses-permission androID:name="androID.permission.READ_EXTERNAL_STORAGE" /> <uses-permission androID:name="androID.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission androID:name="androID.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission androID:name="androID.permission.READ_LOGS" /> <application androID:theme="@androID:style/theme.NoTitlebar"> <activity androID:name=".ActivityMain" androID:label="logcatt"> <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver androID:name=".broadcastBoot" androID:enabled="true" androID:exported="false"> <intent-filter> <action androID:name="androID.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> <service androID:name=".ServiceMain" androID:enabled="true" /> </application></manifest>
活动
package com.my.logcatt;import androID.app.Activity;import androID.content.Intent;import androID.os.Bundle;public class ActivityMain extends Activity { @OverrIDe public voID onCreate( Bundle savedInstanceState) { super.onCreate( savedInstanceState); setContentVIEw( R.layout.main); try { startService( new Intent( getApplicationContext(), ServiceMain.class)); } catch( Exception e) {} } }
服务
package com.my.logcatt;import java.io.BufferedReader;import java.io.BuffereDWriter;import java.io.fileWriter;import java.io.inputStreamReader;import androID.app.Service;import androID.content.Intent;import androID.os.Environment;import androID.os.IBinder;public class ServiceMain extends Service { public static Process procLogcatClean = null; public static Process procLogcatAM = null; public static BufferedReader readerLogcat = null; public static voID fLog( String sLogMessage) { fileWriter fileLog = null; BuffereDWriter bufferLog = null; try { fileLog = new fileWriter( Environment.getExternalStorageDirectory().getabsolutePath() + "/123.log", true); if( fileLog != null) { bufferLog = new BuffereDWriter( fileLog); bufferLog.write( sLogMessage + "\r\n"); bufferLog.flush(); } } catch( Exception e) {} finally { if( bufferLog != null) { try { bufferLog.close(); } catch( Exception e) {} } if( fileLog != null) { try { fileLog.close(); } catch( Exception e) {} } } } @OverrIDe public voID onCreate() { super.onCreate(); startService(); } @OverrIDe public IBinder onBind(Intent intent) { return null; } @OverrIDe public voID onDestroy() { super.onDestroy(); } public voID startService() { final Thread thread = new Thread() { public voID run() { try { Runtime localRuntimeClear = Runtime.getRuntime(); String[] sLogcatClear = new String[ 2]; sLogcatClear[ 0] = "logcat"; sLogcatClear[ 1] = "-c"; procLogcatClean = localRuntimeClear.exec( sLogcatClear); procLogcatClean.waitFor(); Runtime localRuntimeAM = Runtime.getRuntime(); String[] sLogcatAM = new String[ 2]; sLogcatAM[ 0] = "logcat"; sLogcatAM[ 1] = "ActivityManager:I *:S"; procLogcatAM = localRuntimeAM.exec( sLogcatAM); readerLogcat = new BufferedReader( new inputStreamReader( procLogcatAM.getinputStream()), 1024); String str = ""; while( true) { str = ""; try { if( readerLogcat != null) { str = readerLogcat.readline(); fLog( str); } } catch( Exception e) {} if( str.compareto( "") == 0) continue; } } catch( Exception e) {} finally {} } }; thread.setPriority( Thread.MAX_PRIORITY); thread.start(); } }
怎么了?
解决方法:
在4.1.2.,只有root和系统应用程序才能访问logcat.如果是root,你可以使用“su logcat”.
https://groups.google.com/forum/?fromgroups=#!topic/android-developers/6U4A5irWang
我怀疑你会发现这一行引发了一个你不能处理的异常(糟糕的做法顺便说一下 – 如果你有一个合适的捕获,你会发现我认为这个).
procLogcatAM = localRuntimeAM.exec( sLogcatAM);
否则,请检查AndroID版本并执行其他 *** 作或检查是否允许拒绝.或者,将您的应用限制为< 4.1.
总结以上是内存溢出为你收集整理的java – 为什么在循环中读取logcat只读一次全部内容,希望文章能够帮你解决java – 为什么在循环中读取logcat只读一次所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)