一些非常有用的Android Intent Action & android receiver

ACTION_AIRPLANE_MODE_CHANGED
Broadcast Action:用户打开或关闭飞行模式。一个或多个广播会打开或关闭。这个intent会携带下面的附加值:
state:一个boolean值,指明飞行模式是否打开。如果是true,cell radio以及其他一些例如蓝牙,wifi的广播会关闭。
注:这是一个只有系统可以发送的受保护的intent。
常量值:"android.intent.action.AIRPLANE_MODE"
 
ACTION_ALL_APPS
Activity Action:列出所有可用的应用。
常量值:"android.intent.action.ALL_APPS" 
 
ACTION_ANSWER
Activity Action:处理呼入的电话。
常量值:"android.intent.action.ANSWER" 
 
ACTION_APP_ERROR
Activity Action:当用户点击crash/ANR对话框的"Report"按钮时发出的intent。
常量值:"android.intent.action.APP_ERROR"
  
ACTION_ATTACH_DATA
用于指明一些资源应该被附加到其他的地方。例如,一个图片资源可以被附加到一个联系人。它由接受者决定资源应该被附加到什么地方,这个intent不指明最终的目地。
输入:getData()方法可以获取附加资源的URI。
常量值:"android.intent.action.ATTACH_DATA"
 
ACTION_BATTERY_CHANGED
Broadcast Action:这是一个包含电池的充电状态,级别,和其他信息的复杂的广播。
注:这是一个只有系统可以发送的受保护的intent。
常量值:"android.intent.action.BATTERY_CHANGED" 


ACTION_BATTERY_LOW
Broadcast Action:指示设备电量不足。这个广播会触发"电量不足警告"系统对话框。
注:这是一个只有系统可以发送的受保护的intent。
常量值:"android.intent.action.BATTERY_LOW"
 
ACTION_BATTERY_OKAY
Broadcast Action:指示电池从电量不足状态恢复。一旦电池从电量不足状态恢复这个广播会被触发。
注:这是一个只有系统可以发送的受保护的intent。
常量值:"android.intent.action.BATTERY_OKAY" 


ACTION_BOOT_COMPLETED
Broadcast Action:系统启动完成后触发该intent。它可以用来执行应用指定的初始化工作,例如初始化闹钟。你必须指明RECEIVE_BOOT_COMPLETED权限来接收这个intent。
注:这是一个只有系统可以发送的受保护的intent。
常量值:"android.intent.action.BOOT_COMPLETED" 


ACTION_BUG_REPORT
Activity Action:用来显示报告bug的activity。
常量值:"android.intent.action.BUG_REPORT"
 
ACTION_CALL
Activity Action:根据指明的信息向某人拨打电话。
输入:如果为空,启动一个空的拨号界面;如果不为空,通过getData()方法获取一个手机号码或者电话号码的URI进行拨号:URI是一个显示的手机号码。
注:应用在初始化一个拨号事件时会受到一些限制;大部分的应用可以使用ACTION_CALL。
注:这个intent不能用于进行紧急呼叫拨号。然而,应用程序可以通过ACTION_DIAL进行紧急呼叫拨号。
常量值:"android.intent.action.CALL" 


ACTION_CALL_BUTTON
Activity Action:用户点击拨号按钮进入拨号界面,或者其他适当的可以代替拨号界面的UI界面。
常量值:"android.intent.action.CALL_BUTTON" 


ACTION_CAMERA_BUTTON
Broadcast Action:点击拍照键。包含一个单独的额外字段:EXTRA_KEY_EVENT,包含触发这个广播的按键事件。

常量值:"android.intent.action.CAMERA_BUTTON"



从别人那里转的,感觉写的很好

可以在代码文件中声明一个receiver,也可以在manifest中声明一个,前者中的receiver只有在该activity launch起来以后才会监听其所感兴趣的文件,而如果在androidManifext.xml中声明的话,就不受限制,随时可以监听感兴趣的事件。

首先谈谈在 androidManifext.xml 中注册一个 receiver,  例如我们想监听相机按钮按下事件的发生,并且发生后调用我们的 camera 程序


<receiver android:name="CameraButtonIntentReceiver">
            <intent-filter>
                <action android:name="android.intent.action.CAMERA_BUTTON"/>
            </intent-filter>
</receiver>


在这个配置文件中声明了一个receiver用来监听相机的按键事件,所以还需要在代码文件定义与配置文件中同名的receiver



public class CameraButtonIntentReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
    Intent i = new Intent(Intent.ACTION_MAIN);
        i.setClass(context, Camera.class);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
}
}



2.

      关于另外一种,在代码中注册一个receiver,例如我们想在代码文件中监听电池电量的变化,就可以按照如下方法




private final BroadcastReceiver mBatteryReceiver = new BroadcastReceiver() {
       @Override
        public void onReceive(Context context, Intent intent) {
             String action = intent.getAction();
              if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
              …
              }
        }
}


这种方法需要在onCreate 或者onStart中注册该receiver所感兴趣的intent,如:

registerReceiver(mBatteryReceiver, Intent.ACTION_BATTERY_CHANGED);


onStoponDestroy中注销监听


registerReceiver(mBatteryReceiver, Intent.ACTION_BATTERY_CHANGED);





一个监听camera按钮的例子

http://www.oschina.net/code/explore/android-2.2-froyo/com/android/camera/CameraButtonIntentReceiver.java


/**
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.camera;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

/***
 * {@code CameraButtonIntentReceiver} is invoked when the camera button is
 * long-pressed.
 *
 * It is declared in {@code AndroidManifest.xml} to receive the
 * {@code android.intent.action.CAMERA_BUTTON} intent.
 *
 * After making sure we can use the camera hardware, it starts the Camera
 * activity.
 */
public class CameraButtonIntentReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // Try to get the camera hardware
        CameraHolder holder = CameraHolder.instance();
        if (holder.tryOpen() == null) return;

        // We are going to launch the camera, so hold the camera for later use
        holder.keep();
        holder.release();
        Intent i = new Intent(Intent.ACTION_MAIN);
        i.setClass(context, Camera.class);
        i.addCategory(Intent.CATEGORY_LAUNCHER);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(i);
    }
}

猜你喜欢

转载自blog.csdn.net/feelinghappy/article/details/80263650