1 消息处理
1.1 概念介绍
MessageQueue:消息队列
存放消息的地方,按照FIFO的规则执行,每一个线程只可以拥有一个MessageQueue。
在创建Looper对象时会创建一个MessageQueue对象。
Message:消息对象
MessageQueue中存放的对象。一个MessageQueue中可以包含多个Message对象。
可以通过Message.obtain()或者Handler.obtainMessage()获取Message对象。
但是这并不一定是直接创建一个新的实例,而是先从消息池中看有没有可用的Message实例,存在则直接取出返回这个实例。
如果消息池中没有可用的Message实例,则才用给定的参数创建一个Message对象。
调用removeMessages()时,将Message从MessageQueue中删除,同时放入到消息池中。
Looper:
操作MessageQueue。一个Looper对应一个MessageQueue。
通过调用Looper.myLooper()可以获得当前线程的Looper对象。
Looper从MessageQueue中取出Message,然后,交由Handler的handleMessage()进行处理。
处理完成后,调用Message.recycle()将其放入消息池中。
Handler:消息处理者
handler负责将需要传递的信息封装成Message对象,然后调用sendMessage()方法将消息放入MessageQueue中。
当MessageQueue循环到该Message,调用相应的handler对象的handleMessage()方法对其进行处理。
Handler都可以共享同一个Looper和MessageQueue。
1.2 示例
MessageService.java
public class MessageService extends Service {
private static final int COOLSZY = 1;
private static final String TAG = "MessageService";
private static final int KUKA = 2;
private ServiceHandler handler;
private class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
// TODO Auto-generated constructor stub
}
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case COOLSZY:
Log.i(TAG, "handlerMessage--->"+msg.obj);
break;
case KUKA:
Log.i(TAG, "handlerMessage--->"+msg.obj);
break;
default:
break;
}
stopSelf(msg.arg1);
}
}
@Override
public void onCreate() {
Log.i(TAG, "MessageService--->onCreate()");
Looper looper = Looper.getMainLooper();
handler = new ServiceHandler(looper);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "MessageService--->onStartCommand()");
Message msg = handler.obtainMessage();
msg.what = KUKA;
msg.arg1 = startId;
msg.obj = "kuka";
handler.sendMessage(msg);
return START_STICKY;
}
@Override
public void onDestroy() {
Log.i(TAG, "MessageService--->onDestroy()");
super.onDestroy();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
}
manifest.xml
<service android:name="com.example.messagehandler.MessageService"></service>
Activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = ((Button)findViewById(R.id.startBtn));
btn.setOnClickListener(listener);
}
private OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(getApplicationContext(), MessageService.class);
startService(intent);
}
};
}
2 StatusBarNotification
status bar notification添加一个icon到系统的通知栏中(可选择是否有文本信息),添加一段信息到“Notifications”窗口中。
当在后台运行的服务需要与用户进行交互时,我们可以使用status bar notification。
实现步骤:
创建Notification
创建NotificationManager
StatusService:
public class StatusService extends IntentService {
private static final String TAG = "StatusService";
public StatusService() {
super("StatusService");
// TODO Auto-generated constructor stub
}
@Override
protected void onHandleIntent(Intent arg0) {
Log.i(TAG, "开始下载.....");
showNotification(false);
try {
Thread.sleep(5000);
showNotification(true);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.i(TAG, "程序下载完毕");
}
private void showNotification(boolean finish) {
Notification notification = null;
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
if(!finish) {
notification = new Notification(R.drawable.ic_launcher, "开始下载...", System.currentTimeMillis());
notification.setLatestEventInfo(this, "下载", "正在下载中", contentIntent);
} else {
notification = new Notification(R.drawable.ic_launcher, "下载完成", System.currentTimeMillis());
notification.setLatestEventInfo(this, "下载", "所有任务下载完毕", contentIntent);
}
notification.defaults = Notification.DEFAULT_ALL; //设置声音,灯光,震动提醒
// notification.sound = Notification.DEFAULT_SOUND;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(R.layout.activity_main, notification);
}
}
Manifest.xml
<service android:name="com.example.statusbarnotification.StatusService"></service>
Activity:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStartService = (Button) findViewById(R.id.startBtn);
btnStartService.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, StatusService.class);
startService(intent);
}
});
}
@Override
protected void onStart() {
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.cancel(R.layout.activity_main); // 通过点击通知进入后,StatusBarNotification取消
super.onStart();
}
}