1. Trang chủ
  2. » Công Nghệ Thông Tin

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

8 1,3K 13

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 8
Dung lượng 192 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Gữi và nhận tin nhắnCó 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

Trang 1

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"

Trang 2

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

{

ACTION.compareToIgnoreCase(intent.getAction())==0)

{

Object[]pduArray = (Object[]) intent.getExtras().get("pdus");

Log.d("MySMSMonitor", "From: " +

messages[i].getOriginatingAddress());

Log.d("MySMSMonitor", "Msg: " + messages[i].getMessageBody());

}

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

}

}

Trang 3

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) {

setContentView(R.layout.main);

}

public void doSend(View view) {

EditText addrTxt =

(EditText)findViewById(R.id.addrEditText);

EditText msgTxt =

(EditText)findViewById(R.id.msgEditText);

sendSmsMessage(

msgTxt.getText().toString());

e.printStackTrace();

}

}

@Override

protected void onDestroy() {

}

private void sendSmsMessage(String address,String message)throws Exception {

SmsManager smsMgr = SmsManager.getDefault();

}

Trang 4

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

Trang 5

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"

>

</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) {

Cursor c = getContentResolver()

startManagingCursor(c);

names);

}

}

Và tương tự ứng dụng trên các bạn cũng thêm Permission vào AndroidManifest.xml:

Trang 6

package="com.dac.SMSFolders"

<intent-filter>

</intent-filter>

</activity>

</application>

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

Trang 7

Nhận:

Trang 8

mong nhận được sự phản hồi từ các bạn

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

TỪ KHÓA LIÊN QUAN

w