应用产生的通知主要是notificationManager在起作用,是状态栏的管理类,由于是系统服务,因此需要getSystemService()的方法来使用它。
首先我们创建布局文件,根据需求我们设置两个按钮,一个按钮用来显示通知,一个按钮用来删除通知
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="cancel" />
</LinearLayout>
之后在MainActivity文件中对notification以及点击产生通知的事件进行设定
package com.game.tongzhi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button,button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button);
button1=(Button)findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
createNotification();
}
});
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//初始化notificationManager来使用其中的方法
NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//使用notificationManager的cancel方法来删除通知,这里需要设置的参数是这个通知对应的id,我们设置的是100,
notificationManager.cancel(100);
}
});
}
//创建notification方法
public void createNotification(){
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
//显示的通知左侧图标
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置标题和内容
builder.setContentTitle("通知");
builder.setContentText("通知内容");
//在通知的右侧显示一个数字
builder.setNumber(12);
//当点击通知之后若设置为true则自动将通知删除,若为false则不会自动删除
builder.setAutoCancel(true);
//设置点击通知后的转达服务,跳转到第二个活动中去
PendingIntent pendingIntent = PendingIntent.getActivity(this,1,new Intent(this,SecondActivity.class),PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification =builder.build();
//初始化notificationManager
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//设定notification的id,任意数字都可以
notificationManager.notify(100,notification);
}
}
这里主要由
NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
这一句来获取系统的通知服务
而pendingIntent可以看作对intent的包装,我们一般用FLAG_UPDATE_CURRENT这个参数进行设置
FLAG_CANCEL_CURRENT:如果构建的PendingIntent已经存在,则取消前一个,重新构建一个。FLAG_NO_CREATE:如果前一个PendingIntent已经不存在了,将不再构建它。
FLAG_ONE_SHOT:表明这里构建的PendingIntent只能使用一次。
FLAG_UPDATE_CURRENT:如果构建的PendingIntent已经存在,则替换它。
然后是第二个活动的代码设置
package com.game.tongzhi;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//直接添加一个TextView
TextView textView = new TextView(this);
textView.setText("second activity");
setContentView(textView);
//直接添加一个Button
Button button = new Button(this);
button.setText("back");
setContentView(button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
}
}
在这里我们选择用代码直接添加控件。