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

这里写图片描述
觉得本文对你有用,麻烦点赞或关注或收藏,你的肯定是我创作的无限动力,谢谢!!!

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Rjdeng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值