package com.itarchy.demo;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button btnSendValue;
private Button btnSendBundle;
private Button btnCtN;
private Button btnIntent;
private Button btnCall;
private Button btnSendmms;
private Button btnIinternet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1、Intent传值:直接的键值对形式
btnSendValue = (Button) this.findViewById(R.id.btn_send_intent);
btnSendValue.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 设置启动源和启动目标
Intent intent = new Intent(MainActivity.this,
IntentActivity.class);
// Intent绑定数据
intent.putExtra("Name", "猪");
intent.putExtra("Age", 5);
// 启动Activity
startActivity(intent);
}
});
// 2、通过Bundle对象封装数据:
btnSendBundle = (Button) this.findViewById(R.id.btn_send_bundle);
btnSendBundle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 设置启动源和启动目标
Intent intent = new Intent(MainActivity.this,
BundleActivity.class);
// 新建Bundle对象
Bundle bundle = new Bundle();
bundle.putString("Name", "猫");
bundle.putInt("Age", 3);
intent.putExtra("toBundle", bundle);
// 启动Activity
startActivity(intent);
}
});
// 3、通过ComponentName启动Activity:
btnCtN = (Button) this.findViewById(R.id.btn_send_ctn);
btnCtN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 设置启动源和启动目标
Intent intent = new Intent();
ComponentName cn = new ComponentName(MainActivity.this,
CtnActivity.class);
intent.setComponent(cn);
// 启动Activity
startActivity(intent);
}
});
// 4、隐式Intent
btnIntent = (Button) this.findViewById(R.id.btn_intent);
btnIntent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 设置启动源和启动目标
Intent intent = new Intent();
intent.setAction("com.demo");
// 启动Activity
startActivity(intent);
}
});
// 5、:系统内置的Intent"实现拨打电话
btnCall = (Button) this.findViewById(R.id.btn_call);
btnCall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_DIAL); // 隐式Intent
// ACTION_DIAL ACTION_CALL
Uri uri = Uri.parse("tel:13313433672");
intent.setData(uri);
startActivity(intent);
}
});
// 6、:系统内置的Intent"实现发送信息
btnSendmms = (Button) this.findViewById(R.id.btn_sendmms);
btnSendmms.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Uri uri = Uri.parse("tel:13720012546");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
it.putExtra("sms_body", "The SMS text");
it.setType("vnd.android-dir/mms-sms");
startActivity(it);
}
});
// 7、:系统内置的Intent"实现访问网页
btnIinternet = (Button) this.findViewById(R.id.btn_internet);
btnIinternet.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 访问网页
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http://www.baidu.com");
intent.setData(uri);
startActivity(intent);
}
});
}
}
<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:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Intent的功能" />
<Button
android:id="@+id/btn_send_intent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:clickable="false"
android:alpha="0.5"
android:text="Intent传值" />
<Button
android:id="@+id/btn_send_bundle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:longClickable="false"
android:layout_below="@+id/btn_send_intent"
android:text="通过Bundle对象封装" />
<Button
android:id="@+id/btn_send_ctn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_send_bundle"
android:text="ComponentName启动Activity" />
<Button
android:id="@+id/btn_intent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_send_ctn"
android:text="隐式Intent" />
<Button
android:id="@+id/btn_call"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_intent"
android:text="系统内置Intent实现拨打电话 " />
<Button
android:id="@+id/btn_sendmms"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_call"
android:text="系统内置Intent实现发送信息" />
<Button
android:id="@+id/btn_internet"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_sendmms"
android:text="系统内置Intent实现访问网页" />
</RelativeLayout>
package com.itarchy.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class IntentActivity extends Activity {
private TextView tvValues;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent_main);
tvValues = (TextView) this.findViewById(R.id.tv_values);
// 获取激活当前Activity的意图对象
Intent intent = this.getIntent();
String name = intent.getStringExtra("Name");
int age = intent.getIntExtra("Age", 0);
Log.i("Tag", "name=" + name);
Log.i("Tag", "age=" + age);
tvValues.setText("name=" + name + ",age=" + age);
}
}
<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:id="@+id/tv_values"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="NUll" />
</RelativeLayout>
package com.itarchy.demo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class BundleActivity extends Activity {
private TextView tvValues;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent_main);
tvValues = (TextView) this.findViewById(R.id.tv_values);
// 获取传过来的Intent对象
Intent i = this.getIntent();
// 从Intent对象里获取绑定的Bundle
Bundle bundle = i.getBundleExtra("toBundle");
// 从Bundle对象获取封装的数据
String name = bundle.getString("Name");
int age = bundle.getInt("Age");
tvValues.setText("name=" + name + ",age=" + age);
}
}
<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:id="@+id/tv_values"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="NUll" />
</RelativeLayout>
package com.itarchy.demo;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Toast;
public class HitActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 关键词:toast 自定义
// 2、http://blog.youkuaiyun.com/wangjia55/article/details/8139759
// Toast.makeText(getApplicationContext(), "隐式Intent跳转成功",
// Toast.LENGTH_LONG).show();
Toast myToast = Toast.makeText(getApplicationContext(), "隐式Intent跳转成功",
Toast.LENGTH_LONG);
myToast.setGravity(Gravity.CENTER, 0, 0);
myToast.show();
}
}
package com.itarchy.demo;
import android.app.Activity;
import android.os.Bundle;
public class CtnActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}