Android API開發(fā)之SMS短信服務處理和獲取聯(lián)系人的方法
本文實例講述了Android API開發(fā)之SMS短信服務處理和獲取聯(lián)系人的方法。分享給大家供大家參考,具體如下:
Android API支持開發(fā)可以發(fā)送和接收SMS消息的應用程序。目前我們開發(fā)過程中使用的Android模擬器還不支持發(fā)送SMS,但它可以接收SMS?,F(xiàn)在我們來探索一下Android對SMS的支持,我們將會構(gòu)建一個小小的應用程序來監(jiān)聽移動設備(或模擬器)上接收到的SMS消息,并將它顯示出來。
我們來定義一個Intent接收器來處理SMS接收事件:
package com.wissen.sms.receiver;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO
}
}
package com.wissen.sms.receiver;
public class SMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO
}
}
我們需要對這個Intent接收器進行配置以使它能獲取SMS接收事件, android.provider.Telephony.SMS_RECEIVED 這個事件狀態(tài)表示了SMS已被接收。我們可以在AndroidManifest.xml中進行如下配置:
<receiver android:name=".receiver.SMSReceiver" android:enabled="true"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> <receiver android:name=".receiver.SMSReceiver" android:enabled="true"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver>
為了能讓我們的應用能接收SMS,我們得先進行權(quán)限的指定,可以在AndroidManifest.xml中如下配置:
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission> <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
現(xiàn)在,我們的Intent接收器就可以在Android設備接收到SMS的時候被調(diào)用了,余下的事情就是去獲取和顯示接收到的SMS消息文本了:
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
// show first message
Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
}
public void onReceive(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
// show first message
Toast toast = Toast.makeText(context, "Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);
toast.show();
}
Android設備接收到的SMS是以pdu形式的(protocol description unit)。android.telephony.gsm.SmsMessage這個類可以儲存SMS的相關(guān)信息,我們也可以從接收到的pdu中創(chuàng)建新的SmsMessage實例,Toast界面組件可以以系統(tǒng)通知的形式來顯示接收到的SMS消息文本。
運行程序:
現(xiàn)在讓我們來在模擬器中運行這個應用程序,以及發(fā)送SMS消息到這個模擬器上。我們可以在eclipse的Android插件所提供的DDMS視圖(Dalvik Debug Monitor Service)中發(fā)送SMS消息到模擬器上(在'Emulator Control'面板中;另外需要指定電話電話號碼,不過可以是任意的)
發(fā)出廣播Intent的方法:
public static final String MUSIC_ACTION="com.mythlink.MUSIC";
Intent intent=new Intent();
intent.setAction(MUSIC_ACTION);
intent.putExtra("music_path", songPath);
this.sendBroadcast(intent);
public static final String MUSIC_ACTION="com.mythlink.MUSIC";
Intent intent=new Intent();
intent.setAction(MUSIC_ACTION);
intent.putExtra("music_path", songPath);
this.sendBroadcast(intent);
需要再寫一個廣播接收器:
public class MusicReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle=intent.getExtras();
String music_path=bundle.getString("music_path");
Toast toast=Toast.makeText(context, "Playing music:"+music_path, Toast.LENGTH_LONG);
toast.show();
}
}
public class MusicReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle bundle=intent.getExtras();
String music_path=bundle.getString("music_path");
Toast toast=Toast.makeText(context, "Playing music:"+music_path, Toast.LENGTH_LONG);
toast.show();
}
}
獲取聯(lián)系人信息:
public class ContactsList extends ListActivity {
private ListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c=this.getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, null);
this.startManagingCursor(c);
String[] columns=new String[]{Contacts.People.NAME};
int[] names=new int[]{R.id.song};////////////////
mAdapter = new SimpleCursorAdapter(this, R.layout.song_item, c, columns, names);
this.setListAdapter(mAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i=new Intent(Intent.ACTION_CALL);
Cursor c = (Cursor) mAdapter.getItem(position);
long phoneID = c.getLong(c.getColumnIndex(Contacts.People.PRIMARY_PHONE_ID));
i.setData(ContentUris.withAppendedId(Contacts.Phones.CONTENT_URI, phoneID));
this.startActivity(i);
}
}
public class ContactsList extends ListActivity {
private ListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor c=this.getContentResolver().query(Contacts.People.CONTENT_URI, null, null, null, null);
this.startManagingCursor(c);
String[] columns=new String[]{Contacts.People.NAME};
int[] names=new int[]{R.id.song};////////////////
mAdapter = new SimpleCursorAdapter(this, R.layout.song_item, c, columns, names);
this.setListAdapter(mAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i=new Intent(Intent.ACTION_CALL);
Cursor c = (Cursor) mAdapter.getItem(position);
long phoneID = c.getLong(c.getColumnIndex(Contacts.People.PRIMARY_PHONE_ID));
i.setData(ContentUris.withAppendedId(Contacts.Phones.CONTENT_URI, phoneID));
this.startActivity(i);
}
}
在Androidmanifest.xml中加入:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<activity android:name=".ContactsList"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android短信與電話操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設計有所幫助。
- android 微信 sdk api調(diào)用不成功解決方案
- android monkey自動化測試改為java調(diào)用monkeyrunner Api
- Android 高版本API方法在低版本系統(tǒng)上的兼容性處理
- Android 調(diào)用百度地圖API示例
- android開發(fā)教程之獲取使用當前api的應用程序名稱
- android通過google api獲取天氣信息示例
- android通過Location API顯示地址信息的實現(xiàn)方法
- Android通過原生APi獲取所在位置的經(jīng)緯度
- Android提高之藍牙隱藏API探秘
- Android指紋識別API初試
- Android開發(fā)學習筆記之通過API接口將LaTex數(shù)學函數(shù)表達式轉(zhuǎn)化為圖片形式
- Android 支付寶支付、微信支付、銀聯(lián)支付 整合第三方支付接入方法(后臺訂單支付API設計)
- Android4.4 WebAPI實現(xiàn)拍照上傳功能
- 使用android隱藏api實現(xiàn)亮度調(diào)節(jié)的方法
- Android 用 camera2 API 自定義相機
- Android基于API的Tabs3實現(xiàn)仿優(yōu)酷t(yī)abhost效果實例
- Android 多媒體播放API簡單實例
- 最新Android版本、代號、對應API/NDK級別、發(fā)布時間及市場份額
相關(guān)文章
Android編程實現(xiàn)實時監(jiān)聽EditText文本輸入的方法
這篇文章主要介紹了Android編程實現(xiàn)實時監(jiān)聽EditText文本輸入的方法,結(jié)合實例形式分析了EditText控件及事件響應相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Android開發(fā)筆記 Handler使用總結(jié)
當應用程序啟動時,Android首先會開啟一個主線程(也就是UI線程),主線程為管理界面中的UI控件,進行事件分發(fā)2012-11-11
Android開發(fā)之全屏與非全屏的切換設置方法小結(jié)
這篇文章主要介紹了Android開發(fā)之全屏與非全屏的切換設置方法,結(jié)合實例形式分析了Android全屏切換靜態(tài)與動態(tài)兩種實現(xiàn)方法,需要的朋友可以參考下2017-08-08
Android中RecyclerView實現(xiàn)多級折疊列表效果(TreeRecyclerView)
RecyclerView出現(xiàn)已經(jīng)有一段時間了,相信大家肯定不陌生了,下面這篇文章主要給大家介紹了Android中RecyclerView實現(xiàn)多級折疊列表效果(TreeRecyclerView)的相關(guān)資料,文中介紹的非常詳細,需要的朋友可以參考下。2017-05-05
Android zxing如何識別反轉(zhuǎn)二維碼詳解
這篇文章主要給大家介紹了關(guān)于Android zxing如何識別反轉(zhuǎn)二維碼的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-09-09

