Android第二个功能:短信发送
16lz
2021-01-23
核心代码四部分:
1、Layout
2、String值
3、MainActivity
4、给项目添加使用授权
<?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" ><RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" > <TextView android:layout_width="100dp" android:layout_height="wrap_content" android:text="@string/number" android:id="@+id/numberLabel" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/number" android:layout_toRightOf="@id/numberLabel" android:layout_alignTop="@id/numberLabel" android:layout_marginLeft="5px" /> </RelativeLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/content" /> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:minLines="3" android:id="@+id/content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button" android:id="@+id/button" /></LinearLayout>
<?xml version="1.0" encoding="utf-8"?><resources> <string name="hello">Hello World, SecondSmsActivity!</string> <string name="app_name">短信发送器</string> <string name="number">请输入手机号</string> <string name="content">请输入短信内容</string> <string name="button">发送短信</string><string name="success">发送成功</string></resources>
package com.dcj;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private EditText numberText; private EditText contentText; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); numberText = (EditText) this.findViewById(R.id.number); contentText = (EditText) this.findViewById(R.id.content); Button button = (Button) this.findViewById(R.id.button); button.setOnClickListener(new ButtonClickListener()); } private final class ButtonClickListener implements View.OnClickListener {public void onClick(View v) {String number = numberText.getText().toString();String content = contentText.getText().toString();SmsManager manager = SmsManager.getDefault();ArrayList<String> texts = manager.divideMessage(content);for (String text : texts) {manager.sendTextMessage(number, null, text, null, null);}Toast.makeText(MainActivity.this, R.string.success, Toast.LENGTH_SHORT).show();finish();} }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.dcj" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".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> <uses-permission android:name="android.permission.SEND_SMS"/></manifest>
更多相关文章
- Android核心分析(21)----Android应用框架之AndroidApplication
- Android之实现手机号码拦截
- Android 获取手机号
- Android核心基础(二)
- Android实现输入手机号时自动添加空格
- Android核心基础(七)
- Android手机号、串号获取