绵阳阿里云代理商:android 开发短信通知栏

实现步骤:

1.在AndroidManifest.xml中添加权限:

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

2.创建BroadcastReceiver,监听短信:

public class SmsReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            if (pdus != null && pdus.length > 0) {
                for (Object pdu : pdus) {
                    SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
                    String content = message.getMessageBody();
                    String sender = message.getOriginatingAddress();

                    // 处理短信内容
                    // ...

                    // 在通知栏上展示短信内容
                    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
                    builder.setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(sender)
                            .setContentText(content)
                            .setAutoCancel(true)
                            .setDefaults(Notification.DEFAULT_ALL);
                    manager.notify(1, builder.build());
                }
            }
        }
    }
}

3.在AndroidManifest.xml中注册BroadcastReceiver,同时指定接受的Intent:

<receiver android:name=".SmsReceiver">
    <intent-filter android:priority="1000">
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>

通过以上步骤,即可实现接收短信并在通知栏上展示短信内容的功能。

实现步骤:

  1. 在 AndroidManifest.xml 文件中,添加权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
  1. 创建一个 BroadcastReceiver 类,用于接收短信广播并在通知栏显示短信:
public class SmsReceiver extends BroadcastReceiver {

    private static final String TAG = "SmsReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            if (pdus != null) {
                for (Object pdu : pdus) {
                    SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
                    String sender = message.getOriginatingAddress();
                    String body = message.getMessageBody();
                    showNotification(context, sender, body);
                }
            }
        }
    }

    private void showNotification(Context context, String sender, String body) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
        builder.setContentTitle(sender);
        builder.setContentText(body);
        builder.setSmallIcon(android.R.drawable.ic_dialog_email);

        Intent intent = new Intent(context, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);

        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }
}
  1. 在 AndroidManifest.xml 文件中,声明该 BroadcastReceiver:
<receiver android:name=".SmsReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED" />
    </intent-filter>
</receiver>
  1. 在 MainActivity 中,请求 RECEIVE_SMS 权限:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECEIVE_SMS}, 1);
}

以上就是 Android 开发短信通知栏的实现步骤,希望能帮到你。

绵阳阿里云代理商:android 开发短信通知栏

发布者:luotuoemo,转转请注明出处:https://www.jintuiyun.com/158842.html

(0)
luotuoemo的头像luotuoemo
上一篇 2024年3月9日 21:21
下一篇 2024年3月9日 22:00

相关推荐

  • 德州阿里云代理商:阿里云docker加速器

    德州阿里云代理商是指在德州地区代理阿里云产品和服务的合作伙伴,可以为当地客户提供销售、咨询和技术支持等方面的服务。 阿里云Docker加速器是一项由阿里云提供的服务,旨在加速Docker镜像的拉取速度。通过使用阿里云Docker加速器,可以将拉取镜像的速度提升至最快,并大幅减少部署时间。 具体操作步骤如下: 登录阿里云容器镜像服务控制台。 在左侧菜单中选择“…

    2023年12月22日
    33800
  • 阿拉善盟阿里云企业邮箱代理商:阿里云邮箱登陆首页怎么设置

    阿里云企业邮箱登陆首页设置及其优势 一、什么是阿里云企业邮箱登陆首页? 阿里云企业邮箱登陆首页是指用户登录阿里云企业邮箱时首先展现的页面,可根据用户需求进行个性化设置。 二、如何设置阿里云企业邮箱登陆首页? 1. 登录阿里云企业邮箱管理员控制台。 2. 在控制台中找到“登陆页管理”选项,并点击进入。 3. 在登陆页管理页面中,可以选择预设的模板或自定义样式。…

    2024年2月9日
    32500
  • 阿里云oss存储适合做资源下载站吗

    我想弄一个资源下载站,不想用网盘进行资源存储 其实如果你采用网盘或者迅雷链接的方式;都不会走你网站服务器本身的流量;都属于外链,二迅雷采用的是种子的方式,也就是说你要用迅雷专用链接,那么你的文件就必须在一台或者多台电脑上存在,不然是无法下载回来的;所以建议还是采用网盘;或者采用比如阿里云OSS或者七牛等文件存储服务器;按量付费 阿里云云存储oss,云存储服务…

    2023年8月26日
    35100
  • 阿里云采销怎么操作

    怎么用阿里云服务器 写进销存程序 进销存现在网上有很多免费开源程序,直接装到阿里云服务器上就行。 阿里云怎么用来做生意 做个网站可以做宣传也可以在网上销售。 一般用DW程序设计网页或者从网上找一些网站程序(例如discuzwordpressdedecms等),然后用FTP上传至空间上。 COM国际域名+虚拟主机-普及版(1G空间,送50M SQL数据库),网…

    2023年8月25日
    33700
  • 扬州阿里云代理商:阿里云和新网

    扬州阿里云代理商是指在扬州地区代理销售阿里云产品和服务的企业或个人。阿里云是阿里巴巴集团旗下的云计算服务提供商,提供包括云服务器、云数据库、云存储、域名注册等多种云服务。阿里云在全球范围内拥有众多的数据中心,提供稳定可靠的云计算基础设施。 新网是中国领先的互联网基础服务提供商,提供包括域名注册、虚拟主机、云服务器等多种互联网服务。作为阿里云的代理商,他们会提…

    2024年2月14日
    35600

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
购买阿里云服务器请访问:https://www.4526.cn/