首先放上效果图给大家看
在手机的上方弹出消息或提示
我这里使用一个简单的按钮来弹一个消息栏
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送通知"
android:onClick="send"
/>
在onCreate方法里获取通知管理器
//获取通知管理器
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
然后在java代码中写下点击事件
public void send(View view){
//首先实例消息通知
NotificationCompat.Builder builder=new NotificationCompat.Builder(this);
//设置标题
builder.setContentTitle("提示");
//设置内容
builder.setContentText("美女,暗恋你好久了");
//设置图标
builder.setSmallIcon(android.R.drawable.star_on);
//默认的闪光灯、铃声等等,一切默认为主
builder.setDefaults(NotificationCompat.DEFAULT_ALL);
Notification notification=builder.build();
//发送通知
notificationManager.notify(0x101,notification);
}
然后效果就可以出来了,安卓手机有些可能有权限的问题,我实在模拟器上实现的,我的手机是OPPOA37,没有出来消息框