网站建设资讯

NEWS

网站建设资讯

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


当前题目:android手机短信发送
文章网址:http://cdweb.net/article/iidscj.html