Android短信发送器

短信发送器

因为应用要使用手机的短信服务,所以要在清单文件AndroidManifest.xml中添加短信服务权限:

<?xmlversion="1.0" encoding="utf-8"?>

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

      package="cn.itcast.sms"

      android:versionCode="1"

      android:versionName="1.0">

     略....

     <uses-sdk android:minSdkVersion=“4" />

    <uses-permissionandroid:name="android.permission.SEND_SMS"/>

</manifest>


短信发送器

界面布局:

<?xmlversion="1.0" encoding="utf-8"?>

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

    android:orientation="vertical“android:layout_width="fill_parent“android:layout_height="fill_parent" >

    <TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"

    android:text="@string/inputmobile"/>

   

    <EditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"

    android:id="@+id/mobile"/>

     

   <TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"

    android:text="@string/content"/>

   

    <EditTextandroid:layout_width="fill_parent"android:layout_height="wrap_content"

    android:minLines="3"

    android:id="@+id/content"/>

  

    <Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"

    android:text="@string/button"

    android:id="@+id/button"/>

</LinearLayout>

短信发送器

Activity主要代码:

 

      String mobile = mobileView.getText().toString();

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

       SmsManager smsManager =SmsManager.getDefault();

       PendingIntent sentIntent= PendingIntent.getBroadcast(SMSSender.this, 0, new Intent(), 0);

      //如果字数超过70,需拆分成多条短信发送                  

      List<String> msgs = smsManager.divideMessage(content);

      for(String msg : msgs){

          smsManager.sendTextMessage(mobile,null, msg, sentIntent, null);

       //最后二个参数为短信已发送的广播意图,最后一个参数为短信对方已收到短信的广播意图

       }

      Toast.makeText(SMSSender.this, "短信发送完成",Toast.LENGTH_LONG).show();


短信发送器

测试步骤:

 1>在Eclipse中运行此应用

 2>在Dos窗口中进入android SDK安装路径的tools目录,输入以下命令再开启一个Android模拟器:

  emulator -data itcast 

   注:itcast为用户数据存取文件,如果该文件不存在,默认在tools目录创建该文件

 

对应用进行单元测试

在实际开发中,开发android软件的过程需要不断地进行测试。而使用Junit测试框架,侧是正规Android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性。

第一步:首先在AndroidManifest.xml中加入下面红色代码:

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

      package="cn.itcast.action“android:versionCode="1“  android:versionName="1.0">

 <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">

        <uses-libraryandroid:name="android.test.runner" />

        ....

 </application>

 <uses-sdkandroid:minSdkVersion="6" />

 <instrumentationandroid:name="android.test.InstrumentationTestRunner"

 android:targetPackage="cn.itcast.action"android:label="Tests for My App" />

</manifest>

上面targetPackage指定的包要和应用的package相同。

第二步:编写单元测试代码(选择要测试的方法,右键点击“Run As”--“Android JunitTest” ):

importandroid.test.AndroidTestCase;

import android.util.Log;

public class XMLTest extendsAndroidTestCase {

       public voidtestSomething() throws Throwable {

            Assert.assertTrue(1+ 1 == 3);

       }

}

 

3>在短信发送器的手机号中输入上图现显的电话号码

 

注:目前Android系统对中文短信尚未支持,所以发送中文短信会有乱码,这个问题日后会被解决的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值