[1]当调用页面关闭的时候就会调用onActivityResult()方法
[2]要获取调用页面的返回结果,这需要使用startActivityForResult()方法
[3]当选择页面关闭时,需要将选择结果回传到主界面,这时需要使用setResult(intent);
[4]短信发送API SmsManager的使用
//Manifest
<manifest package="jacky.msmsend"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
intent-filter>
activity>
<activity android:name=".ContactActivity">activity>
<activity android:name=".InsertActivity">activity>
application>
<uses-permission android:name="android.permission.SEND_SMS">uses-permission>
manifest>
//获取SmsManager实例
SmsManager smsManager=SmsManager.getDefault();
//实现分条发送
ArrayList divdeMessages = smsManager.divideMessage(content);
for (String div:divdeMessages){
/**
* destinationAddress 接受者
* scAddress 服务中心
* text要发送的内容
* sentIntent deliveryIntent 接收成功或失败时的响应
*/
smsManager.sendTextMessage(phone,null,div,null,null);
}
//MainActivity
package jacky.msmsend;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.EditText;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private EditText et_phone;
private EditText et_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_phone = (EditText) findViewById(R.id.et_phone);
et_content = (EditText) findViewById(R.id.et_content);
}
//当调用页面关闭的时候就会调用该方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
String phone = data.getStringExtra("phone");
//设置显示数据
et_phone.setText(phone);
} else if (requestCode == 2) {
String smscontent = data.getStringExtra("smscontent");
et_content.setText(smscontent);
}
}
//添加按钮响应事件
public void add(View v){
//跳转到ContactActivity页面
Intent intent=new Intent(this,ContactActivity.class);
//startActivity(intent);
//要获取调用页面的返回结果,这需要使用startActivityForResult
startActivityForResult(intent, 1);
}
//插入按钮响应事件
public void insert(View v){
Intent intent=new Intent(this,InsertActivity.class);
startActivityForResult(intent,2);
}
//发送按钮响应事件
public void send(View v){
String phone=et_phone.getText().toString().trim();
String content=et_content.getText().toString().trim();
//获取SmsManager实例
SmsManager smsManager=SmsManager.getDefault();
//实现分条发送
ArrayList divdeMessages = smsManager.divideMessage(content);
for (String div:divdeMessages){
/**
* destinationAddress 接受者
* scAddress 服务中心
* text要发送的内容
* sentIntent deliveryIntent 接收成功或失败时的响应
*/
smsManager.sendTextMessage(phone,null,div,null,null);
}
}
}
//InsertActivity
package jacky.msmsend;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
/**
* 作者:Jacky
* 邮箱:[email protected]
* 时间:2016/2/11 16:40
*/
public class InsertActivity extends Activity {
String[] objects={"我在吃饭,请稍后联系","我在开会,请稍后联系","我在约会,请稍后联系",
"我在洗澡,请稍后联系","我在上课,请稍后联系"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_insert);
ListView lv = (ListView) findViewById(R.id.lv);
ArrayAdapter adapter=new ArrayAdapter(getApplicationContext(),R.layout.activity_insert_item,objects);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
String smsContent=objects[position];
Intent intent=new Intent();
intent.putExtra("smscontent",smsContent);
setResult(20, intent);
finish();
}
});
}
}
//ContactActivity
package jacky.msmsend;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* 作者:Jacky
* 邮箱:[email protected]
* 时间:2016/2/11 15:21
*/
public class ContactActivity extends Activity {
private List lists;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact);
//找到ListView控件
ListView lv = (ListView) findViewById(R.id.lv);
//创建一个List来存储数据
lists = new ArrayList();
//模拟假数据
for (int i = 0; i < 10; i++) {
Person person = new Person();
person.setName("小明" + i);
person.setPhone("110" + i);
lists.add(person);
}
//设置适配器
lv.setAdapter(new MyAdapter());
//设置点击事件
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
//获取选定的数据
String phone=lists.get(position).getPhone();
//将选定的数据放到Intent中
Intent intent=new Intent();
intent.putExtra("phone",phone);
//设置返回的结果
setResult(10,intent);
//关闭当前页面
finish();
}
});
}
class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
view = View.inflate(getApplicationContext(), R.layout.activity_contact_item, null);
} else {
view=convertView;
}
//找到控件
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
TextView tv_phone = (TextView) view.findViewById(R.id.tv_phone);
//显示数据
tv_name.setText(lists.get(position).getName());
tv_phone.setText(lists.get(position).getPhone());
return view;
}
}
}
//Person
package jacky.msmsend;
/**
* 作者:Jacky
* 邮箱:[email protected]
* 时间:2016/2/11 15:29
*/
public class Person {
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
private String name;
private String phone;
}