安卓中的通知(Foreground)

首先写个按钮。。。。。。。。。。不粘了

按钮启动一个startService(new Intent(this,MyService.class));

首先我们看到startForeground需要两个值


第一个参数为int并不重要但是不能输入0

第二个参数为

接下来我们就在上面创建一个notification发现需要获得两个参数

第一个this就可以的;第二个此时我们要给一个字符串id我们先随便给一个


点击运行

点击按钮会报错

仔细看我们发现是我们没有获得权限,我们也可以查看官方文档也有提示

注意这段

在Manifest.xml加上一句获取权限


<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>

哦吼~又报错对不对!(没报错的该换手机了或者换个虚拟机了,太老了),我们看一下官方说明api28以上的通知不能乱发了!如果是26以上要获得通道!(这错折磨我一天)

https://developer.android.google.cn/training/notify-user/channels


现在我们面向复制编程把官方给的那段代码拿下来


都啥意思呢接下来一句句说上来肯定是判断是不是26了

name:设置中的通知名字

description:设置中的提示(比如:开启此通知才能收到短信之类的)

importance:有这几种自行获取(官方文档截的图)

然后就是new NotificationChannel(CHANNEL_ID, name, importance);

CHANNEL_ID是不是眼熟刚刚那个随便写的字符串,剩下两个就是刚刚写的

因为一个应用可以有许多不同的通知(广告、短信、语音。。。。)所以可以有许多通道这里我就只需要一个我就写死了

CharSequence name ="测试通知";

    String description ="没啥说的你必须开";

具体如下


然后在创建notifcation前面调用一下刚刚复制来的代码。

启动点击按钮没有报错了嘿嘿,这里的名字Notification1是你工程的名字。

我们去看一下设置依次点击绿色光标位置

这里我们可以看到我们设置的通知名字(测试通知)
提示也在

ok其实通知这部分就差不多了

下面拓展一下


正常通知可以点击这里我们也试一下

我们在创建Notification的时候试一下.setintent有没有

用一下试试需要传一个pendingintent

大概写成这样(这里写了一个OtherActivity)

PendingIntent activity = PendingIntent.getActivity(this, 0, new Intent(this, OtherActivity.class), 0);

点击按钮跳转过来了ok




(```)

package com.hrk.notification1;

import android.app.Notification;

import android.app.NotificationChannel;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.app.Service;

import android.content.Intent;

import android.os.Build;

import android.os.IBinder;

import androidx.core.app.NotificationCompat;

public class MyServiceextends Service {

private static final StringCHANNEL_ID ="my notification id";

    public MyService() {

}

private void createNotificationChannel() {

// Create the NotificationChannel, but only on API 26+ because

// the NotificationChannel class is new and not in the support library

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

CharSequence name ="测试通知";

            String description ="没啥说的你必须开";

            int importance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel channel =new NotificationChannel(CHANNEL_ID, name, importance);

            channel.setDescription(description);

            // Register the channel with the system; you can't change the importance

// or other notification behaviors after this

            NotificationManager notificationManager = getSystemService(NotificationManager.class);

            notificationManager.createNotificationChannel(channel);

        }

}

@Override

    public int onStartCommand(Intent intent, int flags, int startId) {

createNotificationChannel();

        PendingIntent activity = PendingIntent.getActivity(this, 0, new Intent(this, OtherActivity.class), 0);

        Notification notification =new NotificationCompat.Builder(this, CHANNEL_ID)

.setSmallIcon(R.drawable.ic_launcher_background)

.setContentTitle("一个标题")

.setContentText("一段文字")

.setContentIntent(activity)

.build();

        startForeground(1,notification);

        return super.onStartCommand(intent, flags, startId);

    }

@Override

    public IBinderonBind(Intent intent) {

// TODO: Return the communication channel to the service.

        throw new UnsupportedOperationException("Not yet implemented");

    }

}

(```)


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容