做项目时发现很多时候要用到服务+通知,今天我就简单的总结一下,方便他人也便利自己翻阅查看
启动型服务+通知
绑定型服务+自定义通知
一、首先写启动型服务+通知
写一个启动型服务,注意注册
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
getNotification();
return super.onStartCommand(intent, flags, startId);
}
public void getNotification() {
//得到NotificationManager的对象,用来实现发送Notification
NotificationManager manager = (NotificationManager) getSystemService(Service.NOTIFICATION_SERVICE);
//得到Notification对象
Notification notification = new Notification();
//设置消息来时显示的消息
notification.tickerText = "来消息了";
//设置消息来时显示图标
notification.icon = R.drawable.ic_launcher;
//设置是否会消失
notification.flags =Notification.FLAG_AUTO_CANCEL;
//设置消息来时震动
notification.defaults =Notification.DEFAULT_VIBRATE;
//设置当前时间
long when = System.currentTimeMillis();
notification.when = when;
//通知的跳转事件
Intent intent = new Intent(getApplication(), MainActivity.class);
/**Intent一般是用作Activity、Sercvice、BroadcastReceiver之间传递数据,
而Pendingintent,一般用在 Notification上,
可以理解为延迟执行的intent,PendingIntent是对Intent一个包装。*/
//参数:1、上下文 2、请求码 3、用于启动的intent 4、新开启的Activity的启动模式
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
//参数1、上下文 2、下拉通知栏 显示的标题 3、内容 4、PendingIntent对象
notification.setLatestEventInfo(this, "贾震", "外号叫大炮", pendingIntent);
//启动Notification
manager.notify(0, notification);
//取消通知
//manager.cancelAll();
}
}
注册:<service android:name="com.example.servicenotification.MyService"></service>
在Activity中:
public class MainActivity extends Activity implements OnClickListener {
Button btn1, btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
startService(new Intent(MainActivity.this, MyService.class));
break;
case R.id.button2:
stopService(new Intent(MainActivity.this, MyService.class));
break;
}
}
}
效果:
下拉:
二、绑定型服务 + 自定义通知
服务 --------注意注册:
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return new MyBinder();
}
//在Activity里回调
class MyBinder extends Binder {
public void Notification() {
//写在方法里,方便调用
showNotification();
}
}
@SuppressLint("SimpleDateFormat")
public void showNotification() {
Notification.Builder builder = new Notification.Builder(this).setTicker("新消息提醒")
.setSmallIcon(R.drawable.ic_launcher);
Notification notification = builder.build();
// 将布局文件转化成View,也就是自定义Notification
RemoteViews contentView = new RemoteViews(this.getPackageName(), R.layout.customer_notitfication_layout);
contentView.setTextViewText(R.id.title, "自定义标题");
contentView.setTextViewText(R.id.content, "自定义内容");
contentView.setTextViewText(R.id.time,
new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()));
contentView.setImageViewResource(R.id.icon, R.drawable.t12);
// 将xml布局转化的View加载到Notification中,达到展示自定义View的效果
notification.contentView = contentView;
// Notification加载点击事件
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
// Notification添加跳转效果
notification.contentIntent = contentIntent;
// 点击删除通知栏的通知
notification.defaults = Notification.FLAG_AUTO_CANCEL;
// 滑动删除通知栏的通知
notification.defaults = Notification.DEFAULT_ALL;
// 启动Notification的所有操作
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) this.getSystemService(ns);
mNotificationManager.notify(0, notification);
}
}
在Activity中:
public class MainActivity extends Activity implements OnClickListener{
Button btn1, btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
/*
* 1、要绑定的Service对象Intent
* 2、ServiceConnection,用于Activity和Service沟通的一个类 3、Service的绑定启动类型
* 0:绑定,必须借助于启动
* BIND_ABOVE_CLIENT:绑定无需借助启动,当需要绑定的时候,如果没有已启动Service则自己启动一个
*/
bindService(new Intent(this,MyService.class), sc, BIND_AUTO_CREATE);
break;
case R.id.button2:
unbindService(sc);
break;
}
}
//调用服务中的方法
MyBinder b;
ServiceConnection sc = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
b = (MyBinder) service;
b.Notification();
}
};
}
运行效果:
下拉时