使用AlertDialog实现提示框

image.png

自己写一个提示消息的框

xml文件


    
        
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);
    }

你可能感兴趣的:(使用AlertDialog实现提示框)