我正在努力
Run this Code Example我用自己的代码替换了readWebPage()
方法。
我正在尝试在服务中实现CHAT HEADS。
Log Cat
03-10 00:22:00.309: E/AndroidRuntime(9302): FATAL EXCEPTION: Thread-596
03-10 00:22:00.309: E/AndroidRuntime(9302): Process: com.example.soc, PID: 9302
03-10 00:22:00.309: E/AndroidRuntime(9302): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-10 00:22:00.309: E/AndroidRuntime(9302): at android.os.Handler.<init>(Handler.java:200)
03-10 00:22:00.309: E/AndroidRuntime(9302): at android.os.Handler.<init>(Handler.java:114)
03-10 00:22:00.309: E/AndroidRuntime(9302): at android.view.ViewRootImpl$ViewRootHandler.<init>(ViewRootImpl.java:2968)
03-10 00:22:00.309: E/AndroidRuntime(9302): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:3248)
03-10 00:22:00.309: E/AndroidRuntime(9302): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
03-10 00:22:00.309: E/AndroidRuntime(9302): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
03-10 00:22:00.309: E/AndroidRuntime(9302): at com.example.soc.msgService.readWebPage(msgService.java:98)
03-10 00:22:00.309: E/AndroidRuntime(9302): at com.example.soc.msgService$MyThread.run(msgService.java:108)
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(MainActivity.this, msgService.class));
}
msgService.java
package com.example.soc;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import com.example.soc.util.ImageLoadTask;
import com.squareup.picasso.Picasso;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.IBinder;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.ImageView;
public class msgService extends Service{
private static String TAG = msgService.class.getSimpleName();
private MyThread mythread;
public boolean isRunning = false;
private WindowManager windowManager;
private ImageView chatHead;
String url = "someimageurl.jpg";
@Override
public IBinder onBind(Intent arg0) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
mythread = new MyThread();
}
@Override
public synchronized void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy");
if(!isRunning){
mythread.interrupt();
mythread.stop();
}
}
@Override
public synchronized void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.d(TAG, "onStart");
if(!isRunning){
mythread.start();
isRunning = true;
}
}
public void readWebPage(){
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
chatHead = new ImageView(this);
Picasso.with(this).load(url).into(chatHead);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 0;
params.y = 100;
windowManager.addView(chatHead, params);
}
class MyThread extends Thread{
static final long DELAY = 3000;
@Override
public void run(){
while(isRunning){
Log.d(TAG,"Running");
try {
readWebPage();
Thread.sleep(DELAY);
} catch (InterruptedException e) {
isRunning = false;
e.printStackTrace();
}
}
}
}
}