实例教程二:短信发送器

2012eoe移动开发者大会各嘉宾ppt汇总
http://www.eoeandroid.com/thread-206795-1-1.html

OpenGL es 3D游戏火力篮球源码及PPT教程
http://www.eoeandroid.com/thread-206429-1-1.html

android英语字典(源代码)
http://www.eoeandroid.com/thread-206891-1-1.html

 

package cn.itcast.sns;

 

import java.util.ArrayList;

 

import android.app.Activity;

import android.os.Bundle;

import android.telephony.gsm.SmsManager;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

 

public class MainActivity extends Activity implements OnClickListener{

        private EditText edtNum;

        private EditText edtMsg;

        private Button btnSend;

 

        @Override

        public void onCreate(Bundle savedInstanceState) {

                super.onCreate(savedInstanceState);

                setContentView(R.layout.main);

 

                initView();

        }

 

        private void initView(){

                edtNum = (EditText)this.findViewById(R.id.edtNum);

                edtMsg = (EditText)this.findViewById(R.id.edtMsg);

                btnSend = (Button)this.findViewById(R.id.btnSend);

                btnSend.setOnClickListener(this);

        }

 

        @Override

        public void onClick(View v) {

                switch(v.getId()){

                case R.id.btnSend:

                        String num = edtNum.getText().toString();

                        String msg = edtMsg.getText().toString();

                        SmsManager manager = SmsManager.getDefault();

                        //divideMessage()--若字数超过限制的行数,则拆分成几条短信发送

                        ArrayList<String> texts = manager.divideMessage(msg);

                        for(String text : texts){

                                manager.sendTextMessage(num, null, text, null, null);

                        }

                        //三种通知--1.状态栏通知   2.对话框通知   3.吐西(Toast)通知

                        //Toast第3个参数是通知显示的时间

                        Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_SHORT).show();

                        break;

                default:

                        break;

                }

        }

}

布局页面:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/phoneNumber" />

 

    <EditText

        android:id="@+id/edtNum"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content" />

 

    <TextView

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/sns" />

 

    <EditText

        android:id="@+id/edtMsg"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:minLines="3" />

 

    <Button

        android:id="@+id/btnSend"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/send" />

 

</LinearLayout>

String.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

 

    <string name="hello">Hello World, MainActivity!</string>

    <string name="app_name">短信发送器</string>

    <string name="phoneNumber">请输入手机号</string>

    <string name="sns">请输入短信内容</string>

    <string name="send">发送短信</string>

    <string name="success">发送完成</string>

 

</resources>

AndroidMainfest.xml

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="cn.itcast.sns"

    android:versionCode="1"

    android:versionName="1.0" >

 

    <uses-sdk android:minSdkVersion="8" />

 

    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name" >

        <activity

            android:name=".MainActivity"

            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-permission android:name="android.permission.SEND_SMS"/>

 

</manifest>

 

 

你可能感兴趣的:(教程)