Activity与Service交互方式
(1)绑定服务方式(一般情况下不太推荐使用)
(2)使用BroadCast Receiver的方式进行方法调用或者数据传输是实现解耦的最好方法,通过代码的方式在Service,Activity中注册对应的广播接收者,要调用方法,只要发送一个对应的广播就可以了,即使退出也不会产生内存泄露的危险。那么数据传输使用什么呢?如果要传输对象,那么使用持久化技术Parcelable,Serializable,在Intent中有对应put,set方法,只有实体实现了Parcelable或Serializable接口,那么传输数据信息就变得非常简单了。
(3)Service的子类IntentService是一个非常好用的抽象类(3.0以后),自定义的IntentService的子类必须有一个默认不带参数的构造函数和实现protected void onHandleIntent(Intent intent),参数intent中存放了传递过来的参数,并且该方法会在一个线程中执行。
(4) Service中的属性实例化最好不要放置在public int onStartCommand(Intent intent, int flags, int startId)中,调用多次startService(),那么该方法也会被调用多次,不然会导致Service中的属性被创建多次,例如播放音频的MediaPlayer就不能放置在该方法中。可将保持单例或者属性值不变的属性初始化放置在构造函数中。
(5)使Service变为前台服务,可调用startForeground(Construct.NOTIFICATION_ID, notification);第一个参数是通知的唯一标识,如果后续要修改通知的显示内容,可以使用通知一个标识。
例如:
private void notification(String ticker,String song,String singer){
RemoteViews vi=new RemoteViews(this.getPackageName(),R.layout.notification_use);
Intent myIntent=new Intent(context,MainActivity.class);
PendingIntent contentIntent=PendingIntent.getActivity(context, 1, myIntent, 1);
Notification.Builder builder=new Builder(context);
builder.setSmallIcon(R.drawable.smiley_23)
.setTicker(ticker==null?"哆哆音乐":ticker)
.setContentText("哆哆音乐")
.setContentTitle("我的音乐我做主")
.setAutoCancel(false)
.setOngoing(true)
.setContentIntent(contentIntent)
.setContent(vi);//自定义通知栏的显示内容
Intent intent_current=new Intent();
intent_current.setAction(MainActivity.BROAD_ACTION);
//表示该Intent的类型
intent_current.putExtra("type", PlayService.CURRENT);
Intent intent_last=new Intent();
intent_last.setAction(MainActivity.BROAD_ACTION);
intent_last.putExtra("type", PlayService.LAST);
Intent intent_next=new Intent();
intent_next.setAction(MainActivity.BROAD_ACTION);
intent_next.putExtra("type", PlayService.NEXT);
PendingIntent current=PendingIntent.getBroadcast(context, 0, intent_current, 0);
PendingIntent last=PendingIntent.getBroadcast(context, 1, intent_last, 0);
PendingIntent next=PendingIntent.getBroadcast(context, 2, intent_next, 0);
Notification notification=builder.getNotification();
//给通知栏中的组件添加事件。第一个参数是通知栏中的组件Id,第二个参数为延期意图,接收广播的广播接收者为Service中的内部类.
//这样就是可以实现调用Service中的方法。
notification.contentView.setOnClickPendingIntent(R.id.notification_current_to_last, last);
notification.contentView.setOnClickPendingIntent(R.id.notification_set_current_music, current);
notification.contentView.setOnClickPendingIntent(R.id.notification_current_to_next, next);
if(song!=null){
notification.contentView.setTextViewText(R.id.notification_current_music, song);
}else{
notification.contentView.setTextViewText(R.id.notification_current_music, "未知");
}
if(singer!=null){
notification.contentView.setTextViewText(R.id.notification_current_music_author, singer);
}else{
notification.contentView.setTextViewText(R.id.notification_current_music_author, "未知");
}
NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(MainActivity.Notication_flag,notification);
//设置为前台进程,
startForeground(Construct.NOTIFICATION_ID, notification);
}