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>