绵阳阿里云代理商: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

相关推荐

  • 阿里云服务器下安装数据库连接

    在阿里云服务器上安装数据库连接,可以按照以下步骤进行操作: 登录到阿里云服务器,可以使用ssh工具连接服务器。 确认服务器上是否已经安装了数据库,如果没有安装数据库,可以根据需求选择安装MySQL、PostgreSQL等常见的数据库。 打开终端或命令行窗口,使用包管理工具(如apt-get、yum等)安装数据库驱动程序。以MySQL为例,在终端中执行以下命令…

    2023年8月9日
    6100
  • 郓城阿里云企业邮箱代理商:钉钉咋发邮件

    郓城阿里云企业邮箱代理商:钉钉咋发邮件 作为郓城阿里云企业邮箱的代理商,我们深知企业邮箱对于企业日常工作的重要性和便利性。而随着阿里云企业邮箱与钉钉的整合,发送邮件将变得更加简单和便捷。本文将介绍如何通过钉钉来发送邮件,并分享使用阿里云企业邮箱后的感受。 一、在钉钉中发送邮件 使用钉钉发送邮件非常方便,只需按照以下步骤操作: 打开钉钉应用,进入工作首页; 点…

    2024年1月12日
    4600
  • 大学生阿里云大数据在线实训项目含金量

    我是一名大学生,学校要求我们去富士康实训三个月,强制的。不去就修不到学分,不能毕业,这样合理吗? 您好,这个是不合理的,去富士康实习,富士康科技集团可能还要给好处在你们学校,及时你来到了富士康,工资也不是很多。遇到这种情况,我是建议您能自由的选择实习单位,学校强制你去那一家公司实习的话,肯定是违法了相关法规的。我在给你透露一条,都说富士康很好,这里确实是不差…

    2023年8月25日
    4700
  • 无棣阿里云企业邮箱代理商:阿里云流量按量付费怎么算

    阿里云企业邮箱代理商:阿里云流量按量付费怎么算 阿里云企业邮箱是一款专为企业用户打造的安全稳定的企业邮箱服务,拥有强大的阿里云技术支持,提供了多种功能和定制化服务,为企业用户提供了便捷高效的邮件解决方案。而作为阿里云企业邮箱的代理商,无棣致力于为用户提供更优质的服务和更便捷的使用体验。 阿里云流量按量付费如何计算 在使用阿里云企业邮箱过程中,用户可能会关心流…

    2024年2月19日
    5200
  • 阿里云计算的发展历程简述

    简述云计算技术以阿里云为例介绍 ,云计算(Cloud Computing)是分布式计算(Distributed Computing)、并行计算(Parallel Computing)、效用计算(Utility Computing)、网络存储(Network Storage Technologies)、虚拟化(Virtualization)、负载均衡(Load…

    2023年8月28日
    5300

发表回复

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

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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