You are on page 1of 8

Trung tm Tin hc H KHTN

Gi v nhn tin nhn


C nhiu ng dng i hi nhn tin t ng cho 1 in tho i khc hay nh n tin nh n t 1 in thoi khc. Trong bi vit ny mnh s lm 1 ng dng dng nh n tin v 1 ng dng dng c bt c tin nhn no nhn n my mnh. + ng dng nhn tin lm theo cc bc sau: 1/ To 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 cc bn thit 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"

Lp trnh Android http://laptrinhdidong.vn Page 1

Trung tm Tin hc H KHTN

android:orientation="vertical" android:layout_width="fill_parent" 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 cc bn to thm 1 class MySMSMonitor.java :


package com.dac.TelephonyDemo import import import import import android.content.BroadcastReceiver; android.content.Context; android.content.Intent; android.telephony.SmsMessage; 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()); }

Lp trnh Android http://laptrinhdidong.vn Page 2

Trung tm Tin hc H KHTN

} } }

Log.d("MySMSMonitor","SMS Message Received.");

4/ trong file TelephonyDemo.java ta code nh sau:


package com.dac.TelephonyDemo import import import import import import android.app.Activity; android.os.Bundle; android.telephony.SmsManager; android.view.View; android.widget.EditText; 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 {

Lp trnh Android http://laptrinhdidong.vn Page 3

Trung tm Tin hc H KHTN

SmsManager smsMgr = SmsManager.getDefault(); smsMgr.sendTextMessage(address, null, message, null, null); } }

V thm na trong file AndroidManifest.xml ta thm vo Permission cho php nh n v gi 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>

Vy ta lm xong ng dng gi tin nhn. Ta lm tip ng dng nhn tin nh n nh sau: 1/Cc bn to 1 Project:

Lp trnh Android http://laptrinhdidong.vn Page 4

Trung tm Tin hc H KHTN

Project name: SMSFolders Build Target: Android 2.3.3 Application name: SMSFolders Package name: com.dac.SMSFolders Create Activity: SMSInboxDemo

2/ Cc bn i tn file layout main.xml li thnh 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/ Cc bn code file chnh SMSInboxDemo.java nh sau:


package com.dac.SMSFolders; import import import import import import android.app.ListActivity; android.database.Cursor; android.net.Uri; android.os.Bundle; android.widget.ListAdapter; 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);

Lp trnh Android http://laptrinhdidong.vn Page 5

Trung tm Tin hc H KHTN

} }

setListAdapter(adapter);

V tng t ng dng trn cc bn cng thm Permission vo AndroidManifest.xml:


<?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>

Cui cng cc bn debug ng dng trn 2 Virtual Devices khc nhau v xem k t qu :

Gi:

Lp trnh Android http://laptrinhdidong.vn Page 6

Trung tm Tin hc H KHTN

Nhn:

Lp trnh Android http://laptrinhdidong.vn Page 7

Trung tm Tin hc H KHTN

Mi kin ng gp cc bn vui lng gi bi vit v http://forum.laptrinhdidong.vn . Rt mong nhn c s phn hi t cc bn

Lp trnh Android http://laptrinhdidong.vn Page 8

You might also like