package com.lidaochen.test; import android.content.Intent; import android.os.Bundle; import android.provider.Telephony; import android.support.annotation.Nullable; 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_number; private EditText et_content; // 当我们开启的Activity页面关闭的时候,就调用下面这个方法 @Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { /* if (requestCode == 1) { // 代表请求ContactActivity这个页面的数据 String phone = data.getStringExtra("phone"); et_number.setText(phone); } else if(requestCode == 2) { // 代表请求SmsTemplateActivity这个页面的数据 String smsContent = data.getStringExtra("smsContent"); et_content.setText(smsContent); }*/ if (resultCode == 10) { // 说明数据是从ContactActivity页面返回的 String phone = data.getStringExtra("phone"); et_number.setText(phone); } else if(resultCode == 20) { // 说明数据是从SmsTemplateActivity页面返回的 String smsContent = data.getStringExtra("smsContent"); et_content.setText(smsContent); } super.onActivityResult(requestCode, resultCode, data); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 找到EditText控件 et_number = (EditText)findViewById(R.id.et_number); et_content = (EditText)findViewById(R.id.et_content); } // 点击按钮 跳转到ContactActivity页面 public void add(View v) { Intent intent = new Intent(this, ContactActivity.class); // startActivity(intent); // 如果点击按钮 开启了另外一个Activity,并且当开启的这个Activity关闭的时候,我想要这个开启Activity的数据,用下面的方法开启Activity startActivityForResult(intent, 1); } // 点击按钮 跳转到SmsTemplateActivity页面 public void insertsms(View v) { Intent intent = new Intent(this, SmsTemplateActivity.class); startActivityForResult(intent, 2); // startActivity(intent); } public void send(View v) { // 获取发送短信的内容 String number = et_number.getText().toString().trim(); String content = et_content.getText().toString().trim(); // 获取到SmsManager的实例 SmsManager smsManager = SmsManager.getDefault(); ArrayList<String> divideMessage = smsManager.divideMessage(content); for (String div : divideMessage) { /** * destinationAddress 发送给谁 * scAddress 服务中心号码 * * text 要发送的内容 */ smsManager.sendTextMessage(number, null, div, null, null); } } }
package com.lidaochen.test; import android.content.Intent; import android.support.v7.app.AppCompatActivity; 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; public class ContactActivity extends AppCompatActivity { private List<Person> lists; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact); // 找到ListView控件 ListView lv = (ListView)findViewById(R.id.lv); // 准备ListView要显示的数据,这里模拟点假的数据 lists = new ArrayList<Person>(); for (int i = 0; i < 20; i++) { Person p = new Person(); p.setName("张三" + i); p.setPhone("1100" + i); lists.add(p); } // 展示数据 lv.setAdapter(new MyAdaper()); // 给ListView设置点击事件 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 = new Intent(); intent.putExtra("phone", phone); // 把结果返回给调用者 setResult(10, intent); // 关闭当前页面 finish(); } }); } private class MyAdaper 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.contact_item, null); } else { view = convertView; } // 找到我们在item中定义的控件,用来显示数据 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; } } }
package com.lidaochen.test; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; public class SmsTemplateActivity extends AppCompatActivity { String[] objects = {"我在吃饭,请稍后联系","我在开会,请稍后联系","我在上课,请稍后联系","我在打代码,请稍后联系","我在约会,请稍后联系"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sms_template); // 找到ListView控件 ListView lv = (ListView)findViewById(R.id.lv); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.smstemplate_item, objects); // 显示数据 lv.setAdapter(adapter); // 设置ListView的条目点击事件 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // 取出点击条目的数据 String smsContent = objects[position]; // 把smsContent返回调用者 Intent intent = new Intent(); intent.putExtra("smsContent", smsContent); setResult(20, intent); // 关闭页面 finish(); } }); } }
smstemplate_item.xml
<?xml version="1.0" encoding="utf-8"?> <TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textColor="#00ff5d" android:textAppearance="?android:attr/textAppearanceLarge" />