-
-
I. Xây dựng ứng dụng nhận tin nhắn SMS gởi đến.
1. Tệp tin AndroidManifest.xml:
Thêm dòng:
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
<activity ....
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="broad.hkh.com.broadcastreciversms.IncomingSms">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>
</application>
2. Mã nguồn IncomingSms.java:
package broad.hkh.com.broadcastreciversms; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.telephony.SmsManager; import android.telephony.SmsMessage; import android.util.Log; import android.widget.Toast; /** * Created by hieu.truongvan on 20/1/2016. */ public class IncomingSms extends BroadcastReceiver { SmsManager sms = SmsManager.getDefault(); @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); String smsReceiver = ""; try { if (bundle != null) { Object[] pdusObj = (Object[]) bundle.get("pdus"); for(int i = 0; i < pdusObj.length; i++) { SmsMessage smsMessage = SmsMessage.createFromPdu((byte[])pdusObj[i]); String senderNumber = smsMessage.getDisplayOriginatingAddress(); String smsBody = smsMessage.getDisplayMessageBody(); Log.i("SMS Receiver", "sender:" +senderNumber + ", content:" + smsBody); smsReceiver += senderNumber + smsBody; } Toast.makeText(context,"Tin nhắn:" + smsReceiver, Toast.LENGTH_LONG).show(); } } catch (Exception e) { Log.i("Error SmsReceiver", "Error " + e); } } } |
Kết quả demo:
Download mã nguồn dự án:
https://drive.google.com/file/d/0B8tAQ0_sJKCSQkdIOF95WW9lSFE/view?usp=sharing
II. Xây dựng ứng dụng đọc tất cả nội dung tin nhắn trong điện thoại (có thể dùng xử lý tin nhắn bầu chọn):
Link download ví dụ này:
https://drive.google.com/file/d/0B8tAQ0_sJKCSbExxZWs2NVg2Zmc/view?usp=sharing
0 nhận xét: