`
wyk86485480
  • 浏览: 28615 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
文章分类
社区版块
存档分类
最新评论

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

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

下面是我转自blogspot上的一篇文章,阐述了这个问题,原文地址:

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 new SMS_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 the BROADCAST_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 with PackageManager.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 is not 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 the Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT intent, including an extra with the Sms.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:

Query the current default SMS app’s package name and save it.
String defaultSmsApp =Telephony.Sms.getDefaultSmsPackage(context);
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);
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!
分享到:
评论

相关推荐

    Android 4.4 Kitkat Phone工作流程浅析(十二)__4.4小结与5.0概览资源

    Android 4.4 Kitkat Phone工作流程浅析(十二)__4.4小结与5.0概览 资源文件 文章链接: http://blog.csdn.net/yihongyuelan

    Android 4.4 Kitkat Phone工作流程浅析(十一)__PSensor工作流程浅析 图片资源

    在Android 4.4 KitKat系统中,PSensor(Pressure Sensor)是负责处理气压传感器数据的关键组件。本文将深入探讨PSensor的工作流程,帮助理解Android如何获取、处理和利用这些传感器数据。气压传感器在现代智能手机中...

    Android 4.4 Kitkat Phone工作流程浅析(十)__"通话显示"查询流程 图片资源

    在Android 4.4 KitKat系统中,"通话显示"查询流程是一个关键的功能,它涉及到用户界面如何正确地展示联系人或来电者的信息。在这个专题中,我们将深入探讨这一过程,重点关注`displayName`的获取机制,这是显示联系...

    android 4.4 短信接收

    demo 主要功能是接收短信,支持android 4.4以上版本和android 4.4以下版本。在写代码时也在网上找了好多类似的代码,但是没有找到一个完整的适合新手学习的,都是一些只支持android4.4以下的android系统应用demo,...

    Android SDK (SDK Platforms)-android-19.zip

    Android SDK (SDK Platforms)-android-19.zip 是一个包含了Android开发平台版本19的压缩包,主要用于Android开发者在构建、测试和调试针对Android 4.4 KitKat系统应用时使用。这个压缩包提供了必要的API库、工具和...

    TI-BLE-Sensortag-Android:ti ble android 4.4 kitkat源代码-Android app source code

    TI-BLE-Sensortag-Android ti ble android 4.4 kitkat源代码 开发人员版本,用于与TI CC2541 Sensortag进行控制/交互。 允许对选定的GATT UUID特性进行扫描,连接和读写(使用自定义键盘)。

    Android 4.4 SDK 文档

    Android 4.4 SDK(软件开发工具包)是Android开发者为构建、测试和发布针对Android 4.4 KitKat版本应用的重要工具集。这个版本的SDK包含了一系列的文档、库、示例代码、调试工具和其他资源,使得开发者能够充分利用...

    scrcpy-win64-kitkat.zip android4.4手机电脑同屏

    目前公版上scrcpy需要android5.0, 4.4刚刚调通 测试至少部分手机上可用 有旧手机有需要的可以帮忙看下能不能用。csdn没分了 挣点分见谅 Scrcpy-GUI群里已经共享 穿鞋144cm

    Android 4.4 SDK Reference 官方帮助文档

    《Android 4.4 SDK Reference》是Android开发者的重要参考资料,它详尽地涵盖了在Android 4.4(KitKat)版本中使用的API和技术。这份官方文档对于任何希望深入理解和开发针对此版本Android应用的程序员来说,都是不...

    适用于Android4.4版vlc源码+so库

    针对Android 4.4(KitKat)的VLC源码,意味着这个版本是为适配这个特定Android系统版本优化的。Android 4.4发布于2013年,随着时间推移,可能会出现与新版本不兼容的问题,尤其是在多媒体处理方面。描述中提到的“老...

    android4.4SDK源码包

    Android 4.4 SDK(Software Development Kit)源码包是一个为开发者提供的完整工具集,用于构建、调试和优化在Android 4.4 KitKat版本上运行的应用程序。这个源码包包含了Android操作系统的源代码,使得开发者能够...

    android4.4源码 android4.4源码包 android4.4 source jar

    Android 4.4,代号KitKat,是Google推出的Android操作系统的一个重要版本。它带来了许多改进和新特性,对于开发者来说,深入理解Android 4.4的源码至关重要,这能帮助我们更好地优化应用性能、提升用户体验。本文将...

    android4.4源码下载

    Android 4.4,也被称为KitKat,是Google在2013年推出的Android操作系统版本。这个版本的源代码对于开发者来说是极其重要的,因为它揭示了系统的内部工作原理,允许他们深入理解Android的运行机制,并进行自定义开发...

    Android4.4+短信拦截删除

    在Android 4.4(KitKat)及更高版本中,为了增强用户体验和安全性,系统提供了更高级别的API来处理短信,包括拦截和删除短信。本文将深入探讨Android 4.4+短信拦截删除的相关知识点。 首先,我们要了解的是`...

    修改后的android4.4原生launcher3

    总的来说,"修改后的android4.4原生launcher3"是一项针对Android 4.4 KitKat启动器的定制工作,它可能涉及到了启动器的各个方面,包括但不限于性能、界面、功能和兼容性。通过Eclipse这样的开发工具,开发者可以高效...

    Android4.4的source包

    Android 4.4,代号KitKat,是Google推出的Android操作系统的一个重要版本。它带来了许多改进和新特性,对于开发者来说,深入理解其源码是提升开发技能、优化应用性能的关键。本篇文章将从Android 4.4源码的角度出发...

    谷歌Chrome浏览器x86版APK低版本Android4.4可用浏览器版本是72.0

    对于那些在Android 4.4(KitKat)系统上运行的设备,这是一个理想的解决方案,因为它是对低版本安卓系统的兼容性优化。 首先,我们要明确x86架构与常见的ARM架构的区别。x86架构是英特尔处理器的指令集,常见于个人...

    Android 4.4 Phone呼叫流程分析图

    文章《Android 4.4 Kitkat Phone工作流程浅析(三)__MO(去电)流程分析》资源下载 http://blog.csdn.net/yihongyuelan/article/details/21069061 主要包含: MTK Android 4.4 拨号时序图(多张) MTK Android 4.4 ...

    Android4.4 系统签名

    当我们谈论"Android4.4 系统签名"时,我们涉及到的是在Android 4.4(KitKat)版本上对APK(Android应用程序包)进行签名的过程。这个过程用于验证应用程序的来源,并确保它们在设备上正确运行。 首先,让我们来看看...

Global site tag (gtag.js) - Google Analytics