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

通过 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);

//还需要添加网络权限
<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" />  

等等……

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
IntentAndroid 应用程序中的一个重要概念,它用于在应用程序的不同组件之间传递数据并触发操作。Intent 可以被用于启动 Activity、Service、Broadcast Receiver 等组件,也可以被用于启动其他应用程序中的组件。 在 Android Studio 中使用 Intent 非常简单,以下是一些常见的用法: 1. 启动 Activity ```java // 创建 Intent 对象 Intent intent = new Intent(MainActivity.this, SecondActivity.class); // 传递参数 intent.putExtra("key", "value"); // 启动 Activity startActivity(intent); ``` 2. 启动 Service ```java // 创建 Intent 对象 Intent intent = new Intent(MainActivity.this, MyService.class); // 传递参数 intent.putExtra("key", "value"); // 启动 Service startService(intent); ``` 3. 发送广播 ```java // 创建 Intent 对象 Intent intent = new Intent("com.example.MY_ACTION"); // 传递参数 intent.putExtra("key", "value"); // 发送广播 sendBroadcast(intent); ``` 4. 接收广播 ```java // 创建 BroadcastReceiver 对象 BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // 处理接收到的广播 } }; // 创建 IntentFilter 对象 IntentFilter filter = new IntentFilter(); filter.addAction("com.example.MY_ACTION"); // 注册广播接收器 registerReceiver(receiver, filter); ``` 以上是一些基本的用法,Intent 还可以用于启动其他应用程序中的组件、传递复杂数据类型等。在使用 Intent 时,需要注意传递参数的类型、组件是否存在等问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值