-1

I am trying to develop a Beacon Application in Android. In that i want to get that how many times a beacon is getting scanned in one minute. I know this is possible through log. So how to get it? I am new to this and need very much so please help if anybody has answer!! Thanks in advance!

Harsh Mittal
  • 2,868
  • 1
  • 16
  • 21

2 Answers2

0

Tip: A good convention is to declare a TAG constant in your class:

private static final String TAG = "MyActivity";

Don't forget that when you make a call like

Log.v(TAG, "index=" + i);

int ASSERT  Priority constant for the println method.
int DEBUG   Priority constant for the println method; use Log.d.
int ERROR   Priority constant for the println method; use Log.e.
int INFO    Priority constant for the println method; use Log.i.
int VERBOSE Priority constant for the println method; use Log.v.
int WARN    Priority constant for the println method; use Log.w.
Nooruddin Lakhani
  • 7,507
  • 2
  • 19
  • 39
0

Read the log through the following code:

public class LogTest extends Activity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try {
      Process process = Runtime.getRuntime().exec("logcat -d");
      BufferedReader bufferedReader = new BufferedReader(
      new InputStreamReader(process.getInputStream()));

      StringBuilder log=new StringBuilder();
      String line;
      while ((line = bufferedReader.readLine()) != null) {
        log.append(line);
      }
      TextView tv = (TextView)findViewById(R.id.textView1);
      tv.setText(log.toString());
    } catch (IOException e) {
    }
  }
}

The above code reads the log and displays it in a textview.

You should define:

<uses-permission android:name="android.permission.READ_LOGS" />

permission in your manifest.

To copy the log directly to a file,use the following method:

public static void copyLogcatToFile(Context context) {    
    String fileName = "log_cat"+System.currentTimeMillis()+".txt";
    File outputFile = new File(context.getExternalCacheDir(),fileName);
    @SuppressWarnings("unused")
    Process process = Runtime.getRuntime().exec("logcat -f "+outputFile.getAbsolutePath());
}
kgandroid
  • 5,507
  • 5
  • 39
  • 69