android制作简单的短信发送器

来源:互联网 发布:九章算法班视频 编辑:程序博客网 时间:2024/04/29 11:46

Android中制作简单的短信发送器的方法

首先现在布局文件中进行布局

下图是我做的一个简单的布局

 

其代码为

<LinearLayout  xmlns:android=”http=//schemas.android.com/apk/res/android”

  Xmlns:tools=”http://schemas.android.com/tools

Android:layout_width=”match_parent”

Android:layout_height=”match_parent”

Android:orientation=”vertical”

Android:context=”.MainActivity” >

 

<TextView

   Android:layout_width=”match_parent”

   Android:layout_height=”warp_parenr”

   Android:text=”请输入号码”>

<EditText

Android:id=”@+id/et1”

   Android:layout_width=”match_parent”

   Android:layout_height=”wrap_parent”

   Android:ems=”10”

   Android:inputType=”phone”>

<TextView

   Android:layout_width=”match_parent”

   Android:layout_height=”warp_parent”

   Android:text=”请输入短信内容”

<EditText

   Android:id=”@+id/et2”

   Android:layout_width=”match_parent”

   Android:layout_hr=eight=”warp_parent”

   Android:inputType=”textMultiLine”

   Android:lins=”6”>

 <Button

        android:id="@+id/button1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="发送短信" />

</LinearLayout>

   之后在MainActivity.java中编辑代码

     class MainActivity  extends Activity{

private  EditText et1,et2;

protexted void OnCreate(Bundle savedInstanceState){

super.OnCreate(saveInstanceState);

setContentView(R.layout.activity_main);

et1 =(EditText)findViewById(R.id.et1);

et2 =(EditText)findViewById(R.id.et2);

Button button1 = (button)findViewById(R.id.button1);

Button1.setOnClickListener(new OnClick());

      }

  class  OnCllck  implements  OnClickListener{

              public  void OnClick(View w){

             String phone = tv1.getText().toString().trim();            //去掉字符串两边的空格

       String textMultiLine = tv2.getText().toString();

if(“”.equals(phone)){                                       //如果号码为空

Toast.makeText(MianActivity.this,号码不能为空,0).show();

}

ifelse(“”.equals(textMultiLine)){                    //如果字符串为空

Toast.makeText(MianActivity.this,内容不能为空,0).show();

}

else{

SmsManager smsManager = SmsManager.getDefault();                                 //获取对象

smsManager.sandTextManager(phone,null, textMultiLine, null, null);

}

         }

   }

}

最后添加权限

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

 

1 0