自己写一个提示消息的框
xml文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送消息"
android:id="@+id/send"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="取消通知"
android:id="@+id/cancle"/>
</LinearLayout>
</RelativeLayout>
java的实现功能
public class AlertDialog extends AppCompatActivity implements View.OnClickListener{
private Button button1;
private Button button2;
NotificationManager notificationManager;//通知控制列
int notification_ID;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alertdalog_activity);
notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
button1=(Button)findViewById(R.id.send);
button2=(Button)findViewById(R.id.cancle);
button1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.send:
sendNotification();
break;
case R.id.cancle:
notificationManager.cancel(notification_ID);
break;
}
}
//发送通知
private void sendNotification(){
Notification.Builder builder=new Notification.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setTicker("Hello");//状态栏提示
builder.setWhen(System.currentTimeMillis());
builder.setContentText("通知栏通知");
builder.setContentText("我来自内蒙");
Intent i=new Intent(AlertDialog.this,MainActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,i,0);
builder.setContentIntent(pendingIntent);//点击后的意图
// builder.setDefaults(Notification.DEFAULT_SOUND);//设置声音
// builder.setDefaults(Notification.DEFAULT_LIGHTS);//设置指示灯
// builder.setDefaults(Notification.DEFAULT_VIBRATE);//设置震动
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification=builder.build();//4.1以上使用
//有这个则可以发送
notificationManager.notify(notification_ID,notification);
}