
package zhang.example;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class itemSMS extends Activity {
private EditText phone;
private EditText sms;
private Button ok;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
phone=(EditText)findViewById(R.id.phone);
sms=(EditText)findViewById(R.id.sms);
ok=(Button)findViewById(R.id.ok);
sms.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sms.setText("");
}
});
ok.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String pnum=phone.getText().toString();
String body=sms.getText().toString();
SmsManager smsManager=SmsManager.getDefault();
if(with70(body)){
try{
PendingIntent mPI= PendingIntent.getBroadcast(itemSMS.this, 0, new Intent(), 0);
smsManager.sendTextMessage(pnum, null, body, mPI,null);
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
Toast.makeText(itemSMS.this,"ok",Toast.LENGTH_SHORT).show();
phone.setText("");
sms.setText("");
}else{
if(with70(body)==false){
Toast.makeText(itemSMS.this, "字数太多", Toast.LENGTH_SHORT).show();
}
}
}});
}
public boolean with70(String bodylength){
if(bodylength.length()<=70){
return true;
}else
{
return false;
}
}
}<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:id="@+id/layout1">
<TextView android:id="@+id/text" android:layout_width="wrap_content"
android:layout_height="fill_parent" android:text="收信人"
android:gravity="center_vertical" />
<EditText android:id="@+id/phone" android:layout_width="match_parent"
android:layout_height="fill_parent"
android:inputType="phone" />
</LinearLayout>
<EditText
android:id="@+id/sms"
android:layout_width="fill_parent"
android:layout_height="200dip"
android:text="输入短信内容!"
/>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal"
android:id="@+id/layout2">
<Button
android:id="@+id/ok"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="发送短消息"
/>
</LinearLayout>
</LinearLayout>
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

本文介绍了一个简单的短信发送应用开发过程,包括布局设计、组件使用、短信发送逻辑实现及权限申请等关键步骤。
3656

被折叠的 条评论
为什么被折叠?



