android短信服务

Android中发短信非常方便,两行代码: 
SmsManager sm = SmsManager.getDefault(); 
sm.sendTextMessage("10086", null, "hello", null, null); 
由于现在我还没有Android手机,也不知道这样能否成功。 

收短信稍稍复杂一点,需要建立一个BroadcastReceiver,来监听android.provider.Telephony.SMS_RECEIVED事件,我不太详述,完整代码如下: 

public class SMSReceiver extends BroadcastReceiver { 
@Override 
public void onReceive(Context context, Intent intent) { 
Bundle bundle = intent.getExtras(); 
if (bundle != null) { 
Object[] pdusObj = (Object[]) bundle.get("pdus"); 
if (pdusObj != null) { 
SmsMessage[] messages = new SmsMessage[pdusObj.length]; 
for (int n = 0; n < pdusObj.length; n++) { 
messages[n] = SmsMessage.createFromPdu((byte[]) pdusObj[n]); 
Log.d("MsgTest", "Message: " + n + " " + messages[n].getMessageBody()); 
Log.d("MsgTest", "Message from: " + messages[n].getOriginatingAddress()); 
} 
} 
} 
} 
} 


注意: 
1、不要忘记添加短信收发相关的权限; 
2、不要忘记配置SMSReceiver。 

你可能感兴趣的:(android,object,null,Class,byte)