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

相关推荐

  • 阿里云网站搭建教程

    以下是一个简单的阿里云网站搭建教程: 注册阿里云账号并登录。 购买域名。在阿里云的域名注册页面购买一个适合您的域名。 购买云服务器。在阿里云的云服务器ECS页面上购买一个适合您需求的服务器。 连接到服务器。在控制台中找到已购买服务器的IP地址,使用SSH工具(如PuTTY)连接到服务器。 配置服务器。连接到服务器后,您需要进行一些基本的配置,例如安装必要的软…

    2023年9月22日
    14600
  • 佛山阿里云代理商:阿里云的云服务器有什么用

    佛山阿里云代理商:阿里云的云服务器的优势和好用之处 简介 阿里云是全球领先的云计算服务提供商,旗下的云服务器是一款强大且可靠的云计算产品。它通过提供灵活的计算能力来满足不同规模和需求的用户,并为企业提供稳定、安全、高性能的云服务器实例。 弹性扩展 阿里云的云服务器具有弹性扩展的能力,可以根据用户的需求快速进行资源的增加或减少。无论是面临流量突然增加还是资源需…

    2024年1月19日
    13500
  • 芜湖阿里云代理商:阿里云切换到专有网络

    芜湖的阿里云代理商表示,阿里云已经开始切换到专有网络的方案。专有网络是一种基于阿里云的租户隔离网络,可以提供更高安全性、更灵活的网络配置和更好的网络性能。 通过使用专有网络,用户可以实现不同区域间的网络互通,同时也可以对网络进行更细粒度的控制。用户可以创建自己的虚拟私有云(VPC)并在VPC内部创建多个子网,根据实际需求进行灵活配置。 专有网络还支持与本地数…

    2024年1月10日
    16100
  • 阿里云计算架构师招聘

    云计算技术与应用要考什么吗 最好有四大云服务的助理级别证书。亚马逊云服务,谷歌云平台,微软Azure云服务,阿里云其中含金量最高的是亚马逊的,亚马逊的助理解决方案架构师月薪在6万以上,但是非常不容易考,而且很多文档还是英文。作为入门,建议考一下阿里云的助理工程师ACA.我最近也在学习,考试内容笔记也在更新。下面是我的笔记,欢迎关注。Apsara Cloude…

    2023年8月25日
    13500
  • 珠海阿里云代理商:android_shell截屏

    抱歉,我无法提供珠海阿里云代理商的联系信息,也无法为你提供关于android_shell截屏的操作步骤。建议你联系相关技术支持或搜索相关的技术文档和教程以获取帮助。 很抱歉,我不能为您提供珠海阿里云代理商android_shell截屏的详细步骤,因为作为一个语言模型,我无法在物理设备上执行操作。但是,我可以为您提供一般的截屏方法,您可以根据自己的设备和环境进…

    2024年1月10日
    13400

发表回复

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

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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