《android 学习》三、Intent 的概念及应用
2015-11-12 10:11
399 查看
通过 Intent 可以打开不同的 Activity
一、Intent 的显性启动
二、Intent 的隐性启动
通过设置配置文件,为Activity配置字符串,如下:
这里对BAty 进行了配置添加了一个intent-filter,其中的action 字符串标准为:
包名.intent.action.XXActivity
下面为测试隐性启动:
三、使用 Intent 浏览网页、拨打电话、打开相机,打开图库等
1.打开网页
2.拨打电话
3.打开相机
4.打开图库
5.打开蓝牙
等等……
package com.example.demo_1; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class AAty extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_aaty); findViewById(R.id.btnStartBAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(AAty.this,BAty.class); startActivity(intent); } }); } }
<LinearLayout 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:orientation="vertical" > <TextView android:id="@+id/tvInformation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这个是AAty" /> <Button android:id=< 4000 span class="hljs-value">"@+id/btnStartBAty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="启动BAty" /> </LinearLayout>
package com.example.demo_1; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class BAty extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_baty); findViewById(R.id.btnBack).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }
<LinearLayout 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:orientation="vertical"> <TextView android:id="@+id/tvInformation" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这个是BAty" /> <Button android:id="@+id/btnBack" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="返回" /> </LinearLayout>
一、Intent 的显性启动
//正常的Content 和 Class Intent intent = new Intent(AAty.this,BAty.class); startActivity(intent);
二、Intent 的隐性启动
通过设置配置文件,为Activity配置字符串,如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.demo_1" android:versionCode="1" android:versionName="1.0.0" > <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".AAty" android:label="@string/title_activity_aaty" android:launchMode="singleInstance"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".BAty" android:label="@string/title_activity_baty" > <intent-filter> <category android:name="android.intent.category.DEFAULT" /> <action android:name="com.example.demo_1.intent.action.BAty"/> </intent-filter> </activity> </application> </manifest>
这里对BAty 进行了配置添加了一个intent-filter,其中的action 字符串标准为:
包名.intent.action.XXActivity
<intent-filter> <category android:name="android.intent.category.DEFAULT" /> <action android:name="com.example.demo_1.intent.action.BAty"/> </intent-filter>
下面为测试隐性启动:
package com.example.demo_1; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class AAty extends Activity { private static final String ACTION = "com.example.demo_1.intent.action.BAty"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_aaty); findViewById(R.id.btnStartBAty).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(AAty.ACTION); startActivity(intent); } }); } }
三、使用 Intent 浏览网页、拨打电话、打开相机,打开图库等
1.打开网页
//打开链接 Intent it = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/xiaoarea")); startActivity(it); //打开本地网页 Intent intent = new Intent(); intent.setAction("android.intent.action.VIEW"); Uri CONTENT_URI_BROWSERS = Uri.parse("content://com.android.htmlfileprovider/sdcard/a.html"); intent.setData(CONTENT_URI_BROWSERS); intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity"); startActivity(intent); //还需要添加网络权限 <uses-permission android:name="android.permission.INTERNET"/>
2.拨打电话
//拨打电话 Intent intent = new Intent(Intent.ACTION_CALL,Uri.parse("tel:" + "电话号码")); startActivity(intent); //需要添加拨打电话权限 <uses-permission android:name="android.permission.CALL_PHONE" />
3.打开相机
//打开相机 int reqCode = 0x01;//reqCode是返回的code Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //这里采用startActivityForResult 是为了可以获取返回值 //如果不需要startActivity即可 startActivityForResult(intent, reqCode);
4.打开图库
//打开图库 Intent intent = new Intent(Intent.ACTION_GET_CONTENT); //指定路径 intent.setType("image/*"); startActivityForResult(intent, 1);
5.打开蓝牙
//获取蓝牙设备 BluetoothAdapter blueadapter = BluetoothAdapter.getDefaultAdapter(); //判断是否存在蓝牙设备 if(blueadapter!=null){//不等于null则存在蓝牙设备 //判断蓝牙设备是否开启 if(blueadapter.isEnabled()){ Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); //200就表示200秒。 intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 200); startActivity(intent); } } //需要在androidManifest.xml中声明蓝牙的权限 <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
等等……
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories