activity间数据传递实例_自定义短信发送器
来源:互联网 发布:linux hadoop搭建 编辑:程序博客网 时间:2024/05/20 06:55
一、项目目录结构
二、activity_main.xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.zgs.SendSMSByCustom.MainActivity" > <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <EditText android:id="@+id/et_number" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入联系人号码" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@id/et_number" android:layout_alignParentRight="true" android:onClick="add" android:text="+" /> </RelativeLayout> <EditText android:id="@+id/et_sms_content" android:layout_width="match_parent" android:layout_height="250dp" android:gravity="top" android:hint="请输入发送短信的内容" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="insert" android:text="短信模板" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendSMS" android:text="发送短信" /></LinearLayout>三、activity_contact.xml代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv_contact" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView></LinearLayout>四、activity_template.xml代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/lv_template" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> </LinearLayout>五、item_contact.xml代码
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <TextView android:id="@+id/tv_name" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="张三" android:textColor="#000000" android:textSize="25sp" /> <TextView android:id="@+id/tv_phone" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textColor="#000000" android:textSize="25sp" android:text="110" /> </LinearLayout>六、item_template.xml代码
<?xml version="1.0" encoding="utf-8"?><TextView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:text="TextView" android:textColor="#000000" android:textAppearance="?android:attr/textAppearanceLarge" > </TextView>七、AndroidManifest.xml代码
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.zgs.SendSMSByCustom" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" /> <uses-permission android:name="android.permission.SEND_SMS"/> <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> <!-- 在清单文件中配置新加的activity --> <activity android:name="com.zgs.SendSMSByCustom.ContactActivity" > </activity> <activity android:name="com.zgs.SendSMSByCustom.TemplateActivity" > </activity> </application></manifest>八、MainActivity.java代码
package com.zgs.SendSMSByCustom;import java.util.ArrayList;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.telephony.SmsManager;import android.view.View;import android.widget.EditText;public class MainActivity extends Activity {private EditText et_number;private EditText et_sms_content;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//[1]获取我们关心的控件et_number = (EditText) findViewById(R.id.et_number);et_sms_content = (EditText) findViewById(R.id.et_sms_content);}//点击+按钮跳转到联系人页面public void add(View v){//[1]创建意图对象,getApplicationContext()也可以获取context对象Intent intent = new Intent(getApplicationContext(), ContactActivity.class);//[2]开启Activity//startActivity(intent);//[3]☆☆☆☆ 如果一个页面开启另外一个页面 并且当开启的这个页面关闭的时候 还要返回数据 使用下面这个方法开启ActivitystartActivityForResult(intent, 1);}//点击按钮 跳转到短信模板页面public void insert(View v){Intent intent = new Intent(this,TemplateActivity.class);//☆☆☆开启Activity有2种方式 //(1)如果想要开启的Activity的界面的数据 用 startActivityForResult();//(2)如果就是简简单单页面的跳转 就用startActivity()startActivityForResult(intent, 2);}//点击按钮实现发送短信的逻辑 public void sendSMS(View v){String number = et_number.getText().toString().trim();String content = et_sms_content.getText().toString().trim();//[1]获取smsManager的实例 SmsManager smsManager = SmsManager.getDefault();//[1.1]如果短信内容过过多 发不出去 分条发送 ArrayList<String> divideMessages = smsManager.divideMessage(content);for (String div : divideMessages) {//[2]发送短信数据 smsManager.sendTextMessage(number, null, div, null, null);}}//当我们开启的Activity的页面关闭的时候这个方法会调用@Overrideprotected void onActivityResult(int requestCode, int resultCode, 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_sms_content.setText(smscontent);}/*if (resultCode == 10) {//说明数据是由 ContactActivity返回 String phone = data.getStringExtra("name");et_number.setText(phone);}else if (resultCode == 20) {//说明数据是由SmsTemplateActivity返回 String smscontent = data.getStringExtra("smscontent");et_sms_content.setText(smscontent);} */super.onActivityResult(requestCode, resultCode, data);}}九、ContactActivity.java代码
package com.zgs.SendSMSByCustom;import java.util.ArrayList;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.AdapterView.OnItemClickListener;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;public class ContactActivity extends Activity {private ArrayList<Contact> contactLists;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//加载布局setContentView(R.layout.activity_contact);//[1]找到lv控件 ListView lv_contact = (ListView) findViewById(R.id.lv_contact);//[2]我想把手机当中 联系人的数据展示到listview上 等讲内容提供者在去获取真实数据 contactLists = new ArrayList<Contact>(); for (int i = 0; i < 10; i++) {Contact contact = new Contact();contact.setName("张三"+i);contact.setPhone("1869100892"+i);contactLists.add(contact);}//[3]展示数据 设置数据适配器 lv_contact.setAdapter(new MyAdapter());//[4]给listview的条目设置点击事件 lv_contact.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {//[5]取出我们点中条目的数据 String phone = contactLists.get(position).getPhone();System.out.println("phone:"+phone);//[6]把phone 返回给第一个页面 Intent intent = new Intent();intent.putExtra("phone", phone);//[7]把数据返回给调用者 Call this to set the result that your activity will return to its caller. setResult(10, intent);//[8]关闭当前Activity mainActivity的onActivityResult方法就会执行finish();}});}//创建数据适配器 private class MyAdapter extends BaseAdapter{@Overridepublic int getCount() {return contactLists.size();}@Overridepublic Object getItem(int position) {return contactLists.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view;if (convertView == null) {view = View.inflate(getApplicationContext(), R.layout.item_contact, 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(contactLists.get(position).getName());tv_phone.setText(contactLists.get(position).getPhone());return view;}}}十、TemplateActivity.java代码
package com.zgs.SendSMSByCustom;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.ArrayAdapter;import android.widget.ListView;public class TemplateActivity extends Activity {String objects[] = {"我在开会,请稍后联系","我在吃饭,请稍后联系","我在打代码,请稍后联系","我在开车,请稍后联系","我在约会,请稍后联系"};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_template);//[1]找到lv控件ListView lv = (ListView) findViewById(R.id.lv_template);//[2]创建数据适配器 ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.item_template, objects);//[3]把数据展示到listview上 lv.setAdapter(adapter);//[4]给lv设置点击事件 lv.setOnItemClickListener(new OnItemClickListener() {@Overridepublic void onItemClick(AdapterView<?> parent, View view,int position, long id) {//[5]获取我们点中条目的数据 String smscontent = objects[position];//[6]把这个数据返回给调用者 Intent intent = new Intent();intent.putExtra("smscontent", smscontent);//[7]通过这个方法把数据返回给调用者 setResult(20, intent);//[8]要记得调用finish finish();}});}}十一、操作演示
0 0
- activity间数据传递实例_自定义短信发送器
- activity间数据传递实例_使用系统短信发送器发送数据
- activity间数据传递实例_人品计算器
- Android自定义发送短信实例
- 返回Activity时传递数据(如发送短信时引用联系人)
- 003_短信发送器
- Activity间传递数据
- activity间传递数据
- Activity间传递数据
- Activity间传递数据
- activity间传递数据
- 发送短信之获取联系人案例(intent传递数据)
- Activity之间传递ArrayList<自定义类>数据
- android发送短信实例
- 发送短信实例
- java发送短信实例
- 自定义发送短信,打电话
- Activity间的数据传递
- 高斯牛顿法,LM法
- ubuntu安装Theano+cuda
- 提示框:Dialog的用法 与 封装
- 【操作系统】“哲学家进餐”问题
- FreeCodeCamp之前端开发学习笔记1
- activity间数据传递实例_自定义短信发送器
- 破解压缩文件密码rarcrack
- Canvas笔记——动态添加视图树
- strlen和sizeof
- PHP设计模式系列 - 建造者模式
- 交叉验证原理及Spark MLlib使用实例(Scala/Java/Python)
- 最强大脑解密:记忆法和记忆力
- DMZ主机
- JMeter资料整理