网站首页 语言 会计 电脑 医学 资格证 职场 文艺体育 范文
当前位置:书香门第 > 范文 > 文学

Android实现发短信和彩信功能

栏目: 文学 / 发布于: / 人气:2.64W

首先 activity_ 布局:

Android实现发短信和彩信功能

<RelativeLayout xmlns:android="/apk/res/android"

xmlns:tools="/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context="ctivity" >

<TextView

android:id="@+id/phone_lable"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_marginTop="23dp"

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

<EditText

android:id="@+id/edit_phone"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@+id/phone_lable"

android:layout_centerHorizontal="true"

android:ems="10"

android:inputType="phone" >

<TextView

android:id="@+id/sms_lable"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/edit_phone"

android:layout_below="@+id/edit_phone"

android:layout_marginTop="22dp"

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

<Button

android:id="@+id/But_sms"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/edit_sms"

android:layout_alignParentBottom="true"

android:layout_marginBottom="14dp"

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

<EditText

android:id="@+id/edit_sms"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_above="@+id/But_sms"

android:layout_alignLeft="@+id/sms_lable"

android:ems="10"

android:inputType="textMultiLine" />

然后在 中添加 发送短信的权限:

接下来就是SmsActivity 中 主要代码 的'实现 其功能 :

public class SmsActivity extends Activity {

// 声明控件对象

private Button butten_sms;

private EditText edit_phone;

private EditText edit_sms;

@Override

protected void onCreate(Bundle savedInstanceState) {

eate(savedInstanceState);

//设置显示视图

setContentView(vity_sms);

//获取按钮组件

butten_sms=(Button) findViewById(_sms);

//获取输入框组件

edit_phone = (EditText) findViewById(_phone);

edit_sms = (EditText) findViewById(_sms);

//注册按钮事件

butten_nClickListener(new ickListener() {

@Override

public void onClick(View arg0) {

/**

* 发彩信

*/

//获取电话

String phone_num = edit_ext()ring();

//创建意图对象

Intent intent = new Intent(ON_SEND);

xtra(A_STREAM, Uri.parse("file:///sdcard/a.jpg"));

xtra("address", phone_num);

xtra("exit_on_sent", true);

xtra("subject", "subject:I love you");

xtra("sms_body", "content ::XXXX");

intent.setType("image/jpeg");

startActivity(intent);

/*// 获取短信管理器对象

SmsManager smsManager = efault();

// 意图对象

PendingIntent pendingIntent = roadcast(

, 0, new Intent(), 0);*/

}

});

}

//发短信 功能

public void send(){

//获取号码

String phone_Num = edit_ext()ring();

//获取发送的短信

String phone_sms = edit_ext()ring();

//获取信息管理器对象

SmsManager smsmanager = efault();

//意图对象

PendingIntent pendingIntent = roadcast(, 0, new Intent(), 0);

//判断 信息长度 大于 70则分两条发送。。默认是70

ArrayListcontents = smsmanager

deMessage(phone_sms);

for (String c : contents) {

// 发送信息的操作

TextMessage(phone_Num, null, c,

pendingIntent, null);

}

/*//发送信息操作

TextMessage(phone_Num, null, phone_sms, pendingIntent, null);*/

//吐司的效果

Text(, "短信发送成功",TH_LONG)();

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

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

getMenuInflater()ate(, menu);

return true;

}

}