Android 无法接收开机广播的问题

Android手机开机后,会发送android.intent.action.BOOT_COMPLETED广播,监听这个广播就能监听开机。

<receiver android:name="com.netmoon.broadcast.BootBroadCastReceiver">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED">
        </action>
    </intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>



public class BootRroadCastReceiver extends BroadcastReceiver {
    private final String ACTION_BOOT = "android.intent.action.BOOT_COMPLETED";
    @Override
    public void onReceive(Context context, Intent intent) {
        if(ACTION_BOOT.equals(intent.getAction()))
            Toast.makeText(context, R.string.bootup_receiver, Toast.LENGTH_SHORT).show();
    }
}

但是Android API Level8 以上的时候,程序可以安装在SD卡上。如果程序安装在SD卡上,那么在BOOT_COMPLETED广播发送之后,SD卡才会挂载,因此程序无法监听到该广播。

**解决办法:**同时监听开机和sd卡挂载。(也不能只监听挂载就认为开机了,因为有的手机没有sd卡)

实现对挂载进行监听media mounted如下:

<receiver android:name=".Ge">
    <intent-filter >
        <action android:name="android.intent.action.MEDIA_MOUNTED"/>
        <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
        <data android:scheme="file">
        </data>
    </intent-filter>
</receiver>

合并开机广播监听和sd卡挂载监听:

<receiver android:name=".Ge">        
    <intent-filter >
        <action android:name="android.intent.action.BOOT_COMPLETED"/>
    </intent-filter>
    <intent-filter >
        <action android:name="android.intent.action.MEDIA_MOUNTED"/>
        <action android:name="android.intent.action.MEDIA_UNMOUNTED"/>
        <data android:scheme="file">
        </data>
    </intent-filter>
</receiver>

还收不到?

增加权限:

<uses-permission android:name="android.permission.RESTART_PACKAGES" />

修改开机广播接收器如下:

 <receiver android:name=".BootBroadcastReceiver" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
     
                    <category android:name="android.intent.category.HOME" />
                </intent-filter>
                <intent-filter>
                    <action android:name="android.intent.action.PACKAGE_ADDED" />
                    <action android:name="android.intent.action.PACKAGE_REMOVED" />
                    <action android:name="android.intent.action.PACKAGE_REPLACED" />
     
                    <data android:scheme="package" />
                </intent-filter>
            </receiver>


public class BootBroadcastReceiver extends BroadcastReceiver
{
	@Override
	public void onReceive(Context context, Intent intent)
	{
		//接收广播:系统启动完成后运行程序
		if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED))
		{
			Intent ootStartIntent = new Intent(context, Login_Activity.class);
			ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(ootStartIntent);
		}
		//接收广播:安装更新后,自动启动自己。      
		if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED) || intent.getAction().equals(Intent.ACTION_PACKAGE_REPLACED))
		{
			Intent ootStartIntent = new Intent(context, Login_Activity.class);
			ootStartIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			context.startActivity(ootStartIntent);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/xdy1120/article/details/85044208