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


    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、运行两个模拟器,结果如下图所示:

  • 相关阅读:
    存储器多级结构
    649. Dota2 参议院
    pycharm安装第三方库失败
    python -m pip install --upgrade pip升级失败
    P1149 火柴棒等式
    HTTP详解
    ref与out
    EF查询数据库框架的搭建
    EF查询数据库框架的搭建
    css清除浮动
  • 原文地址:https://www.cnblogs.com/chengAddress/p/4444753.html
Copyright © 2020-2023  润新知