Java 为什么在循环中只读取一次logcat
我在2.3.3到4.1的任何模拟器和真实设备上运行我的服务(将logcat写入文件)。好的。 在此设备上生成了正确的日志:Java 为什么在循环中只读取一次logcat,java,android,logcat,Java,Android,Logcat,我在2.3.3到4.1的任何模拟器和真实设备上运行我的服务(将logcat写入文件)。好的。 在此设备上生成了正确的日志: --------- beginning of /dev/log/main --------- beginning of /dev/log/system I/ActivityManager( 3386): START {act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x1000000
--------- beginning of /dev/log/main
--------- beginning of /dev/log/system
I/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.tiiyilg
I/ActivityManager( 3386): Displayed com.android.calculator2/.Calculator: +9s374ms<br>
.....
.....
只打印一行,不打印任何内容。。。。服务保留在内存中,但无法正常工作
这是我的密码
雄激素单
服务
有什么问题吗?在4.1.2中,只有根应用程序和系统应用程序才能访问logcat。如果是根,可以使用“su logcat” 我怀疑您会发现这一行抛出了一个您无法处理的异常(顺便说一句,糟糕的做法-如果您有一个正确的捕获,我想您会发现这一点)
否则,请检查Android版本并执行其他操作,或者检查权限拒绝。或者,将你的应用限制在<4.1.Simon,谢谢你的回答。在real app中,我在异常路径中添加了输出日志。在这段代码中,不要在
procLogcatAM=localRuntimeAM.exec(sLogcatAM)上输入异常代码>。此外,该代码在2.3.6上也不起作用…Simon,sanks!你说得对,没有“su”的代码不工作,但不会生成异常(imho很奇怪)。使用此代码Runtime localRuntimeAM=Runtime.getRuntime();procLogcatAM=localRuntimeAM.exec(“su”);DataOutputStream localDataOutputStream=新的DataOutputStream(procLogcatAM.getOutputStream());localDataOutputStream.writeBytes(“logcat ActivityManager:I*:S\n”);localDataOutputStream.flush();readerLogcat=new BufferedReader(new InputStreamReader(procLogcatAM.getInputStream()),1024)代码>。
--------- beginning of /dev/log/main
<?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();
}
}
procLogcatAM = localRuntimeAM.exec( sLogcatAM);