Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

76
LINES

< > BotCompany Repo | #1000406 // Android redirecting System.out and System.err to screen test

JavaX source code (Android) - run with: the app

1  
import android.widget.*;
2  
import android.view.*;
3  
import android.content.Context;
4  
import android.app.Activity;
5  
import java.util.Timer;
6  
import java.util.TimerTask;
7  
import java.io.*;
8  
9  
public class main {
10  
  static class Lg {
11  
    Activity context;
12  
    ScrollView sv;
13  
    TextView tv;
14  
    StringBuilder buf = new StringBuilder();
15  
    
16  
    Lg(Activity context) {
17  
      this.context = context;
18  
      sv = new ScrollView(context);
19  
      tv = new TextView(context);
20  
      tv.setText(buf.toString());
21  
      sv.addView(tv);
22  
    }
23  
    
24  
    View getView() {
25  
      return sv;
26  
    }
27  
    
28  
    void print(final String s) {
29  
      context.runOnUiThread(new Runnable() {
30  
        public void run() {
31  
          buf.append(s);
32  
          tv.setText(buf.toString());
33  
        }
34  
      });
35  
    }
36  
    
37  
    void println(String s) {
38  
      print(s + "\n");
39  
    }
40  
  }
41  
  
42  
  static Lg lg;
43  
  
44  
  public static View main(final Activity context) {
45  
    lg = new Lg(context);
46  
    
47  
    OutputStream outputStream = new OutputStream() {
48  
      public void write(int b) {
49  
        try {
50  
          lg.print(new String(new byte[] {(byte) b}, "UTF-8")); // This is crap
51  
        } catch (UnsupportedEncodingException e) {}
52  
      }
53  
      
54  
      @Override
55  
      public void write(byte[] b, int off, int len) {
56  
        try {
57  
          lg.print(new String(b, off, len, "UTF-8")); // This is crap
58  
        } catch (UnsupportedEncodingException e) {}
59  
      }
60  
    };
61  
    
62  
    PrintStream ps = new PrintStream(outputStream, true);
63  
    System.setOut(ps);
64  
    System.setErr(ps);
65  
    
66  
    Timer myTimer = new Timer();
67  
    myTimer.schedule(new TimerTask() {          
68  
      public void run() {
69  
        System.out.println("More text");
70  
        System.err.println("Also on System.err!");
71  
      }
72  
    }, 0, 1000);
73  
    
74  
    return lg.getView();
75  
  }
76  
}

Author comment

Began life as a copy of #1000405

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

Comments [hide]

ID Author/Program Comment Date
84 stefan Wow. It works... 2015-08-03 03:15:50

add comment

Snippet ID: #1000406
Snippet name: Android redirecting System.out and System.err to screen test
Eternal ID of this version: #1000406/1
Text MD5: 5257a24a22a7b6c5c143d578c386776f
Author: stefan
Category: javax android
Type: JavaX source code (Android)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-08-03 03:17:37
Source code size: 1857 bytes / 76 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 745 / 645
Referenced in: [show references]