当点击activity_main(button1)上的按钮时,我想测试ChatClientService。
在onStartCommand方法中,我想运行一个使用innerclass作为runnable的线程。这是错误的:意图工作(日志显示aaaaa),它进入run方法(日志显示aaaaa),但我没有设法进入内部类。有任何想法吗? (对不起日志)
ChatClientService.java
public class ChatClientService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
//TODO do something useful
new Thread( new Runnable() {
@Override
public void run() {
Log.i("aaaaaaaaaaaaaaaaaaaa","aaaaaaaaaaaaaaaaaaaa");
}
}).start();
return Service.START_NOT_STICKY;
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public class ChatClient implements Runnable {
private BufferedWriter bw;
private BufferedReader br;
private boolean running;
public void halt() {
Log.i("DDDDDDDDDDDDDDDDDD","DDDDDDDDDDDDDDDDDD");
running = true;
try {
bw.close();
bw = null;
br.close();
br = null;
} catch (IOException e) {
Log.i("EEEEEEEEEEEEEEEEE","EEEEEEEEEEEEEEEEEEE");
}
}
@Override
public void run() {
Log.i("HHHHHHHHHHHHHHH", "HHHHHHHHHHHHHHH");
try {
running = true;
Socket s = new Socket("<your pc ip>", 9999);
this.br = new BufferedReader(new InputStreamReader(s.getInputStream()));
this.bw = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
String in = null;
while(running && (in = br.readLine()) != null) {
Log.i("CHATCLIENT RECV ", in);
}
} catch (UnknownHostException e) {
e.printStackTrace();
Log.i("FFFFFFFFFFFFFFFF","FFFFFFFFFFFFFFF");
} catch (IOException e) {
e.printStackTrace();
Log.i("GGGGGGGGGGGGG","GGGGGGGGGGG");
}
}
}
}
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@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;
}
public void TestChat(View v){
Log.i("cccccccccccccccccccccccccc","cccccccccccccccccccccc");
startService(new Intent(getApplication(), ChatClientService.class));
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="be.howest.mad.lab24.oef1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="18"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="be.howest.mad.lab24.oef1.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name="be.howest.mad.lab24.oef1.ChatClientService"
android:enabled="true"
android:exported="false" >
</service>
</application>
</manifest>
提前致谢!
答案 0 :(得分:1)
试试这个......
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
new Thread(new ChatClient()).start();
return START_NOT_STICKY;
}