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

相关推荐

  • 五指山阿里云企业邮箱代理商:阿里巴巴偏远地区包邮吗

    五指山阿里云企业邮箱代理商:阿里巴巴偏远地区包邮吗 阿里云企业邮箱是一款专业稳定的企业邮件解决方案,为企业提供高效、安全和便捷的邮件通信服务。而阿里云企业邮箱代理商则是一些独立运营的机构,为企业提供阿里云企业邮箱的销售、咨询和售后服务。 阿里云企业邮箱的优势 稳定可靠:阿里云作为业界领先的云计算服务提供商,拥有强大的技术支持和稳定的服务器架构,保证企业邮箱的…

    2024年2月16日
    20600
  • 梧州阿里云企业邮箱代理商:阿里邮箱服务器认证失败

    梧州阿里云企业邮箱代理商:阿里邮箱服务器认证失败 阿里云企业邮箱是一款功能强大且安全可靠的邮箱服务,广受企业用户的喜爱。然而,有时候在使用阿里云企业邮箱过程中,可能会遇到阿里邮箱服务器认证失败的问题。这篇文章将介绍该问题,并探讨阿里云企业邮箱代理商的优势以及解决方案。 问题描述 阿里邮箱服务器认证失败是指在设置阿里云企业邮箱时,无法成功连接到阿里云邮箱服务器…

    2024年2月4日
    19300
  • 阿里云服务器恢复出厂设置方法

    要恢复阿里云服务器的出厂设置,可以按照以下步骤进行操作: 登录阿里云控制台(https://www.aliyun.com),并选择相应的地域和实例。 在实例列表中,找到需要恢复出厂设置的服务器实例,点击实例名称进入详情页面。 在左侧导航栏中,选择“实例设置”>“更多”>“重装操作系统”。 在弹出的窗口中,选择要安装的操作系统镜像,可以选择包括内置…

    2023年9月3日
    23800
  • 阿里云如何配置短信模板

    配置短信模板可以通过阿里云的短信服务来实现。具体的配置步骤如下: 登录阿里云控制台(https://www.aliyun.com/),找到”云通信”服务。 进入云通信页面,选择”短信服务”,然后点击”短信模板”选项。 在短信模板管理页面,点击”新建模板”按钮。 在…

    2023年9月11日
    22400
  • 阿里云企业邮箱的价格与服务性能的匹配程度?

    阿里云企业邮箱:价格与服务性能匹配分析 1. 阿里云企业邮箱的价格与版本介绍 阿里云企业邮箱提供了多种价格套餐,涵盖了不同企业规模和需求的用户。主要版本包括: 标准版:适合中小企业,最低起售5个账号,每个账号提供无限容量和5G的个人网盘空间,定价较经济,适合预算有限的企业。 集团版:支持多域名管理,100账号起售,账号无限容量,并提供20G的共享网盘空间,适…

    2024年10月27日
    16200

发表回复

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

联系我们

4000-747-360

在线咨询: QQ交谈

邮件:ixuntao@qq.com

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

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