重庆分公司,新征程启航

为企业提供网站建设、域名注册、服务器等服务

android手机短信发送-创新互联

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

站在用户的角度思考问题,与客户深入沟通,找到济水街道网站设计与济水街道网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:成都做网站、网站设计、企业官网、英文网站、手机端网站、网站推广、申请域名、虚拟空间、企业邮箱。业务覆盖济水街道地区。

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

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

  android:layout_width="fill_parent"

  android:layout_height="fill_parent"

 android:orientation="vertical" >

  

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/number"

    />

  

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:id="@+id/number"

    />

  

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/content"

    />

  

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:minLines="3"

    android:id="@+id/context"

    />

  

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="@string/button"

    android:id="@+id/button"

   />

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

  smsTest

  Settings

  请输入短信内容

  请输入手机号

  发送信息

 发送成功

以上就是布局,可以到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 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;

 }

}

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

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

  package="com.example.smstest"

  android:versionCode="1"

  android:versionName="1.0" >

  

    android:minSdkVersion="6"

    android:targetSdkVersion="10" />

  

    android:allowBackup="true"

    android:icon="@drawable/ic_launcher"

    android:label="@string/app_name"

    android:theme="@style/AppTheme" >

    

      android:name="com.example.smstest.MainActivity"

      android:label="@string/app_name" >

      

        

        

      

    

  

   

红体字是加上去的,其余是自动生成的,到此×××OK了。

2014/04/03 14:44

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章名称:android手机短信发送-创新互联
分享路径:http://cqcxhl.cn/article/dpjpcp.html

其他资讯

在线咨询
服务热线
服务热线:028-86922220
TOP