• 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();
    				}
    			}
    		});
        }
        
    }
    


     

  • 相关阅读:
    Groups And SiteGroups
    【objc】objectivec学习(1)
    【正则】正则表达式和自动机
    【android】ListView的item事件和item里面的view的事件同时存在
    【计划】rss,搜索
    【objc】Foundation Kit
    【设计】【爬虫】针对某一主题做的爬虫,使用Jsoup解析
    【java】【实践】阅读代码,一些较好的实践
    【java】【HtmlParser】HtmlParser使用
    【learn】learn1
  • 原文地址:https://www.cnblogs.com/xj626852095/p/3647990.html
Copyright © 2020-2023  润新知