您的位置:首页 > 其它

GeekBand--第九周分享

2016-05-01 09:55 232 查看

多进程

只要执行android:process就可以创建一个进程!

进程还有等级!

* 前台进程

* 可见进程

* 服务进程

* 后台进程

* 空进程

什么情况下使用多进程呢?

就是需要使用多内存的时候,开辟多个进程扩大内存的使用!防止程序卡顿

进程间的内存不可见!

多进程的通信PIC

如何通信?

系统实现

Messenger 利用Handler(尽量使用Messenger)

AIDL

Messenge举例

public class MessengerService extends Service {
Messenger messenger = new Messenger(new  IncomingHandler());
class IncomingHandler extends Handler{
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//处理消息
switch (msg.what){
case 0:
break;
}
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {

return messenger.getBinder();
}
}

public class MessengerActivity extends Activity {
private Messenger mMessenger;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
mMessenger = new Messenger(service);
}

@Override
public void onServiceDisconnected(ComponentName name) {

}
};
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
bindService(new Intent(this,MessengerService.class),mServiceConnection, Context.BIND_AUTO_CREATE);
if(mMessenger != null){
Message message = Message.obtain(null,0,"hello");
try {
mMessenger.send(message);
} catch (RemoteException e) {
e.printStackTrace();
}

}
}
}


按照以上写法就可以用Messenger进行进程间的通信了!

服务与通知

Service小结

按种类:

Local

remote

按类型:

前台

后台

启动方式

服务的生命周期

oncreate

onstart

onbind

ondestory

Notification

什么是notification

Notification,俗称通知,是一种具有全局效果的通知,它展示在屏幕的顶端,首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容。

如何创建notification

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, MusicButtonActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("My notification")
.setContentText("Hello World!");

mBuilder.setContentIntent(pendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(1, mBuilder.build())


在使用NotificationManager.notify()发送通知的时候,需要传递一个标识符,用于唯一标识这个通知。对于有些场景,并不是无限的添加新的通知,有时候需要更新原有通知的信息,这个时候可以重写构建Notification,而使用与之前通知相同标识符来发送通知,这个时候旧的通知就被被新的通知所取代,起到更新通知的效果。

  对于一个通知,当展示在状态栏之后,但是使用过后,如何取消呢?Android为我们提供两种方式移除通知,一种是Notification自己维护,使用setAutoCancel()方法设置是否维护,传递一个boolean类型的数据。另外一种方式使用NotificationManager通知管理器对象来维护,它通过notify()发送通知的时候,指定的通知标识Id来操作通知,可以使用cancel(int)来移除一个指定的通知,也可以使用cancelAll()移除所有的通知。

  使用NotificationManager移除指定通知示例:

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(0);


PendingIntent

 PendingIntent提供了多个静态的getXxx()方法,用于获得适用于不同场景的PendingIntent对象。一般需要传递的几个参数都很常规,只介绍一个flag参数,用于标识PendingIntent的构造选择:


FLAG_CANCEL_CURRENT:如果构建的PendingIntent已经存在,则取消前一个,重新构建一个。

FLAG_NO_CREATE:如果前一个PendingIntent已经不存在了,将不再构建它。

FLAG_ONE_SHOT:表明这里构建的PendingIntent只能使用一次。

FLAG_UPDATE_CURRENT:如果构建的PendingIntent已经存在,则替换它,常用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: