部分代码是来自网络实例
源码在http://download.csdn.net/detail/hwj277/8274483
窗体设计
添加打电话权限:(AndroidMainfest.xml内容)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test2sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.CALL_PHONE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
也可以使用权限管理工具添加,如下图
代码编程:(MainActivity.java)
package com.example.test2sms;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
public class mysms implements OnClickListener {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView txt=(TextView)findViewById(R.id.textView1);
txt.setText("按下发短信按钮");
EditText et_phonenumber = (EditText)findViewById(R.id.EditText01);
String number = et_phonenumber.getText().toString();
String content ="1111d短信测试。!222...,,";//短信内容
String phone =number;//电话号码
SmsManager smsManager =SmsManager.getDefault();
List<String> texts =smsManager.divideMessage(content);
for(int i = 0;i<texts.size();i++){
String text =texts.get(i);
smsManager.sendTextMessage(phone,null, content, null, null);
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt1=(Button)findViewById(R.id.button1);
bt1.setOnClickListener(new mytel());
Button bt2=(Button)findViewById(R.id.button2);
bt2.setOnClickListener(new mysms());
}
public class mytel implements OnClickListener {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
TextView txt=(TextView)findViewById(R.id.textView1);
txt.setText("按下打电话按钮");
EditText et_phonenumber = (EditText)findViewById(R.id.editText1);
String number = et_phonenumber.getText().toString();
Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+number));
startActivity (intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}