Thread.start() not running run()?
I would expect the following code to print "Listening..." to the LogCat
continuously and infinitely. Unfortunately, I see nothing happening
(nothing is printed in LogCat). Does anyone see what I am doing wrong?
package com.example.threaddemo;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity
{
Thread thread = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thread = new Thread(new Runnable()
{
public void run()
{
try
{
while (true)
{
Log.i("Listen()", "Listening...");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
thread.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
No comments:
Post a Comment