循环中怎样让Looper.loop();后面程序运行
package fund123.com.client3;import fund123.com.db.DownDatasTask;
import fund123.com.db.OnDownDatasListener;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.os.Message;
import android.provider.Settings.Secure;
import android.util.Log;
public class MessageService extends Service {
private static final String ITEM_MESSAGE = "message";
private static final String ITEM_TITLE = "title";
private static final String ITEM_TIME = "addtime";
//获取消息线程
private Thread mThread = null;
//点击查看
//private Intent msgIntent = null;
private PendingIntent msgPendingIntent = null;
//通知栏消息
private int msgNotificationid = 1000;
private Notification msgNotification = null;
private NotificationManager msgNotificatiomanager = null;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId) {
//初始化
msgNotification = new Notification();
msgNotification.icon = R.drawable.icon1;
msgNotification.tickerText = "新消息";
msgNotification.defaults = Notification.DEFAULT_SOUND;
msgNotification.flags = Notification.FLAG_AUTO_CANCEL;
msgNotificatiomanager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//开启线程
MsgThread thread=new MsgThread();
mThread=new Thread(thread);
mThread.start();
}
// @Override
// public void onDestroy() {
//// System.exit(0);
// super.onDestroy();
// }
class MsgThread implements Runnable{
public boolean isrunning = true;
public void run() {
while(isrunning){
try {
//休息1分钟
Thread.sleep(60000);
//获取服务器消息
Looper.prepare();
Log.v("测试1", "测试1");
loadPushMessage();
Looper.loop();
Log.v("测试2", "测试2");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
private void loadPushMessage(){
String mobileid = Secure.getString(getBaseContext().getContentResolver(), Secure.ANDROID_ID);
String string_url = Client3Application.url_getData + getResources().getString(R.string.url_push_msg, mobileid);
final DownDatasTask down_datas_task_ = new DownDatasTask();
down_datas_task_.execute(string_url);
down_datas_task_.setOnDownDatasListener(new OnDownDatasListener() {
@Override
public void onDownData() {
int count = down_datas_task_.getDataCount();
if (count > 0) {
Message msg=new Message();
msg.what=1;
String string_msg = down_datas_task_.getData(0, ITEM_MESSAGE).toString();
String string_title = down_datas_task_.getData(0, ITEM_TITLE).toString();
String string_time = down_datas_task_.getData(0, ITEM_TIME).toString();
Bundle data = new Bundle();
data.putString("message", string_msg);
data.putString("time", string_time);
data.putString("title", string_title);
msg.setData(data);
mHandler.sendMessage(msg);
Log.v("测试3", "测试3");
}
down_datas_task_.cancel(true);
down_datas_task_.clearData();
}
});
}
private Handler mHandler=new Handler(){
public void handleMessage(Message msg) {
int i = msg.what;
if(i > 0){
Bundle data = msg.getData();
String string_msg = data.getString("message");
String string_time = data.getString("time");
String string_title = data.getString("title");
Bundle bundle = new Bundle();
Intent msgIntent = new Intent();
bundle.putString("message", string_msg);
bundle.putString("time", string_time);
bundle.putString("title", string_title);
msgIntent.putExtras(bundle);
msgIntent.setClass(MessageService.this, MessageActivity.class);
msgPendingIntent = PendingIntent.getActivity(MessageService.this,0,msgIntent,0);
//更新通知栏
msgNotification.setLatestEventInfo(MessageService.this,"新消息",string_title,msgPendingIntent);
msgNotificatiomanager.notify(msgNotificationid, msgNotification);
//每次通知完,通知id递增一下,避免消息覆盖掉
msgNotificationid++;
}
}
};
}
--------------------编程问答-------------------- Looper.loop()之后的代码是无法运行的,因为loop()里面是个死循环,有消息就处理,没消息就挂起休眠。 --------------------编程问答--------------------
那这个问题有办法解决嘛?我循环的程序就不能跑了啊,然后取到的消息有时还是前几条的内容 --------------------编程问答-------------------- 应该在Looper.loop()之前创建Handler子类的对象, 之后使用handler.sendEmptyMessage()来往这个线程发消息,让这个线程处于工作态。
而消息处理在Handler子类的方法handlerMessage(Message msg)里面完成,你需要覆盖这个方法。 --------------------编程问答-------------------- 调用Looper.quite(), 或者通过handler throw new RuntimeException()就行了.
补充:移动开发 , Android