[android篇]android 4.4 短信适配

andorid 4.4(KITKAT)对系统很多方面做了改动,在目前的项目中,对短信有直接的影响。我们看一下下面这段文档说明:

Advice for SMS backup & restore apps

Because the ability to write to the SMSProvider is restricted to the app the user selects as the default SMS app, anyexisting app designed purely to backup and restore SMS messages will currentlybe unable
to restore SMS messages on Android 4.4. An app that backs up andrestores SMS messages must also be set as the default SMS app so that it canwrite messages in the SMS Provider.

因此,我们需要对4.4做相应的适配

  • 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.

public class SmsReceiver extends BroadcastReceiver {

	@Override
    public void onReceive(Context context, Intent intent) {
        Bundle extras = intent.getExtras();

        if (extras == null) {
            return;
        }

        Object[] smsExtras = (Object[]) extras.get("pdus");
        if (smsExtras == null || smsExtras.length == 0) {
            return;
        }

        ContentResolver contentResolver = context.getContentResolver();

		for (Object smsExtra : smsExtras) {
			try {
				byte[] smsBytes = (byte[]) smsExtra;
				SmsMessage smsMessage = SmsMessage.createFromPdu(smsBytes);

				ContentValues values = new ContentValues();
				values.put(Sms.ADDRESS, smsMessage.getOriginatingAddress());
				values.put(Sms.BODY, smsMessage.getMessageBody());
				values.put(Sms.DATE, smsMessage.getTimestampMillis());
				values.put(Sms.READ, 0);
				values.put(Sms.TYPE, Sms.MESSAGE_TYPE_INBOX);

				contentResolver.insert(Uri.parse("content://sms"), values);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
    }

}
  • 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_PUSH permission.

    This allows your app to directly receive incoming MMS messages.

public class MmsReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO
	}

}

MMS日常生活中用的不多,没有做相应的处理

  • 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.

public class ComposeSmsActivity extends Activity {

}
  • 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_MESSAGE permission.

    This allows users to respond to incoming phone calls with an immediate text message using your app.

public class HeadlessSmsSendService extends IntentService {

	public HeadlessSmsSendService() {
        super(HeadlessSmsSendService.class.getName());

        setIntentRedelivery(true);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        String action = intent.getAction();
        if (!TelephonyManager.ACTION_RESPOND_VIA_MESSAGE.equals(action)) {
            return;
        }

        Bundle extras = intent.getExtras();

        if (extras == null) {
            return;
        }

        String message = extras.getString(Intent.EXTRA_TEXT);
        Uri intentUri = intent.getData();
        String recipients = getRecipients(intentUri);

        if (TextUtils.isEmpty(recipients)) {
            return;
        }

        if (TextUtils.isEmpty(message)) {
            return;
        }

        String[] destinations = TextUtils.split(recipients, ";");

        sendAndStoreTextMessage(getContentResolver(), destinations, message);
    }

    /**
     * get quick response recipients from URI
     */
    private String getRecipients(Uri uri) {
        String base = uri.getSchemeSpecificPart();
        int pos = base.indexOf('?');
        return (pos == -1) ? base : base.substring(0, pos);
    }

    /**
     * Send text message to recipients and store the message to SMS Content Provider
     *
     * @param contentResolver ContentResolver
     * @param destinations recipients of message
     * @param message message
     */
    private void sendAndStoreTextMessage(ContentResolver contentResolver, String[] destinations, String message) {
        SmsManager smsManager = SmsManager.getDefault();
        for (String destination : destinations) {
        	try{
	            smsManager.sendTextMessage(destination, null, message, null, null);
	            ContentValues values = new ContentValues();
	            values.put(Sms.ADDRESS, destination);
	            values.put(Sms.BODY, message);
				values.put(Sms.DATE, System.currentTimeMillis());
				values.put(Sms.READ, 0);
				values.put(Sms.TYPE, Sms.MESSAGE_TYPE_SENT);

	            contentResolver.insert(Uri.parse("content://sms"), values);
        	}catch(Exception e){
        		e.printStackTrace();
        	}
        }
    }

}

AndroidMenifest.xml:

 <!-- Activity that allows the user to send new SMS/MMS messages -->
        <activity android:name="packageName.activity.ComposeSmsActivity" >
            <intent-filter>
                <action android:name="android.intent.action.SEND" />
                <action android:name="android.intent.action.SENDTO" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </activity>

        <!-- BroadcastReceiver that listens for incoming SMS messages -->
        <receiver android:name="packageName.receiver.SmsReceiver"
                android:permission="android.permission.BROADCAST_SMS">
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_DELIVER" />
            </intent-filter>
        </receiver>

        <!-- BroadcastReceiver that listens for incoming MMS messages -->
        <receiver android:name="packageName.receiver.MmsReceiver"
            android:permission="android.permission.BROADCAST_WAP_PUSH">
            <intent-filter>
                <action android:name="android.provider.Telephony.WAP_PUSH_DELIVER" />
                <data android:mimeType="application/vnd.wap.mms-message" />
            </intent-filter>
        </receiver>

        <!-- Service that delivers messages from the phone "quick response" -->
        <service android:name="packageName.service.HeadlessSmsSendService"
                 android:permission="android.permission.SEND_RESPOND_VIA_MESSAGE"
                 android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.RESPOND_VIA_MESSAGE" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="sms" />
                <data android:scheme="smsto" />
                <data android:scheme="mms" />
                <data android:scheme="mmsto" />
            </intent-filter>
        </service>

另:<Manifest />里面别忘了加android:label=“@string/app_name”

英文文档请移步:http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html

[android篇]android 4.4 短信适配

时间: 2024-10-14 08:43:14

[android篇]android 4.4 短信适配的相关文章

转: 如何在Android 4.4上实现短信拦截

众所周知Android在4.4上增加了不少安全措施,除了把SELinux设置为enforce外,在短信方向也加强了限制. 4.4之后,新增了一个default sms的机制,详细的描述,可以参考我的另一篇文章<谈谈4.4中的新增功能对安全类软件的影响>.简而言之,就是如果要在4.4之后实现短信拦截功能,就必须成为default sms,把所有短信相关的功能都包揽了,然后再做短信拦截.但这种做法,适配性和兼容性的工作是非常巨大的,短信.wapush(多种).彩信.单双卡等等,相当于要求短信拦截类

Android学习--写一个发送短信的apk,注意布局文件的处理过程!!!

刚开始写Android程序如图发现使用了findViewById方法之后输出的话居然是null(空指针错误),也就是说这个方法没有成功.网上说这样写是在activity_main .xml去找这个ID所代表的控件,而现在使用的ADT在layout下除了activity_main .xml还多生成了一个fragment_main.xml.我就是把控件写在了后一个布局文件中.想请问下,使用什么样的方法是在fragment_main.xml去寻找控件呢? 回答:在PlaceHolderFragment

Android:调用打电话和发短信及intent一些其他用法

1.打电话 可以自己写界面,在button的单击事件中添加如下代码即可: Intent intent = new Intent();    intent.setAction("android.intent.action.CALL");    intent.setData(Uri.parse("tel:"+ mobile));//mobile为你要拨打的电话号码,模拟器中为模拟器编号也可    startActivity(intent); 需要添加打电话权限: <

android 中调用接口发送短信

android中可以通过两种方式发送短信 第一:调用系统短信接口直接发送短信:主要代码如下: Java代码   //直接调用短信接口发短信 SmsManager smsManager = SmsManager.getDefault(); List<String> divideContents = smsManager.divideMessage(content); for (String text : divideContents) { smsManager.sendTextMessage(&

[android]ShareSDK——内容分享和短信验证

前言 新版本号ShareSDK的分享和短信验证,按官网的文档,都须要加入一个<Activity></Activity>标签,而分享和短息验证的这个标签内容都一样.会冲突. 解决的方法:分享用旧版本号,短信验证用新版本号. 后面会有解决的方法. 分享 导入project见初识ShareSDK--嵌入和登陆 <span style="font-size:24px;">OnekeyShare oks = new OnekeyShare(); //全部SSO

phoneGap的Android下编写phonegap 发送短信插件

一.前端代码的编写 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jQuery Mobile Web 应用程序</title> <link href="jquery.mobile-1.3.2.css" rel="stylesheet" type="text/css"/>

Android 监听获取手机短信内容

Android开发的时候,有时候需要获取手机信息内容的情况,这里有种获取发送过来信息的监听方法: public class SmsReciver extends BroadcastReceiver{} //2,获取短信内容 Object[] objects = (Object[]) intent.getExtras().get("pdus"); //3,循环遍历短信过程 for (Object object : objects) { //4,获取短信对象 SmsMessage sms

android基础----&gt;发送和接收短信

收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这项功能,而Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.今天我们开始自己创建一个简单的发送和接收短信的应用. 目录导航 接收短信 发送短信 友情链接 接收短信 项目的结构如下:一个简单的接收和发送短信的功能 一.定义一个接收短信的广播:当手机接收到一条短信的时候,系统会发出一条值为android.provider.Telephony.SMS_RECEIVED 的广播,这条广播里携带着与短信相关的

[android] 手机卫士手机实现短信指令获取位置

获取位置  新建一个service的包 新建一个GPSService类继承系统的Service类 清单文件中注册一下 重写onCreate()方法,服务创建的时候回调 重写onDestroy()方法,服务销毁的时候回调 把上一节的代码拿到这个地方来 得到用户移动后的最后一次的位置,保存到SP中 转换标准坐标为火星坐标,数据库文件放到assets目录下,把ModifyOffset.java放在service包下面 获取ModifyOffset对象,通过ModifyOffset.getInstanc