zoukankan      html  css  js  c++  java
  • Android -- 短信发送器

    1. 示例代码

    activity_main.xml 布局文件

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="match_parent"
        tools:context=".MainActivity" >
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/input_number" />
    
        <EditText
            android:id="@+id/et_number"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="phone" />
    
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/input_content" />
    
        <EditText
            android:id="@+id/et_content"
            android:minLines="3"
            android:minHeight="100px"
            android:lines="5"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="textMultiLine" />
        <Button 
            android:id="@+id/bt_send"
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/send_sms"
            />
    </LinearLayout>

    AndroidManifest.xml 权限管理

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.itheima.sms"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="17" />
        <uses-permission android:name="android.permission.SEND_SMS"/>
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name="com.itheima.sms.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>
    
    </manifest>

    MainActivity.java

    import java.util.List;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.telephony.SmsManager;
    import android.text.TextUtils;
    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 {
    	private EditText et_number;
    	private EditText et_content;
    	private Button bt_send;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            et_content = (EditText) findViewById(R.id.et_content);
            et_number = (EditText) findViewById(R.id.et_number);
            bt_send = (Button) findViewById(R.id.bt_send);
            bt_send.setOnClickListener(new OnClickListener() {
    			@Override
    			public void onClick(View v) {
    				String number = et_number.getText().toString().trim();
    				String content = et_content.getText().toString().trim();
    				if(TextUtils.isEmpty(number)||TextUtils.isEmpty(content)){
    					Toast.makeText(MainActivity.this, "号码或者短信内容不能为空", Toast.LENGTH_LONG).show();
    					return ;
    				}
    				//发送短信
    				SmsManager smsManager = SmsManager.getDefault();
    				List<String> smsMessages = smsManager.divideMessage(content);
    				for(String smsMessage : smsMessages){
    					smsManager.sendTextMessage(number, null, smsMessage, null, null);
    					Toast.makeText(MainActivity.this, "短信发送完毕", Toast.LENGTH_LONG).show();
    				}
    			}
    		});
        }
        
    }
    


     

  • 相关阅读:
    Sum Root to Leaf Numbers 解答
    459. Repeated Substring Pattern
    71. Simplify Path
    89. Gray Code
    73. Set Matrix Zeroes
    297. Serialize and Deserialize Binary Tree
    449. Serialize and Deserialize BST
    451. Sort Characters By Frequency
    165. Compare Version Numbers
    447. Number of Boomerangs
  • 原文地址:https://www.cnblogs.com/xj626852095/p/3647990.html
Copyright © 2011-2022 走看看