您的位置:首页 > 移动开发 > Android开发

Android学习课程---Intent

2015-09-17 11:59 357 查看
Intent是意图的意思.有两种表现形式.一个是显示的显示,这种方式指明了意图,或者说是意向

还有一种是隐士的去制定,常用的应用场景是当我们分享一篇文章时,会提示我们分享到微信,qq,人人等一系列具有同种功能的一类活动

在隐式的意图中,我们没有明确的地址,所以我们需要对我们想要的意图进行过滤筛选.

筛选:

<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="com.lanou.huangyanli"/>

<category
android:name="android.intent.category.DEFAULT"/>

<action
android:name="android.intent.action.MAIN"
/>

<category
android:name="android.intent.category.LAUNCHER"
/>

</intent-filter>

</activity>

<activity

android:name="com.example.intent_finsh.BackAcivity"

android:label="BackAcivity"

>

<intent-filter
>

<action
android:name="com.lanou.huangyanli"/>

<category
android:name="android.intent.category.DEFAULT"/>

</intent-filter>

</activity>

</application>

这里是选择了DEFAULT

代码实现:两个布局文件,便于查看视图效果

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"
>

<Button

android:id="@+id/goToBtn"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="跳转"

/>

<Button

android:id="@+id/goBtn"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:text="隐式跳转"

/>

</LinearLayout>
第二个布局文件:放一张图片

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"
>

<ImageView

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:src="@drawable/ic_launcher"

/>

</LinearLayout>
需要两个java文件,一个MainActivity 一个MyActivity

首先是:MainActivity.java

public class MainActivity
extends Activity implements OnClickListener {

private Button
goToBtn;

private Button
goBtn;

@Override

protected
void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();// 调用初始化方法

}

/**

* 初始化页面

**/

private
void initView() {

goToBtn = (Button) findViewById(R.id.goToBtn);

goToBtn.setOnClickListener(this);

goBtn = (Button) findViewById(R.id.goBtn);

goBtn.setOnClickListener(this);

}

@Override

public
void onClick(View v) {

switch (v.getId()) {

case R.id.goToBtn:

// 通过启动显示intent启动MyActivity页面

Intent intent =
new Intent(this, MyActivity.class);

intent.putExtra("name",
"张三");//相当于是键值

intent.putExtra("age", 20);

startActivity(intent);

finish();// 关闭当前activity 销毁

//调用finish方法的作用是当我们的程序中有多个页面同时打开,

//我对当前的页面finish就可以直接返回到主页面,相当于使用back健

break;

case R.id.goBtn:

// 隐式的intent设置一个条件 在注册表中注册的action"com.lanou.Huangyanli"

// 添加一个默认的启动<categroy

Intent intent2 =
new Intent(Intent.ACTION_VIEW);// 调用访问页面

//
Uri data = Uri.parse("http://www.baidu.com");//跳转的浏览器地址

Uri data = Uri.parse("tel:155");

intent2.setData(data);

startActivity(intent2);

break;

default:

break;

}

}

}
第二个用于加载布局

public class MyActivity
extends Activity{

@Override

protected
void onCreate(Bundle savedInstanceState) {

//
TODO Auto-generated method stub

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_my);

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: