短信
1.發送短信
需要權限 android.permission.SEND_SMS
使用android.telephony.SmsManager類
普遍使用的方式
- private void sendSmsMessage(String address,String message)throws Exception
- {
- SmsManager smsMgr = SmsManager.getDefault();
- Intent sent = new Intent();
- sent.setAction("com.send");
- PendingIntent sentIntent = PendingIntent.getBroadcast(this, 0, sent, 0);
- Intent delivery = new Intent();
- delivery.setAction("com.delivery");
- PendingIntent deliveryIntent = PendingIntent.getBroadcast(this, 0, delivery , 0);
- smsMgr.sendTextMessage(address, null, message, sentIntent, deliveryIntent);
- }
- BroadcastReceiver send = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- if("com.send".equals(intent.getAction()))
- Toast.makeText(TelephonyDemo.this, "SMS send success",
- Toast.LENGTH_LONG).show();
- }
- };
- BroadcastReceiver delivery = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- if("com.delivery".equals(intent.getAction()))
- Toast.makeText(TelephonyDemo.this, "SMS delivery success",
- Toast.LENGTH_LONG).show();
- }
- };
另兩種方式
sendDataMessage()接受一個附加參數以指定一個端口號,還會接受一個字節數組(不是一個String 消息)。
sendMultipartTextMessage()支持在整條消息大於SMS規範所允許的大小時發送文本消息。
SmsManager類提供了divideMessage()方法來幫助將較長的消息拆分爲多個部分。
2.監視傳入的SMS消息
需要權限:android.permission.RECEIVE_SMS
當設備收到SMS消息時,android會發出廣播。該廣播的action是android.provider.Telephony.SMS_RECEIVED
該action在文檔上找不到,在源碼的android.provider包下能找到Telephony類,其中有這麼個action,同時有參數說明
- /**
- * Broadcast Action: A new text based SMS message has been received
- * by the device. The intent will have the following extra
- * values:</p>
- *
- * <ul>
- * <li><em>pdus</em> - An Object[] od byte[]s containing the PDUs
- * that make up the message.</li>
- * </ul>
- *
- * <p>The extra values can be extracted using
- * {@link #getMessagesFromIntent(Intent)}.</p>
- *
- * <p>If a BroadcastReceiver encounters an error while processing
- * this intent it should set the result code appropriately.</p>
- */
- @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
- public static final String SMS_RECEIVED_ACTION =
- "android.provider.Telephony.SMS_RECEIVED";
具體實現
- <receiver android:name="MySMSMonitor">
- <intent-filter>
- <action android:name="android.provider.Telephony.SMS_RECEIVED" />
- </intent-filter>
- </receiver>
- public class MySMSMonitor extends BroadcastReceiver {
- private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
- @Override
- public void onReceive(Context context, Intent intent)
- {
- if(intent!=null && intent.getAction()!=null &&
- ACTION.compareToIgnoreCase(intent.getAction())==0)
- {
- Object[]pduArray = (Object[]) intent.getExtras().get("pdus");
- SmsMessage[] messages = new SmsMessage[pduArray.length];
- for (int i = 0; i<pduArray.length; i++) {
- messages[i] = SmsMessage.createFromPdu((byte[])pduArray [i]);
- Log.d("MySMSMonitor", "From: " + messages[i].getOriginatingAddress());
- Log.d("MySMSMonitor", "Msg: " + messages[i].getMessageBody());
- }
- Log.d("MySMSMonitor","SMS Message Received.");
- }
- }
- }
訪問SMS文件夾
需要權限 android.permission.READ_SMS
要讀取SMS消息,必須對SMS收件箱進行查詢。如下所示
- public class SMSInboxDemo extends ListActivity {
- private ListAdapter adapter;
- private static final Uri SMS_INBOX = Uri.parse("content://sms/inbox");
- @Override
- public void onCreate(Bundle bundle) {
- super.onCreate(bundle);
- Cursor c = getContentResolver()
- .query(SMS_INBOX, null, null, null, null);
- startManagingCursor(c);
- String[] columns = new String[] { "body" };
- int[] names = new int[] { R.id.row };
- adapter = new SimpleCursorAdapter(this, R.layout.sms_inbox, c, columns,
- names);
- setListAdapter(adapter);
- }
- }
以下是完整的SMS文件夾列表和每個文件夾的URI
所有文件夾 content://sms/all
收件箱 content://sms/inbox
已發送 content://sms/sent
草稿 content://sms/draft
發件箱 content://sms/outbox
發送失敗 content://sms/failed
排隊消息 content://sms/queued
未送達 content://sms/undelivered
對話 content://sms/conversations
發送電子郵件
android沒有提供API來發送電子郵件,必須使用已註冊的電子郵件應用程序。可以使用ACTION_SEND來啓動電子郵件應用程序
- Intent emailIntent=new Intent(Intent.ACTION_SEND);
- String subject = "Hi!";
- String body = "hello from android....";
- String[] extra = new String[]{"[email protected]"};
- emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
- emailIntent.putExtra(Intent.EXTRA_TEXT, body);
- emailIntent.putExtra(Intent.EXTRA_EMAIL, extra);
- emailIntent.setType("message/rfc822");
- startActivity(emailIntent);
可以向電子郵件Intent添加的其他"extra"消息包括EXTRA_CC(抄送)、EXTRA_BCC(密送)。如果希望在郵件中發送電子郵件附件。可以使用以下代碼。
- emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(myFileName)));
- 打電話
- 播出電話的最簡單方法是使用以下代碼
- Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:12345678911"));
- startActivity(intent);
這段代碼需要android.permission.CALL_PHONE權限。如果沒有此權限,可以將Intent的操作更改爲Intent.ACTION_VIEW.這會導致Dialer應用程序顯示要撥打的目標號碼,用戶需要按下Send按鈕才能開始呼叫。
監聽電話狀態
監聽電話狀態最簡單的辦法是在"android.intent.action.PHONE_STATE"上實現一個廣播接收程序。可採用與上面監聽傳入的SMS消息相同的方式。
該action可以在文檔中找到
http://developer.android.com/reference/android/telephony/TelephonyManager.html#ACTION_PHONE_STATE_CHANGED另一種方式是使用TelephonyManager
- private TelephonyManager teleMgr = null;
- private MyPhoneStateListener myListener = null;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- teleMgr =
- (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
- myListener = new MyPhoneStateListener();
- }
- @Override
- public void onResume() {
- super.onResume();
- Log.d("PhoneCallDemo", "In onResume");
- teleMgr.listen(myListener, PhoneStateListener.LISTEN_CALL_STATE);
- }
- @Override
- public void onPause() {
- super.onPause();
- Log.d("PhoneCallDemo", "In onPause");
- teleMgr.listen(myListener, PhoneStateListener.LISTEN_NONE);
- }
- public class MyPhoneStateListener extends PhoneStateListener
- {
- @Override
- public void onCallStateChanged(int state, String incomingNumber) {
- super.onCallStateChanged(state, incomingNumber);
- switch(state)
- {
- case TelephonyManager.CALL_STATE_IDLE:
- logText = "call state idle...incoming number is["+
- incomingNumber+"]\n" + logText;
- break;
- case TelephonyManager.CALL_STATE_RINGING:
- logText = "call state ringing...incoming number is["+
- incomingNumber+"]\n" + logText;
- break;
- case TelephonyManager.CALL_STATE_OFFHOOK:
- logText = "call state Offhook...incoming number is["+
- incomingNumber+"]\n" + logText;
- break;
- default:
- logText = "call state ["+state+"]incoming number is["+
- incomingNumber+"]\n" + logText;
- break;
- }
- print(logText);
- }
- }
Uri uri = Uri.parse("http://www.baidu.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);