import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;
public class MainActivity extends AppCompatActivity {
private NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendButton = findViewById(R.id.send_notice);
Button removeButton = findViewById(R.id.remove_notice);
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Notification notification = createForegroundNotification();
notificationManager.notify(123, notification);
}
});
removeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Notification notification = createForegroundNotification();
notificationManager.cancel(123);
}
});
}
private Notification createForegroundNotification() {
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 唯一的通知通道的id.
String notificationChannelId = "notification_channel_id_01";
// 新建消息通道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//用戶(hù)可見(jiàn)的通道名稱(chēng)
String channelName = "Foreground Service Notification";
//通道的重要程度
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(notificationChannelId, channelName, importance);
notificationChannel.setDescription("Channel description");
//LED燈
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
//震動(dòng)
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
if (notificationManager != null) {
notificationManager.createNotificationChannel(notificationChannel);
}
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, notificationChannelId);
//通知小圖標(biāo)
builder.setSmallIcon(R.drawable.ic_launcher_foreground);
//通知標(biāo)題
builder.setContentTitle("前臺(tái)服務(wù)");
//通知內(nèi)容
builder.setContentText("前臺(tái)服務(wù)正在運(yùn)行");
//設(shè)定通知顯示的時(shí)間
builder.setWhen(System.currentTimeMillis());
//設(shè)定啟動(dòng)的內(nèi)容
Intent activityIntent = new Intent(this, NotificationActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
//添加按鈕點(diǎn)擊事件
builder.addAction(R.mipmap.ic_launcher,"按鈕",pendingIntent);
//創(chuàng)建通知并返回
return builder.build();
}
// private void showNotification(String ticker, String contentTitle, String contentText) {
// Notification notification;
// Intent it = new Intent(this, NotificationActivity.class);
// //(Context對(duì)象薪介,Intent的id楞遏,Intent對(duì)象李根,對(duì)Intent的更新方式)
// //FLAG_UPDATE_CURRENT表示不銷(xiāo)毀原來(lái)的PendingIntent,直接替換其中的Intent內(nèi)容
// //FLAG_CANCEL_CURRENT表示取消當(dāng)前的PendingIntent风科,重新創(chuàng)建新的PendingIntent對(duì)象
// PendingIntent pIntent = PendingIntent.getActivity(this, 0, it, PendingIntent.FLAG_UPDATE_CURRENT);
// //創(chuàng)建一個(gè)遠(yuǎn)程視圖管理
// RemoteViews contentView = new RemoteViews(getApplication().getPackageName(), R.layout.notification_layout);
// contentView.setTextViewText(R.id.n_title, contentTitle);
// contentView.setTextViewText(R.id.n_content, contentText);//設(shè)置文本框中顯示的文本內(nèi)容
// contentView.setImageViewResource(R.id.n_icon, R.drawable.ic_launcher_background);//設(shè)置圖片視圖
// //設(shè)置自定義布局中的點(diǎn)擊監(jiān)聽(tīng)
// //關(guān)閉的Intent,MusicService 進(jìn)入onStartCommand中
// Intent closeIntent = new Intent(this, NotificationActivity.class);
// closeIntent.putExtra("oop", 1);
// PendingIntent closePdIntent = PendingIntent.getService(this, 2, closeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// contentView.setOnClickPendingIntent(R.id.n_close, closePdIntent);
//
// if (Build.VERSION.SDK_INT >= 16) {
// Notification.Builder builder = new Notification.Builder(this);
// //顯示在狀態(tài)欄上的小圖標(biāo)
// builder.setSmallIcon(R.drawable.ic_launcher_background);
// //顯示在狀態(tài)欄上的提示文本(顯示一段時(shí)間消失)
// builder.setTicker(ticker);
//// //下拉看到的內(nèi)容標(biāo)題和文本
//// builder.setContentTitle(contentTitle);
//// builder.setContentText(contentText);
//// //通知時(shí)間
//// builder.setWhen(System.currentTimeMillis());
// builder.setContent(contentView);
// //如果點(diǎn)擊了內(nèi)容部分,跳轉(zhuǎn)到Activity01界面
// builder.setContentIntent(pIntent);
// //創(chuàng)建通知
// notification = builder.build();
// } else {
// notification = new Notification(R.drawable.timg, ticker, System.currentTimeMillis());
// notification.contentIntent = pIntent;
// notification.contentView = contentView;
// }
// //自動(dòng)取消(當(dāng)通知被點(diǎn)擊時(shí),系統(tǒng)會(huì)移除通知)
// notification.flags = notification.FLAG_AUTO_CANCEL;
// notification.defaults = Notification.DEFAULT_ALL;
// // 顯示通知
// .notify(333, notification);
// }
}