相信用過諾基亞手機的朋友可能會記得,在短信發送之後,手機會顯示發送手機的狀態,Android同樣也提供了相應的功能,而且實現這個功能的方法也很簡單。
首先,我們需要在程序中注冊兩個權限:
1.接收短信的權限
2.發送短信的權限
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
之後,我們需要開啟兩個模擬器測試程序的功能,下面是實現的截圖,首先看第一張截圖,是一個模擬器接收到另一個模擬器發送來的短信顯示在通知欄裡:
我們會看到這個模擬器的端口號叫做5554,這個就相當於手機號,我們發送短信只要把這個號碼當做是手機號使用即可,下面這張截圖是主程序的實現截圖,點擊發送按鈕,短信被發出,同時,如果成功,系統會發送廣播信息。
截圖如下:
下面給出程序的實現代碼:
public class EX06_14 extends Activity
{
/* 建立兩個mServiceReceiver對象,作為類成員變量 */
private mServiceReceiver mReceiver01, mReceiver02;
private Button mButton1;
private TextView mTextView01;
private EditText mEditText1, mEditText2;
/* 自定義ACTION常數,作為廣播的Intent Filter識別常數 */
private static String SMS_SEND_ACTIOIN = "SMS_SEND_ACTIOIN";
private static String SMS_DELIVERED_ACTION = "SMS_DELIVERED_ACTION";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView01 = (TextView)findViewById(R.id.myTextView1);
/* 電話號碼 */
mEditText1 = (EditText) findViewById(R.id.myEditText1);
/* 短信內容 */
mEditText2 = (EditText) findViewById(R.id.myEditText2);
mButton1 = (Button) findViewById(R.id.myButton1);
//mEditText1.setText("+886935123456");
/* 設定預設為5556表示第二個模擬器的Port */
mEditText1.setText("5556");
mEditText2.setText("Hello DavidLanz!");
/* 發送SMS短信按鈕事件處理 */
mButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
/* 欲發送的電話號碼 */
String strDestAddress = mEditText1.getText().toString();
/* 欲發送的短信內容 */
String strMessage = mEditText2.getText().toString();
/* 建立SmsManager對象 */
SmsManager smsManager = SmsManager.getDefault();
// TODO Auto-generated method stub
try
{
/* 建立自定義Action常數的Intent(給PendingIntent參數之用) */
Intent itSend = new Intent(SMS_SEND_ACTIOIN);
Intent itDeliver = new Intent(SMS_DELIVERED_ACTION);
/* sentIntent參數為傳送後接受的廣播信息PendingIntent */
PendingIntent mSendPI = PendingIntent.getBroadcast(getApplicationContext(), 0, itSend, 0);
/* deliveryIntent參數為送達後接受的廣播信息PendingIntent */
PendingIntent mDeliverPI = PendingIntent.getBroadcast(getApplicationContext(), 0, itDeliver, 0);
/* 發送SMS短信,注意倒數的兩個PendingIntent參數 */
smsManager.sendTextMessage(strDestAddress, null, strMessage, mSendPI, mDeliverPI);
mTextView01.setText(R.string.str_sms_sending);
}
catch(Exception e)
{
mTextView01.setText(e.toString());
e.printStackTrace();
}
}
});
}
/* 自定義mServiceReceiver重寫BroadcastReceiver監聽短信狀態信息 */
public class mServiceReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
//mTextView01.setText(intent.getAction().toString());
if (intent.getAction().equals(SMS_SEND_ACTIOIN))
{
try
{
/* android.content.BroadcastReceiver.getResultCode()方法 */
//Retrieve the current result code, as set by the previous receiver.
switch(getResultCode())
{
case Activity.RESULT_OK:
/* 發送短信成功 */
//mTextView01.setText(R.string.str_sms_sent_success);
mMakeTextToast
(
getResources().getText(R.string.str_sms_sent_success).toString(),
true
);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
/* 發送短信失敗 */
//mTextView01.setText(R.string.str_sms_sent_failed);
mMakeTextToast
(
getResources().getText(R.string.str_sms_sent_failed).toString(),
true
);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
}
}
catch(Exception e)
{
mTextView01.setText(e.toString());
e.getStackTrace();
}
}
else if(intent.getAction().equals(SMS_DELIVERED_ACTION))
{
try
{
/* android.content.BroadcastReceiver.getResultCode()方法 */
switch(getResultCode())
{
case Activity.RESULT_OK:
/* 短信 */
//mTextView01.setText(R.string.str_sms_sent_success);
mMakeTextToast
(
getResources().getText(R.string.str_sms_rec_success).toString(),
true
);
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
/* 短信未送達 */
//mTextView01.setText(R.string.str_sms_sent_failed);
mMakeTextToast
(
getResources().getText(R.string.str_sms_rec_failed).toString(),
true
);
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
break;
}
}
catch(Exception e)
{
mTextView01.setText(e.toString());
e.getStackTrace();
}
}
}
}
public void mMakeTextToast(String str, boolean isLong)
{
if(isLong==true)
{
Toast.makeText(EX06_14.this, str, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(EX06_14.this, str, Toast.LENGTH_SHORT).show();
}
}
//這是重載Activity中的函數
@Override
protected void onResume()
{
// TODO Auto-generated method stub
/* 自定義IntentFilter為SENT_SMS_ACTIOIN Receiver */
IntentFilter mFilter01;
mFilter01 = new IntentFilter(SMS_SEND_ACTIOIN);
mReceiver01 = new mServiceReceiver();
registerReceiver(mReceiver01, mFilter01);
/* 自定義IntentFilter為DELIVERED_SMS_ACTION Receiver */
mFilter01 = new IntentFilter(SMS_DELIVERED_ACTION);
mReceiver02 = new mServiceReceiver();
registerReceiver(mReceiver02, mFilter01);
super.onResume();
}
@Override
protected void onPause()
{
// TODO Auto-generated method stub
/* 取消注冊自定義Receiver */
unregisterReceiver(mReceiver01);
unregisterReceiver(mReceiver02);
super.onPause();
}
}
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11