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);
}
还有一种是隐士的去制定,常用的应用场景是当我们分享一篇文章时,会提示我们分享到微信,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);
}
相关文章推荐
- android自定义radiobutton样式文字颜色随选中状态而改变
- Android基本View控件
- Android之RecyclerView 实现真正的Gallery效果
- Android开发:Handler Runnable和Thread之间的区别和联系
- Xamarin.Android经验之谈
- Android之socket客户端
- android下访问已存在的sqlite数据库文件的办法
- Android 开发工具
- 原生android中对AsynckTask中实现方法的理解
- Android发送短信并监测发送状态
- android四大组件之Service详解
- 在Android Studio的drawable文件下创建XML文件
- Android-onInterceptTouchEvent()和onTouchEvent()总结---非常棒棒哒
- android添加wifi热点管理黑白名单
- Android之gallery 常见2种使用方法和3D效果总结
- Android中Activity四种启动模式和taskAffinity属性详解
- Android之ListView
- android使用selector修改TextView中的字体颜色和背景颜色
- 常见Android开源框架使用笔记之——xUtils框架
- Android SDK 完整版