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


     

  • 相关阅读:
    冒泡排序和选择排序的根本差别在哪里?
    选择排序的3种语言实现方法(C java python)
    OVS中arp响应的流表的实现
    python 输出语句的写法
    haproxy + keepalived 实现网站高可靠
    nginx + keepalived 实现高可靠web网站
    实践:配置keepalived实现主备热备份功能
    Another app is currently holding the yum lock; waiting for it to exit 解决方法
    举例:使用XML库的方式,实现RPC通信
    openvswitch 2.7 安装过程记录 总结
  • 原文地址:https://www.cnblogs.com/xj626852095/p/3647990.html
Copyright © 2011-2022 走看看