Android发送通知和自定义Dialog

btn1:发送一个通知

btn3:自定义对话框

public class ActivityNotifi extends AppCompatActivity implements View.OnClickListener {
    Button btn1, btn3;
    String CHANNEL_ID = "ChannelID";
    int notificationId = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notifi);
        btn1 = findViewById(R.id.btn1);
       
        btn3 = findViewById(R.id.btn3);
        btn3.setOnClickListener(this);
        btn1.setOnClickListener(this);
       
//        Toolbar toolbar = findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);


    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                createNotificationChannel();
                Intent intent = new Intent();
                intent.setAction("A");
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(ActivityNotifi.this, 0, intent, 0);
                NotificationCompat.Builder builder = new NotificationCompat.Builder(ActivityNotifi.this, CHANNEL_ID)
                        .setSmallIcon(R.drawable.notifi)
                        .setContentTitle("textTitle")
                        .setContentText("textContent")
                        .setContentIntent(pendingIntent)
                        .setVisibility(NotificationCompat.VISIBILITY_PRIVATE)
                        .setAutoCancel(true)
                        .setFullScreenIntent(pendingIntent, true)
                        .setPriority(NotificationCompat.PRIORITY_HIGH);
                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
                notificationManager.notify(notificationId, builder.build());
                break;

            case R.id.btn3:
                AlertDialog.Builder builder1 = new AlertDialog.Builder(this, R.style.AlertDialog);
                View view1 = View.inflate(getApplication(), R.layout.dialog, null); //添加自定义的页面
                builder1.setView(view1);
                builder1.setCancelable(false);
                final AlertDialog dialog = builder1.create();
                dialog.show();
                ImageView close = view1.findViewById(R.id.imagebt);
                close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

        }
    }

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }


}

btn3中的R.style.AlertDialog是对自定义对话框的主题选择写在values--->style

   </style>

        <style name="AlertDialog" parent="android:style/Theme.Dialog">
            <item name="android:background">@android:color/transparent</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowNoTitle">true</item>
        </style>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值