搞了这么就的android了,打电话,发短信,发邮件这些接口都没有弄过,惭愧啊,所以今天看了发短信的程序:
原文连接:
http://blog.csdn.net/wenlin56/archive/2010/12/13/6074206.aspx
http://blog.csdn.net/xjanker2/archive/2010/12/13/6074344.aspx
布局文件:
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/accepter"
/>
<EditText android:id="@+id/number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content"
/>
<EditText android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="80dip"
/>
<Button android:id="@+id/send"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/send"/>
</LinearLayout>
程序:
package cn.edu.wtu;
import java.util.List;
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 SMS extends Activity implements OnClickListener{
/** Called when the activity is first created. */
// declare
private EditText mContentText;
private EditText mNumberText;
private Button mBtn;
private String mNumber;
private String mContent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// init
mNumberText = (EditText) findViewById(R.id.number);
mContentText = (EditText) findViewById(R.id.content);
mBtn = (Button) findViewById(R.id.send);
// bind listener
mBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==mBtn.getId()){
mNumber = mNumberText.getText().toString();
mContent = mContentText.getText().toString();
PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(), 0);
// android 2.0之前SmsManager 在android.telephone.gsm这个包中,2.0以后在android.telephone这个包中
SmsManager smsManager = SmsManager.getDefault();
// 安照一条短信的最大容量来拆分成多条短信
List<String> texts = smsManager.divideMessage(mContent);
for (String text : texts) {
smsManager.sendTextMessage(mNumber, null, text, pi, null);
}
Toast.makeText(this, R.string.ok, Toast.LENGTH_LONG).show();
}
}
}
短信管理器 : SmsManager
1. 在 Android 2.0 以前 应该使用 android.telephony.gsm.SmsManager
之后应该用 android.telephony.SmsManager;
注意是在Application标签之外。