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

《android 学习》三、Intent 的概念及应用

2015-11-12 10:11 399 查看
通过 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=<
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" />


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