Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy read logcat in loop read only one time
    primarykey
    data
    text
    <p>I run my service (write logcat to file) on any emulators and real devices from 2.3.3 to 4.1. All OK. On this devices generated right log:</p> <pre><code>--------- 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&lt;br&gt; ..... ..... </code></pre> <p>But when I run service on 4.1.2 (samsung (google) Nexus S (soju, crespo), SDK 16, flash 485486, build JZO54K) or 2.3.6, my service stoped after <b>one line</b> in logs.</p> <p>On this devices generated wrong log:</p> <pre><code>--------- beginning of /dev/log/main </code></pre> <p>Only print one line and nothing.... Service stay in memory, but not work right...</p> <p>This is my code</p> <p><b>AndroidManifest</b></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.logcatt" android:versionCode="1" android:versionName="0.5"&gt; &lt;uses-sdk android:minSdkVersion="10" /&gt; &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt; &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; &lt;uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /&gt; &lt;uses-permission android:name="android.permission.READ_LOGS" /&gt; &lt;application android:theme="@android:style/Theme.NoTitleBar"&gt; &lt;activity android:name=".ActivityMain" android:label="logcatt"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;receiver android:name=".BroadcastBoot" android:enabled="true" android:exported="false"&gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.BOOT_COMPLETED" /&gt; &lt;/intent-filter&gt; &lt;/receiver&gt; &lt;service android:name=".ServiceMain" android:enabled="true" /&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p><b>Activity</b></p> <pre><code>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) {} } } </code></pre> <p><b>Service</b></p> <pre><code>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(); } } </code></pre> <p>What is wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload