/*
* Intent的基本作用
* 一个Intent对象包含一组信息
* 1.Component name 指定启动那一个activity
* 2.Action 指定另外一个action要做什么
* 3.Data 是一个url
* 4.Categroy
* 5.Extras存放键值对
* 6.Flags
*
*
*/
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个Activity" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转"/>
</RelativeLayout>
MainActivity.java
package com.example.android012;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button but1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1 = (Button) findViewById(R.id.button1);
but1.setOnClickListener(new MyButtonOnClickListion());
}
class MyButtonOnClickListion implements OnClickListener {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
Intent intent = new Intent();
// 传入(名字,值)
intent.putExtra("testintent", "123");
intent.setClass(MainActivity.this, Second.class);
startActivity(intent);
}
}
second.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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个activity" />
</LinearLayout>
second.java
public class Second extends Activity {
private TextView text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO 自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.sencon);
Intent intent=getIntent();
String value=intent.getStringExtra("testintent");
text2=(TextView)findViewById(R.id.textView1);
text2.setText(value);
}
}
AndroidMainfest.xml清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android012"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android012.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
android:name=".Second"
android:label="second" >
</activity>
</application>
</manifest>
方法二:发短信
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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个Activity" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="跳转"/>
</RelativeLayout>
java文件
package com.example.android012;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button but1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but1 = (Button) findViewById(R.id.button1);
but1.setOnClickListener(new MyButtonOnClickListion());
}
class MyButtonOnClickListion implements OnClickListener {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
// Intent intent = new Intent();
// // 传入(名字,值)
// intent.putExtra("testintent", "123");
// intent.setClass(MainActivity.this, Second.class);
// startActivity(intent);
Uri uri = Uri.parse("smsto://18209225***");// uri为手机号吗
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms", "124");
startActivity(intent);
}
}
}