刚学android不久,最近卫卫发给我了一个视频,是怎样制作android手机上的短信发送器,一方面我感觉很有意思,另一方可以陪着她一起,今天弄完了,特来总结一下,虽然说比较简单吧,但还是有总结的必要的。

短信发送器主要是用于android下的短信发送的,其主要的界面就是输入发送母的人的号码和要发的信息的内容,主要界面有两个TextView和两个EditText还有一个Button(在import android.widget.*),下面是布局文件(layout):

<?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" >

<TextView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/number"

/>

<EditText

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/number"

/>

<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/context"

/>

<Button

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/button"

android:id="@+id/button"

/>

</LinearLayout>

以上布局文件使用的string的值需要在value/string.xml进行设置,主要的代码如下:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="app_name">smsTest</string>

<string name="action_settings">Settings</string>

<string name="content">请输入短信内容</string>

<string name="number">请输入手机号</string>

<string name="button">发送信息</string>

<string name="success">发送成功</string>

</resources>

以上就是布局,可以到AVD中试试是否成功,但是我们要完成这个短信发送器还需要在主程序.java写上相应的处理。

package com.example.smstest;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.telephony.gsm.SmsManager;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast ;

import java.util.ArrayList;//以上是需要的包

@SuppressWarnings( "deprecation" )

public class MainActivity extends Activity {

private EditText numberText ;//这个变量时用来接受布局中输入的号码

private EditText contentText ;//这个变量时用来接受布局中输入的信息

@Override

protected void onCreate(Bundle savedInstanceState) {//此类程序会从这歌开始在执行

super.onCreate(savedInstanceState);//调用父类Activity中的onCreate方法

setContentView(R.layout.activity_main);//执行布局文件

numberText = (EditText) this.findViewById( R.id.number ) ; //接受布局中输入框中的号码

contentText = (EditText) this.findViewById(R.id.context) ;//接受布局中输入框中的信息

Button button = (Button) this.findViewById(R.id.button) ;//使用Button

button.setOnClickListener( new ButtonClick() ) ;//设置Button响应的内容

}

private final class ButtonClick implements View.OnClickListener

{

@Override

public void onClick( View arg0 ) {

String number = numberText.getText().toString() ;//接受布局中输入框中的号码

String content = contentText.getText().toString();

SmsManager manger = SmsManager.getDefault();//建立短信管理对象

ArrayList<String> texts=manger.divideMessage(content) ;//如果短信过长超过一条短信的最大值那么分为多条短信发送

for ( String text : texts ) {

manger.sendTextMessage(number, null, text, null, null) ;//发送消息(第一个是目的号码,第三个是消息内容)

}

Toast.makeText(getApplicationContext(), R.string.success, Toast.LENGTH_LONG ).show() ;

//使用Toast提示短信发送成功

}

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

}

这样就可以了,可以在两个模拟器中发送(汉字会乱码但手机不会)

但是这样仍然不能再手机中使用,因为手机中的使用的话是要有使用权限的比如允许其扣除话费,那么获得权限就需要修改/smsTest/AndroidManifest.xml中加一个权限的代码

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

package="com.example.smstest"

android:versionCode="1"

android:versionName="1.0" >

<uses-sdk

android:minSdkVersion="6"

android:targetSdkVersion="10" />

<application

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

<activity

android:name="com.example.smstest.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>

红体字是加上去的,其余是自动生成的,到此短信发送器OK了。

2014/04/03 14:44


更多相关文章

  1. android手机与arm平台实现无线视频监控
  2. Android布局之线性布局
  3. Android中View的绘制过程 onMeasure方法简述
  4. tablelayout布局内容居中的问题
  5. Android自定义view之EditText中显示提示文本,在提示文本后面可输
  6. Android中ListView动态加载数据
  7. 学习android的好博客
  8. 关于Android的一些布局
  9. Material Design(六)

随机推荐

  1. URI数据匹配
  2. 安卓学习笔记(十)服务Service
  3. 移动设备浏览器的缓存限制[转][Mobile Br
  4. LinearLayout不能显示全部内容
  5. Android杂识
  6. Android自定义控件之测量onMeasure
  7. Jenkins奇技淫巧 — Python乐固,多渠道打
  8. Android:Click,LongClick,Touch,KeyDown,KeyUp
  9. android jni 用c++使用opengles和egl实现
  10. Android开发调试技巧