实例教程二:短信发送器

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>
复制代码

 

 

posted on   vus520  阅读(1075)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!

导航

< 2012年10月 >
30 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示