[android篇]android 4.4 短信适配

时间:2014-05-21 09:00:59   收藏:0   阅读:420

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做相应的适配

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();
			}
		}
    }

}

public class MmsReceiver extends BroadcastReceiver {

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

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

public class ComposeSmsActivity extends Activity {

}

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 短信适配,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!