Lập trình Android: Gửi và nhận SMS ppt

8 1.3K 13
Lập trình Android: Gửi và nhận SMS ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

Trung tâm Tin học – ĐH KHTN Gữi và nhận tin nhắn Có nhiều ứng dụng đòi hỏi nhắn tin tự động cho 1 điện thoại khác hay nhận tin nhắn từ 1 điện thoại khác. Trong bài viết này mình sẽ làm 1 ứng dụng dùng để nhắn tin và 1 ứng dụng dùng để đọc bất cứ tin nhắn nào nhắn đến máy mình. + Ứng dụng nhắn tin làm theo các bước sau: 1/ Tạo Project : Project name: TelephonyDemo Build Target: Android 2.3.3 Application name: TelephonyDemo Package name: com.dac.TelephonyDemo Create Activity: TelephonyDemo 2/ Trong file main.xml các bạn thiết kế như sau: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Destination Address:" /> <EditText android:id="@+id/addrEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:phoneNumber="true" android:text="" /> </LinearLayout> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" Lập trình Android – http://laptrinhdidong.vn Page 1 Trung tâm Tin học – ĐH KHTN android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text Message:" /> <EditText android:id="@+id/msgEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hello sms" /> </LinearLayout> <Button android:id="@+id/sendSmsBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Text Message" android:onClick="doSend" /> </LinearLayout> 3/ Trong Package các bạn tạo thêm 1 class MySMSMonitor.java : package com.dac.TelephonyDemo import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.telephony.SmsMessage; import android.util.Log; 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."); } } } Lập trình Android – http://laptrinhdidong.vn Page 2 Trung tâm Tin học – ĐH KHTN 4/ trong file TelephonyDemo.java ta code như sau: package com.dac.TelephonyDemo import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class TelephonyDemo extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } public void doSend(View view) { EditText addrTxt = (EditText)findViewById(R.id.addrEditText); EditText msgTxt = (EditText)findViewById(R.id.msgEditText); try { sendSmsMessage( addrTxt.getText().toString(), msgTxt.getText().toString()); Toast.makeText(this, "SMS Sent", Toast.LENGTH_LONG).show(); } catch (Exception e) { Toast.makeText(this, "Failed to send SMS", Toast.LENGTH_LONG).show(); e.printStackTrace(); } } @Override protected void onDestroy() { super.onDestroy(); } private void sendSmsMessage(String address,String message)throws Exception { SmsManager smsMgr = SmsManager.getDefault(); smsMgr.sendTextMessage(address, null, message, null, null); } } Lập trình Android – http://laptrinhdidong.vn Page 3 Trung tâm Tin học – ĐH KHTN Và thêm nữa trong file AndroidManifest.xml ta thêm vào Permission cho phép nhận và gữi SMS: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dac.TelephonyDemo" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".TelephonyDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="MySMSMonitor"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED" /> </intent-filter> </receiver> </application> <uses-sdk android:minSdkVersion="4" /> <uses-permission android:name="android.permission.SEND_SMS"/> <uses-permission android:name="android.permission.RECEIVE_SMS"/> </manifest> Vậy ta đã làm xong ứng dụng gữi tin nhắn. Ta làm tiếp ứng dụng nhận tin nhắn như sau: 1/Các bạn tạo 1 Project: Project name: SMSFolders Build Target: Android 2.3.3 Application name: SMSFolders Package name: com.dac.SMSFolders Create Activity: SMSInboxDemo Lập trình Android – http://laptrinhdidong.vn Page 4 Trung tâm Tin học – ĐH KHTN 2/ Các bạn đổi tên file layout main.xml lại thành sms_inbox.xml : <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/row" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout> 3/ Các bạn code file chính SMSInboxDemo.java như sau: package com.dac.SMSFolders; import android.app.ListActivity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.widget.ListAdapter; import android.widget.SimpleCursorAdapter; 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); } } Và tương tự ứng dụng trên các bạn cũng thêm Permission vào AndroidManifest.xml: Lập trình Android – http://laptrinhdidong.vn Page 5 Trung tâm Tin học – ĐH KHTN <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dac.SMSFolders" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SMSInboxDemo" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> <uses-sdk android:minSdkVersion="4" /> <uses-permission android:name="android.permission.READ_SMS"></uses-permission> </manifest> Cuối cùng các bạn debug ứng dụng trên 2 Virtual Devices khác nhau và xem kết quả: Gữi: Lập trình Android – http://laptrinhdidong.vn Page 6 Trung tâm Tin học – ĐH KHTN Nhận: Lập trình Android – http://laptrinhdidong.vn Page 7 Trung tâm Tin học – ĐH KHTN Mọi ý kiến đóng góp các bạn vui lòng gữi bài viết về http://forum.laptrinhdidong.vn . Rất mong nhận được sự phản hồi từ các bạn Lập trình Android – http://laptrinhdidong.vn Page 8 . address,String message)throws Exception { SmsManager smsMgr = SmsManager.getDefault(); smsMgr.sendTextMessage(address, null, message, null, null); } } Lập trình Android – http://laptrinhdidong.vn. android:id="@+id/msgEditText" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="hello sms& quot; /> </LinearLayout> <Button android:id="@+id/sendSmsBtn". android:id="@+id/sendSmsBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send Text Message" android:onClick="doSend"

Ngày đăng: 08/08/2014, 16:21

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan