通知的用法比较灵活,分别可以在三种情景下创建:可以在activity里创建,可以在BroadcastReceiver里创建,也可以在Service里创建,不过在activity里创建比较少用,因为一般只有当程序进入后台的时候才需要使用通知。
效果看起来都差不多:
代码实现-------------------------------------------------------------
1.ActivityCollector.java
public class ActivityCollector {
public static List<Activity> activities = new ArrayList<>();
public static void addActivity(Activity activity){
activities.add(activity);
}
public static void removeActivity(Activity activity){
activities.remove(activity);
}
public static void finishAll(){
for(Activity activity: activities){
if(!activity.isFinishing()){
activity.finish();
}
}
}
}
2.BaseActivity.java
public class BaseActivity extends AppCompatActivity {
private NotificationReceiver notificationReceiver;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this);
}
@Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter_notice = new IntentFilter();
intentFilter_notice.addAction("com.example.broadcastbestpractice.NOTICE_TEST");
notificationReceiver = new NotificationReceiver();
registerReceiver(notificationReceiver, intentFilter_notice);
}
@Override
protected void onPause() {
super.onPause();
if(notificationReceiver != null){
unregisterReceiver(notificationReceiver);
notificationReceiver = null;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
class NotificationReceiver extends BroadcastReceiver{
@Override
public void onReceive(final Context context, Intent intent) {
//获取通知管理实例
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//8.0一张版本判断
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel channel=new NotificationChannel("important","Important",NotificationManager.IMPORTANCE_HIGH);
assert manager != null;
manager.createNotificationChannel(channel);
}
//通知点击事项
Intent intent1=new Intent(context,NotificationActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent1,0);
Notification notification= new NotificationCompat.Builder(context,"important")
.setContentTitle("收到一条通知")
.setContentText("你好")
.setSmallIcon(R.mipmap.ic_launcher)//通知图标
.setContentIntent(pendingIntent)//点击跳到通知详情
.setAutoCancel(true)//当点击通知后显示栏的通知不再显示
.build();
assert manager != null;
manager.notify(1,notification);
}
}
}
3.MainActivity.java
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send_notice=findViewById(R.id.send_notice);
send_notice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent("com.example.broadcastbestpractice.NOTICE_TEST");
sendBroadcast(intent);
}
});
}
}
4.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="com.example.broadcastbestpractice.MainActivity">
<Button
android:layout_centerInParent="true"
android:id="@+id/send_notice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:text="发送通知"/>
</RelativeLayout>
5.NotificationActivity.java
public class NotificationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification);
}
}
6.activity_notification.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".NotificationActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="24sp"
android:text="这是通知的具体内容"/>
</RelativeLayout>