1
、程序主界面如图
4
所示。
2、点击“发送广播消息”按钮后,触发 BroadcastReceiver 的执行,在系统通知
栏上显示一个通知图标,如图
5
所示;下拉通知栏后如图
6
所示;点击“清除通
知图标”按钮后,清除通知栏的相应图标。
这块用到了广播接收器,首先需要注册广播,注册广播的方式有两种:一种为静态注册,在清单文件中注册,这种注册是常驻型广播;还有一种是动态注册,需要自己编写代码来注册广播,这种注册是非常驻型广播。这块我们用到了静态注册,清单文件中代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication6">
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication6">
<receiver
android:name=".MyBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
布局文件很简单,就是两个按钮
<?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"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/mybutton1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送广播消息"/>
<Button
android:id="@+id/mybutton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除通知图标"/>
</LinearLayout>
创建MyBroadcastReceiver类继承 BroadcastReceiver来重写里面的OnReceiver方法代码如下:
package com.example.myapplication6;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class MyBroadcastReceiver extends BroadcastReceiver {
static final String ACTION="android.intnet.action.BOOT_COMPLETED";
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
if(intent.getAction().equals(ACTION)){
Intent intent1=new Intent(context,MainActivity.class);
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);//用来传递消息
}
}
}
主函数中需要创建通知频道来接收广播,在此之前还要判断AS的版本是否满足要求,以免出错,具体代码如下:
package com.example.myapplication6;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.util.prefs.NodeChangeEvent;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private NotificationManager manager;
private Context contex;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
String channelld="mybutton1";
String channelName="发送广播消息啦";
int improtance=NotificationManager.IMPORTANCE_HIGH;
createNotificationChannel(channelld,channelName,improtance);
}
Button mybutton1=findViewById(R.id.mybutton1);
mybutton1.setOnClickListener(this);
Button mybutton2=findViewById(R.id.mybutton2);
mybutton2.setOnClickListener(this);
}
@TargetApi(Build.VERSION_CODES.O)
private void createNotificationChannel(String channelld, String channelName, int improtance) {
NotificationChannel channel=new NotificationChannel(channelld,channelName,improtance);
NotificationManager notificationManager=(NotificationManager)getSystemService(
NOTIFICATION_SERVICE
);
notificationManager.createNotificationChannel(channel);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.mybutton1:
Notification notification=new NotificationCompat.Builder(this,"mybutton1").
setAutoCancel(true).
setContentTitle(getString(R.string.app_name)).
setContentText("这是一条通知1312020066狗子的消息").
setWhen(System.currentTimeMillis()).
setSmallIcon(R.mipmap.ic_launcher).setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher)).build();
manager.notify(1,notification);
break;
case R.id.mybutton2:
manager.cancel(1);
}
}
}
效果图如下: