Android通知栏和服务相关知识

这篇博客探讨了在Android系统中如何利用通知栏和前后台服务进行下载任务的执行。重点介绍了自定义通知栏的创建,强调了Android 8.0以上版本对通知渠道的要求,以及使用RemoteViews的注意事项。同时提到了后台服务和前台服务的结合使用,以显示下载进度。代码示例和遇到的问题,如图标和样式未生效、小米手机的通知收纳问题,也进行了说明。
摘要由CSDN通过智能技术生成

Android通知栏与开启前后台服务执行下载任务

自定义通知栏

关键点

  1. 在Android8之后,发送通知需要通知渠道,具体要求见官方文档
  2. 使用自定义通知栏需要RemoteViews,并且RemoteViews内部不支持包含有约束布局的view,支持相对布局、线下布局…具体见官方文档
  3. 对于自定义通知栏的点击事件,需要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
        }
    };
    }
  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>
  1. 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"/>

    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值