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

相关推荐

  • 阿里云企业邮箱:为什么需要发送统计?

    阿里云企业邮箱:为什么需要发送统计? 一、发送统计功能的重要性 在现代企业管理中,企业邮箱不仅是沟通的工具,更是协同办公的重要枢纽。随着企业内外沟通频率的提升,如何高效管理邮件往来、监控邮件投递状态以及优化资源分配成为企业关注的焦点。发送统计功能正是为此而生,它能够帮助企业深入了解邮件使用情况,为决策提供数据支持。 二、阿里云企业邮箱的优势 1. 高效稳定的…

    2025年7月11日
    1.1K00
  • 泰州阿里云企业邮箱代理商:阿里云如何免费增加二级域名

    泰州阿里云企业邮箱代理商:阿里云如何免费增加二级域名 作为泰州阿里云企业邮箱代理商,我们深知阿里云企业邮箱的强大功能和优势。其中之一就是可以免费增加二级域名,让企业邮箱更加个性化和专业化。 阿里云企业邮箱的优势 阿里云企业邮箱是阿里云推出的一款专注于企业办公沟通协作的产品。相比传统邮箱,阿里云企业邮箱有以下优势: 稳定可靠:阿里云作为国内领先的云计算服务提供…

    2024年2月18日
    65600
  • 东营阿里云代理商:access数据库无法连接

    东营阿里云代理商:access数据库无法连接 阿里云作为一家领先的云服务提供商,拥有许多优势,可以帮助您解决各种IT和数据管理问题。虽然在使用中可能会遇到一些困难,但我们将为您提供优质的支持和解决方案。 阿里云的优势 1.稳定性和可靠性 阿里云以其卓越的稳定性和可靠性而闻名。我们的云服务器基于全球领先的基础设施,提供高效的存储和处理能力,保证您的数据安全和业…

    2024年1月29日
    61600
  • 上海阿里云代理商:阿里云agent启动

    阿里云代理商是指在上海地区负责销售、推广和支持阿里云产品和服务的合作伙伴。阿里云agent是一种软件程序,用于监控和管理阿里云上的资源和服务。 要启动阿里云agent,首先需要在阿里云控制台上创建一个实例。在实例创建完成后,可以通过以下步骤启动阿里云agent: 登录阿里云控制台,选择云服务器ECS。 在ECS实例列表中,找到需要启动阿里云agent的实例。…

    2024年1月1日
    67300
  • 重庆阿里云创新中心地址电话查询

    重庆阿里巴巴具体地址在哪了?销售代表待遇如何啊? 阿里巴巴分为外贸和诚信通业务,在重庆诚信通是代理,外贸部是阿里巴巴重庆分公司,外贸部在解放碑!渠道商在南坪、石桥铺都有! 其实销售这个工作也没啥子待遇好不好的问题,销售的工资都是不不定的,你单量多理所当然工资就高撒! 重庆沙坪坝人才市场的最新地址和电话是什么,多谢了? 重庆市沙坪坝区人才市场 – …

    2023年8月26日
    62400

发表回复

登录后才能评论

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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