activity间数据传递实例_使用系统短信发送器发送数据

一、项目目录结构

二、activity_main.xml界面

三、activity_main.xml代码

<RelativeLayout 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"
    tools:context="com.zgs.SendSMS.MainActivity" >

    <ListView
        android:id="@+id/lv_sms"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>

</RelativeLayout>
四、item_sms.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:textColor="#000000" >

</TextView>

五、AndroidManifest.xml代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zgs.SendSMS"
    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>
    </application>

</manifest>

六、MainActivity.java代码

package com.zgs.SendSMS;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
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 MainActivity extends Activity {

	//[0]封装到了数组中
	String objects[] = {"今天的风儿轻柔无比,今天的花儿香飘万里;今天的鸟儿十分欢喜,今天的云儿满载笑意;今天的事儿万分顺利,今天的人儿如此甜蜜。所有美...",
			"丫头,生活是你自己的,你哭它就对你哭,你笑它就对你笑。转眼,又是一年,你的生日即将来到。今年,还是少不了我对你的祝福,我忍不住...",
			"世界上最动听的声音,是妈妈声声的呼唤;世界上最温暖的笑容,是妈妈温暖的笑脸。妈妈,原谅生日时我不能陪在您身边,在这个日子里,我...",
	"今天是你的生日,祝你:发财势头如快马加鞭,一日千里;发展速度如滔滔江水,势不可挡;好事发生如雨后春笋,络绎不绝;祝福发送如比赛..."}; 

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		//[1]获取关心的ListView控件
		ListView lv_sms = (ListView) findViewById(R.id.lv_sms);
		//[2]创建adapter数据适配器
		ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), R.layout.item_sms, objects);
		//[3]设置adapter数据适配器到ListView控件
		lv_sms.setAdapter(adapter);
		//[4]设置ListView控件条目监听事件
		lv_sms.setOnItemClickListener(new OnItemClickListener() {
			//当条目被点击的时候调用
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//[5]获取点击条目的内容
				String content = objects[position];
				//[6]创建意图对象
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_SENDTO);
				intent.setData(Uri.parse("smsto:"+110)); //接收短信人号码
				intent.putExtra("sms_body", content); //短信内容
				startActivity(intent);
				
			}
			
		});
		

	}

}
七、 操作演示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值