《android 学习》三、Intent 的概念及应用
16lz
2022-03-18
通过 Intent 可以打开不同的 Activity
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="@+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);//还需要添加网络权限"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" />
等等……
更多相关文章
- android wifi 设置 控制开关
- 安卓 跳转淘宝、京东app,并打开商品详情页
- android面试题汇总
- Android中在service中启动activity
- Android(安卓)使用系统摄像头拍相片与拍视频,并显示
- android 之 选择文件
- android打开联系人的代码
- android通过led实现手电筒
- Android(安卓)Activity的生命周期及四种启动模式的联合研究