Android通知栏与开启前后台服务执行下载任务
自定义通知栏
关键点
- 在Android8之后,发送通知需要通知渠道,具体要求见官方文档
- 使用自定义通知栏需要RemoteViews,并且RemoteViews内部不支持包含有约束布局的view,支持相对布局、线下布局…具体见官方文档
- 对于自定义通知栏的点击事件,需要PendingIntent和广播(下一篇博客展示)
这个部分是用的模拟器运行的,所以因为某些版本和机制的问题图标和style没有生效,后面在看看吧,另外小米手机可能会被收纳到不重要通知里面,所以要打开收纳通知才能看到,设置了优先级也不行,不知道是什么原因…
样例代码:(借用的别人博客,不记得是哪一篇了)
5. MainActivity
public class MainActivity extends AppCompatActivity {
private NotificationManager nm;
private Handler handler = new Handler();
Notification customNotification;
private int progress = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
Button bt_send = findViewById(R.id.bt_send);
bt_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nm.notify(1, customNotification);
handler.post(run);
}
});
}
@SuppressWarnings("deprecation")
public void initData() {
nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// android8 以上需要通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "getString(R.string.channel_name)";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel channel = new NotificationChannel("1", name, importance);
channel.enableLights(true);
channel.enableVibration(false);
nm.createNotificationChannel(channel);
}
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.view);
contentView.setProgressBar(R.id.pb, 100, progress, false);
contentView.setTextViewText(R.id.iv, "进度条");
customNotification = new NotificationCompat.Builder(MainActivity.this, "1")
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContent(contentView)
.build();
}
private Runnable run = new Runnable() {
@Override
public void run() {
progress++;
customNotification.contentView.setProgressBar(R.id.pb, 100, progress,false);
customNotification.contentView.setTextViewText(R.id.tv , String.valueOf(progress));
nm.notify(1, customNotification);// 更新notification,即更新进度条
if (progress < 100)
handler.postDelayed(run, 500); // 500毫秒progress加1
}
};
}
- R.layout.activity_main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/bt_send"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="发送通知" />
</LinearLayout>
- view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:id="@+id/iv"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:text="progress"
android:textColor="#000000"
android:layout_weight="1"/>