notifycation有两种使用情况,第一种是只发消息,第二种是自定义布局显示,并且还可以对交互事件进行相应,类似于很多音乐播放器界面退出之后在通知栏显示简单播放条,可以控制。
先发自定的布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/basketball"/>
<TextView
android:id="@+id/txt_notify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:textSize="20sp" />
</LinearLayout>
然后是service:
public class MyService extends Service {
public MyService() {
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
String info = intent.getExtras().getString("info");
Log.i("notifycation 服务", info);
return super.onStartCommand(intent, flags, startId);
}
}
主程序代码:
/**
* notifycation的简单实现
*/
public class MainActivity extends AppCompatActivity {
private static final int NOTIFYCATION_FLAG=1;
@Bind(R.id.btn_notify)
Button btnNotify;
@Bind(R.id.btn_notify_custom)
Button btnNotifyCustom;
private NotificationManager manager;//notifycation管理器
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
initView();
}
/**
* 初始化
*/
private void initView() {
manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
@OnClick({R.id.btn_notify,R.id.btn_notify_custom})
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_notify:
sendNotify();
break;
case R.id.btn_notify_custom:
sendNotifyCustom();
break;
default:
break;
}
}
/**
* 发送自定义的notify
* 包括布局文件和监听事件
*/
private void sendNotifyCustom() {
Intent intent=new Intent(this,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
Intent svcIntent=new Intent(this,MyService.class);
Bundle bundle=new Bundle();
bundle.putString("info","notify点击事件传递给service的参数");
svcIntent.putExtras(bundle);
//这里可以用服务,可以用广播,也可以用activity进行跳转
PendingIntent serviceIntent=PendingIntent.getService(this,0,svcIntent,0);
RemoteViews remoteViews=new RemoteViews(getPackageName(),R.layout.layout_notify_info);
remoteViews.setTextViewText(R.id.txt_notify,"这是控件显示的notify消息");
remoteViews.setOnClickPendingIntent(R.id.img_icon,serviceIntent);
Notification notification=new Notification.Builder(this)
.setSmallIcon(R.drawable.basketball)
.setTicker("你有新消息了")
.setWhen(System.currentTimeMillis())
.setContentIntent(pendingIntent)
.setContent(remoteViews)
.setNumber(1)
.build();
notification.flags|=Notification.FLAG_AUTO_CANCEL;
manager.notify(NOTIFYCATION_FLAG,notification);
}
/**
* 发送简单notify
*/
private void sendNotify() {
Intent intent=new Intent(this,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,0);
Notification notification=new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setTicker("你有新消息了")
.setContentTitle("notify Title")
.setContentText("这是notify的内容")
.setContentIntent(pendingIntent)
.setNumber(1)
.build();
notification.flags|=Notification.FLAG_AUTO_CANCEL;
manager.notify(NOTIFYCATION_FLAG,notification);
}
}
运行效果:
简单通知:
自定义通知:
点击图片之后的反应,调用服务打印日志:
原设想做音乐播放器关闭界面之后让她继续播放并可以控制的技术关算是通过了。下来该尝试一下了。