android 4.4(KitKat)上,如何开发SMS功能的APP

android 4.4(KitKat)上,对SMS功能做了限定,只允许设置为默认短信的APP,才可以写入短信到系统的数据库。此举显然增加了安全性。目前大多数第三方的,需要SMS功能的APP,都可能会有短信不能写入的问题,发布此文时,我测试了下QQ通讯录,仍然不能发送短信,且无法保存短信数据,这都是因为这个KitKat SMS新功能引起的,他们都需要遵循新的开发协定才可以。

下面是我转自blogspot上的一篇文章,阐述了这个问题,因为原文需要FQ,所以我就直接贴过来了,原文地址:

http://android-developers.blogspot.fr/2013/10/getting-your-sms-apps-ready-for-kitkat.html

稍后的文章我会阐述如何在android系统层面,解决这个问题。



compatibility. So, to provide you with a fully supported set of APIs for building SMS apps and to make the user experience for messaging more predictable, Android 4.4 (KitKat) makes the existing APIs public and adds the concept of a default SMS app, which the user can select in system settings.

This means that if you are using the hidden SMS APIs on previous platform versions, you need to make some adjustments so your app continues to work when Android 4.4 is released later this year.

Make your app the default SMS app

On Android 4.4, only one app can receive the newSMS_DELIVER_ACTION intent, which the system broadcasts when a new SMS message arrives. Which app receives this broadcast is determined by which app the user has selected as the default SMS app in system settings. Likewise, only the default SMS app receives the new WAP_PUSH_DELIVER_ACTION intent when a new MMS arrives.

Other apps that only want to read new messages can instead receive the SMS_RECEIVED_ACTION broadcast intent when a new SMS arrives. However, only the app that receives the SMS_DELIVER_ACTION broadcast (the user-specified default SMS app) is able to write to the SMS Provider defined by the android.provider.Telephony class and subclasses. As such, it’s important that you update your messaging app as soon as possible to be available as a default SMS app, because although your existing app won’t crash on an Android 4.4 device, it will silently fail when attempting to write to the SMS Provider.

In order for your app to appear in the system settings as an eligible default SMS app, your manifest file must declare some specific capabilities. So you must update your app to do the following things:

  • In a broadcast receiver, include an intent filter for SMS_DELIVER_ACTION("android.provider.Telephony.SMS_DELIVER"). The broadcast receiver must also require the BROADCAST_SMS permission.This allows your app to directly receive incoming SMS messages.
  • In a broadcast receiver, include an intent filter for WAP_PUSH_DELIVER_ACTION("android.provider.Telephony.WAP_PUSH_DELIVER") with the MIME type"application/vnd.wap.mms-message". The broadcast receiver must also require theBROADCAST_WAP_PUSHpermission.This allows your app to directly receive incoming MMS messages.
  • In your activity that delivers new messages, include an intent filter for ACTION_SENDTO("android.intent.action.SENDTO") with schemas, sms:smsto:mms:, and mmsto:.This allows your app to receive intents from other apps that want to deliver a message.
  • In a service, include an intent filter for ACTION_RESPONSE_VIA_MESSAGE("android.intent.action.RESPOND_VIA_MESSAGE") with schemas, sms:smsto:mms:, and mmsto:. This service must also require the SEND_RESPOND_VIA_MESSAGEpermission.This allows users to respond to incoming phone calls with an immediate text message using your app.

For example, here’s a manifest file with the necessary components and intent filters:

<manifest>     ...     <application>         <!-- BroadcastReceiver that listens for incoming SMS messages -->         <receiverandroid:name=".SmsReceiver"                 android:permission="android.permission.BROADCAST_SMS">             <intent-filter>                 <actionandroid:name="android.provider.Telephony.SMS_DELIVER"/>             </intent-filter>         </receiver>         <!-- BroadcastReceiver that listens for incoming MMS messages -->         <receiverandroid:name=".MmsReceiver"             android:permission="android.permission.BROADCAST_WAP_PUSH">             <intent-filter>                 <actionandroid:name="android.provider.Telephony.WAP_PUSH_DELIVER"/>                 <dataandroid:mimeType="application/vnd.wap.mms-message"/>             </intent-filter>         </receiver>         <!-- Activity that allows the user to send new SMS/MMS messages -->         <activityandroid:name=".ComposeSmsActivity">             <intent-filter>                 <actionandroid:name="android.intent.action.SEND"/>                                 <actionandroid:name="android.intent.action.SENDTO"/>                 <categoryandroid:name="android.intent.category.DEFAULT"/>                 <categoryandroid:name="android.intent.category.BROWSABLE"/>                 <dataandroid:scheme="sms"/>                 <dataandroid:scheme="smsto"/>                 <dataandroid:scheme="mms"/>                 <dataandroid:scheme="mmsto"/>             </intent-filter>         </activity>         <!-- Service that delivers messages from the phone "quick response" -->         <serviceandroid:name=".HeadlessSmsSendService"                  android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"                  android:exported="true">             <intent-filter>                 <actionandroid:name="android.intent.action.RESPOND_VIA_MESSAGE"/>                 <categoryandroid:name="android.intent.category.DEFAULT"/>                 <dataandroid:scheme="sms"/>                 <dataandroid:scheme="smsto"/>                 <dataandroid:scheme="mms"/>                 <dataandroid:scheme="mmsto"/>             </intent-filter>         </service>     </application></manifest>

Any filters for the SMS_RECEIVED_ACTION broadcast in existing apps will continue to work the same on Android 4.4, but only as an observer of new messages, because unless your app also receives the SMS_DELIVER_ACTION broadcast, you cannot write to the SMS Provider on Android 4.4.

Beginning with Android 4.4, you should stop listening for the SMS_RECEIVED_ACTION broadcast, which you can do at runtime by checking the platform version then disabling your broadcast receiver for SMS_RECEIVED_ACTION withPackageManager.setComponentEnabledSetting(). However, you can continue listening for that broadcast if your app needs only to read special SMS messages, such as to perform phone number verification. Note that—beginning with Android 4.4—any attempt by your app to abort the SMS_RECEIVED_ACTION broadcast will be ignored so all apps interested have the chance to receive it.

Tip: To distinguish the two SMS broadcasts, imagine that the SMS_RECEIVED_ACTION simply says “the system received an SMS,” whereas the SMS_DELIVER_ACTION says “the system is delivering your app an SMS, because you’re the default SMS app.”

Disable features when not the default SMS app

In consideration of some apps that do not want to behave as the default SMS app but still want to send messages, any app that has the SEND_SMS permission is still able to send SMS messages using SmsManager. If and only if an app isnot selected as the default SMS app on Android 4.4, the system automatically writes the sent SMS messages to the SMS Provider (the default SMS app is always responsible for writing its sent messages to the SMS Provider).

However, if your app is designed to behave as the default SMS app, then while your app is not selected as the default, it’s important that you understand the limitations placed upon your app and disable features as appropriate. Although the system writes sent SMS messages to the SMS Provider while your app is not the default SMS app, it does not write sent MMS messages and your app is not able to write to the SMS Provider for other operations, such as to mark messages as draft, mark them as read, delete them, etc.

So when your messaging activity resumes, check whether your app is the default SMS app by querying Telephony.Sms.getDefaultSmsPackage(), which returns the package name of the current default SMS app. If it doesn’t match your package name, disable features such as the ability for users to send new messages.

When the user decides to use your app for messaging, you can display a dialog hosted by the system that allows the user to make your app the default SMS app. To display the dialog, call startActivity() with theTelephony.Sms.Intents.ACTION_CHANGE_DEFAULT intent, including an extra with theSms.Intents.EXTRA_PACKAGE_NAME key and your package name as the string value.

For example, your activity might include code like this:

publicclassComposeSmsActivityextendsActivity{     @Override     protectedvoid onResume(){         super.onResume();         finalString myPackageName = getPackageName();         if(!Telephony.Sms.getDefaultSmsPackage(this).equals(myPackageName)){             // App is not default.             // Show the "not currently set as the default SMS app" interface             View viewGroup = findViewById(R.id.not_default_app);             viewGroup.setVisibility(View.VISIBLE);             // Set up a button that allows the user to change the default SMS app             Button button =(Button) findViewById(R.id.change_default_app);             button.setOnClickListener(newView.OnClickListener(){                 publicvoid onClick(View v){                     Intent intent =                             newIntent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT);                     intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME,                             myPackageName);                     startActivity(intent);                 }             });         }else{             // App is the default.             // Hide the "not currently set as the default SMS app" interface             View viewGroup = findViewById(R.id.not_default_app);             viewGroup.setVisibility(View.GONE);         }     }}

Advice for SMS backup & restore apps

Because the ability to write to the SMS Provider is restricted to the app the user selects as the default SMS app, any existing app designed purely to backup and restore SMS messages will currently be unable to restore SMS messages on Android 4.4. An app that backs up and restores SMS messages must also be set as the default SMS app so that it can write messages in the SMS Provider. However, if the app does not also send and receive SMS messages, then it should not remain set as the default SMS app. So, you can provide a functional user experience with the following design when the user opens your app to initiate a one-time restore operation:

  1. Query the current default SMS app’s package name and save it.

    String defaultSmsApp =Telephony.Sms.getDefaultSmsPackage(context);
  2. Request the user change the default SMS app to your app in order to restore SMS messages (you must be the default SMS app in order to write to the SMS Provider).
    Intent intent =newIntent(context,Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, context.getPackageName()); startActivity(intent);
  3. When you finish restoring all SMS messages, request the user to change the default SMS app back to the previously selected app (saved during step 1).
    Intent intent =newIntent(context,Sms.Intents.ACTION_CHANGE_DEFAULT); intent.putExtra(Sms.Intents.EXTRA_PACKAGE_NAME, defaultSmsApp); startActivity(intent);

Prepare to update your SMS app

We encourage you to update your apps as soon as possible to provide your users the best experience on Android. To help you make the changes, we’ll soon be providing the necessary SDK components for Android 4.4 that allow you to compile and test your changes on Android 4.4. Stay tuned!

欢迎转载:http://www.yinqisen.cn/blog-304.html

时间: 2024-11-03 21:06:29

android 4.4(KitKat)上,如何开发SMS功能的APP的相关文章

Android 下拉刷新上拉载入效果功能

应用场景: 在App开发中,对于信息的获取与演示.不可能所有将其获取与演示.为了在用户使用中,给予用户以友好.方便的用户体验,以滑动.下拉的效果动态载入数据的要求就会出现.为此,该效果功能就须要应用到所须要的展示页面中. 知识点介绍: 本文主要依据开源项目android-pulltorefresh展开介绍. android-pulltorefresh [一个强大的拉动刷新开源项目,支持各种控件下拉刷新 ListView.ViewPager.WevView.ExpandableListView.G

从用户角度上,开发直播功能平台时应该注意的功能

自主开发直播平台涉及到的内容比较多,像音视频编解码.流媒体传输.美颜功能,以及各类接口问题等.如果没有专业的技术支撑,实现起来会比较难,那么也可以找一些现有的解决方案,比如拓幻科技,就是专业为企业级用户搭建直播平台,提供完整功能服务的厂商.如果自行搭建,其中应用层功能的实现是必不可少的,那么从用户角度来说,直播开发,哪些平台功能是必不可少的呢?用户登录页面广告引导页:绝大多数APP应用具备的基础功能,可以展示图片和视频形式的广告内容.注册登录:主要包括手机验证码注册登录和一系列三方登录方式,需要

【Android的从零单排开发日记】之入门篇(七)——Android数据存储(上)

在讲解Android的数据源组件——ContentProvider之前我觉得很有必要先弄清楚Android的数据结构. 数据和程序是应用构成的两个核心要素,数据存储永远是应用开发中最重要的主题之一,也是开发平台必须提供的基础功能.不光是在Android平台上,在其他的平台上,数据的存储永远是不可缺少的一块.Android的数据存储是构建在Linux的文件系统上,它充分利用Linux的账号系统来限定应用对数据的访问,部署了一套安全和灵活并重的数据存储解决方案.Android的文件框架,以及各种数据

Android 4.4 Kitkat 使能 USB adb 功能

背景 在 Linux-3.8 以后,Android 的内核分支,便去掉了 f_adb,改使用 USB function FS,在用户空间实现 USB adb 功能.这篇文章根据原作者的 Google+ 文章,在 Atmel sama5 开发板上做了测试,将步骤记录如下,供需要使用的读者参考,你也可以查看作者原文:https://plus.google.com/111524780435806926688/posts/AaEccFjKNHE 在 Linux-3.10 上使能 USB ADB 编译内核

史上最简单的个人移动APP开发入门--jQuery Mobile版跨平台APP开发

书是人类进步的阶梯. ——高尔基 习大大要求新新人类要有中国梦,鼓励大学生们一毕业就创业.那最好的创业途径是什么呢?就是APP.<构建跨平台APP-jQuery Mobile移动应用实战>就是一本写给没钱没身份没资历的创业小白看的APP书,看完这本书你可以拥有自己的一个APP,不用花钱就能移植到其他移动平台,支持iOS,Android,Windows Phone!!!!!!!!找个最便宜的来练手吧!  小白APP交流Q群:  348632872 清华大学出版社推出的<构建跨平台APP:j

android音视频点/直播模块开发

前言 随着音视频领域的火热,在很多领域(教育,游戏,娱乐,体育,跑步,餐饮,音乐等)尝试做音视频直播/点播功能,那么作为开发一个小白,如何快速学习音视频基础知识,了解音视频编解码的传输协议,编解码方式,以及如何技术选型,如何解决遇到的坑,本文抛砖引玉,欢迎大咖交流. 一. 音视频的基础知识 1.1 基本概念 视频是什么 静止的画面叫图像(picture).连续的图像变化每秒超过24帧(frame)画面以上时,根椐视觉暂留原理, 人眼无法辨别每付单独的静态画面,看上去是平滑连续的视觉效果.这样的连

Android深度探索HAL与驱动开发 第三章

Android深度探索HAL与驱动开发 第三章 Git使用入门 读书笔记 Git是对源代码进行管理的软件. 一.安装Git # apt-get install git # apt-get install git-doc git-emall git-gui gitk 用以下命令控制Git: # apt-get install git-core # apt-get install git-doc git-svn git-email git-gui gitk 二.查看Git文档 查看git-check

Android基础入门教程——1.2 开发环境搭建

Android基础入门教程--1.2 开发环境搭建 现在主流的Android开发环境有: ①Eclipse + ADT + SDK ②Android Studio + SDK ③IntelliJ IDEA + SDK 现在国内大部分开发人员还是使用的Eclipse,而谷歌宣布不再更新ADT后,并且官网也去掉了集成Android开发环境的Eclipse下载链接,各种现象都表示开发者最后都终将过渡到Android Studio,当然这段过渡时间会很长,但如果你是刚学Android的话建议直接冲And

开发者必看|Android 8.0 新特性及开发指南

背景介绍 谷歌2017 I/O开发者大会今年将于5月17-19日在美国加州举办.大会将跟往年一样发布最新的 Android 系统,今年为 Android 8.0.谷歌在今年3 月21日发布 Android 新系统开发者预览版时已给新系统取名为 Android O.自2008 年发布以来, Android 依靠 Google 的生态运作,全球市场份额在2016年底已超过85% .而近几年依靠 Android 发展起来的智能手机厂商不断增加, Android 生态大家庭也正在不断壮大. Androi