ACTION_POWER_DISCONNECTED廣播使用解析

在做項目的時候,要求拔出USB接口要刪除指定文件,達到某功能;就想到利用ACTION_POWER_DISCONNECTED廣播。

  • 1、 配置文件

AndroidManifest.xml

<receiver android:name="com.xtc.charging.activity.PowerBroadcastReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
        <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
    </intent-filter>
</receiver>
  • 2、工具類

CommonUtil.java

public static boolean createFile( String destFileName )
{
    File file = new File( destFileName );
    if ( file.exists( ) )
    {
        LogUtil.d( TAG, "創建單個文件" + destFileName + "失敗,目標文件已存在!" );
        return false;
    }
    if ( destFileName.endsWith( File.separator ) )
    {
        LogUtil.e( TAG, "創建單個文件" + destFileName + "失敗,目標文件不能爲目錄!" );
        return false;
    }

    // 判斷目標文件所在的目錄是否存在
    if ( !file.getParentFile( ).exists( ) )
    {
        // 如果目標文件所在的目錄不存在,則創建父目錄
        LogUtil.d( TAG, "目標文件所在目錄不存在,準備創建它!" );
        if ( !file.getParentFile( ).mkdirs( ) )
        {
            LogUtil.e( TAG, "創建目標文件所在目錄失敗!" );
            return false;
        }
    }

    // 創建目標文件
    try
    {
        if ( file.createNewFile( ) )
        {
            LogUtil.d( TAG, "創建單個文件" + destFileName + "成功!" );
            return true;
        }
        else
        {
            LogUtil.e( TAG, "創建單個文件" + destFileName + "失敗!" );
            return false;
        }
    } catch ( IOException e )
    {
        e.printStackTrace( );
        LogUtil.e( TAG, "創建單個文件" + destFileName + "失敗!" + e.getMessage( ) );
        return false;
    }
}

public static void delFile( String filePaht )
{
    try
    {
        if ( Environment.getExternalStorageState( ).equals( Environment.MEDIA_MOUNTED ) )
        {
            File file = new File( filePaht );

            if ( file.exists( ) == true )
            {
                file.delete( );
                LogUtil.d( TAG, "刪除目標文件:" + filePaht );
            }
            else
            {
                LogUtil.d( TAG, "刪除目標文件不存在:" + filePaht );
            }
        }
    } catch ( Exception exception )
    {
        exception.printStackTrace( );
    }
}
  • 3、接收廣播

PowerBroadcastReceiver.java

package com.xtc.charging.activity;

import com.xtc.charging.util.CommonUtil;
import com.xtc.charging.util.LogUtil;

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

public class PowerBroadcastReceiver extends BroadcastReceiver
{
    @Override
    public void onReceive( Context context, Intent intent )
    {
        // TODO Auto-generated method stub
        // 方式一
        /*
        int chargePlug = intent.getIntExtra( BatteryManager.EXTRA_PLUGGED, -1 );
        LogUtil.d( CommonUtil.TAG, "chargePlug =" + chargePlug );

        if ( chargePlug == -1 )
        {
            CommonUtil.delFile( CommonUtil.INSTALL_FILE );
        }
        */

        // 方式二
        String action = intent.getAction( );
        LogUtil.d( CommonUtil.TAG, "action =" + action );
        if ( action.equals( Intent.ACTION_POWER_DISCONNECTED ) )
        {
            CommonUtil.delFile( CommonUtil.INSTALL_FILE );
        }
    }
}
  • 4、打印Log

    這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章