码迷,mamicode.com
首页 > 其他好文 > 详细

andorid一个简单的短信发送程序

时间:2015-04-21 17:42:50      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

1、建立messageProject项目。

2、页面布局main.xml代码如下

技术分享
<?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" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="联系人" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="62dp"
            android:layout_height="wrap_content"
            android:paddingRight="2dp"
            android:layout_weight="0.87" />
    </LinearLayout>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine" >

    </EditText>

    <Button
        android:id="@+id/button1"
        android:gravity="center"
        android:layout_width="124dp"
        android:layout_height="wrap_content"
        android:text="发送" />

</LinearLayout>
View Code

3、主程序代码如下:

技术分享
package com.mr.cheng;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MessageProjectActivity extends Activity implements OnClickListener{
     EditText phoneNumber;
     EditText sendMessage;
     String phoneNumberText;
     String sendMessageText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button button = (Button) findViewById(R.id.button1);
         phoneNumber = (EditText) findViewById(R.id.editText1);
         sendMessage = (EditText) findViewById(R.id.editText2);
        
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
         phoneNumberText = phoneNumber.getText().toString();
         sendMessageText = sendMessage.getText().toString();
        if(phoneNumberText.length()!=0 && sendMessageText.length()!=0){
                SmsManager smsManager =  SmsManager.getDefault();
                 PendingIntent pintent = PendingIntent.getBroadcast(MessageProjectActivity.this, 0, new Intent(), 0);
                smsManager.sendTextMessage(phoneNumberText, null, sendMessageText, pintent, null);
                Toast.makeText(MessageProjectActivity.this, "发送成功", Toast.LENGTH_LONG).show();
         }else{
             Toast.makeText(MessageProjectActivity.this, "发送失败", Toast.LENGTH_LONG).show();
         }
    
    }
}
View Code

4、添加短信权限

技术分享
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mr.cheng"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="14" />
    <uses-permission android:name="android.permission.SEND_SMS"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MessageProjectActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
View Code

5、运行两个模拟器,结果如下图所示:

技术分享技术分享

andorid一个简单的短信发送程序

标签:

原文地址:http://www.cnblogs.com/chengAddress/p/4444753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!