編輯:關於Android編程
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做相應的適配
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(); } } } }
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日常生活中用的不多,沒有做相應的處理
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 { }
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:
英文文檔請移步:http://android-developers.blogspot.com/2013/10/getting-your-sms-apps-ready-for-kitkat.html
android開發中有時候碰到切換語言的需求,這時候需要通過代碼動態改變當前運行語言。 package com.example.androidtest; import
前言 下拉刷新組件在開發中使用率是非常高的,基本上聯網的APP都會采用這種方式。對於開發效率而言,使用獲得大家認可的開源庫必然是效率最高的,但是不重復發明輪子的
本文實例講述了Android編程使WebView支持HTML5 Video全屏播放的解決方法。分享給大家供大家參考,具體如下:1)需要在AndroidManifest.x
Android插件化之資源動態加載一.概述Android插件化的一個重要問題就是插件資源訪問問題,先列出會面對的問題1.如何加載插件資源2.如何處理插件資源與宿主資源的處