java读取文件只读一次,java – 为什么在循环中读取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=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

.....

.....

但是当我在4.1.2(三星(谷歌)Nexus S(soju,crespo),SDK 16,flash 485486,构建JZO54K)或2.3.6上运行服务时,我的服务停在日志中的一行之后.

在此设备上生成错误的日志:

--------- beginning of /dev/log/main

只打印一行,什么都没有….服务留在内存中,但不能正常工作……

这是我的代码

AndroidManifest

package="com.my.logcatt"

android:versionCode="1"

android:versionName="0.5">

活动

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”.

我怀疑你会发现这一行引发了一个你不能处理的异常(糟糕的做法顺便说一下 – 如果你有一个合适的捕获,你会发现我认为这个).

procLogcatAM = localRuntimeAM.exec( sLogcatAM);

否则,请检查Android版本并执行其他操作或检查是否允许拒绝.或者,将您的应用限制为< 4.1.

标签:android,java,logcat

来源: https://codeday.me/bug/20190901/1784548.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值