幾種常用BroadcastReceiver

jia635發表於2014-06-25

開機廣播

可在此廣播中做開機自啟動服務


public class BootBroadcasrReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i("CDH", "開機廣播");
	}
}
<receiver android:name=".BootBroadcasrReceiver" >
<span style="white-space:pre">	</span><intent-filter>
<span style="white-space:pre">		</span><!-- 接受系統開機廣播 -->
<span style="white-space:pre">		</span><action android:name="android.intent.action.BOOT_COMPLETED"/>
        <category android:name="android.intent.category.HOME"/>
<span style="white-space:pre">	</span></intent-filter>
</receiver>

<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; ">需要新增許可權</span>
<span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; "><uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/></span><div>
</div><div><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; ">應用增加廣播</span>
</div><div><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; ">
</span></div><div><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; ">/**
 * 1.安裝其他應用時會收到廣播,但安裝本應用時不會收到廣播
 * 2.重新安裝其他應用時會收到廣播,但重新安裝本應用時不會收到廣播
 */
public class PackageAddedBroadcastReceiver extends BroadcastReceiver {
<span style="white-space:pre">	</span>@Override
<span style="white-space:pre">	</span>public void onReceive(Context context, Intent intent) {
<span style="white-space:pre">		</span>Log.i("CDH", "應用增加廣播");
<span style="white-space:pre">		</span>Log.i("CDH", "包名:"+intent.getDataString());
<span style="white-space:pre">		</span>Log.i("CDH", "使用者ID:"+intent.getIntExtra(Intent.EXTRA_UID, 0));
<span style="white-space:pre">		</span>Log.i("CDH", "重新安裝應用?:"+intent.getBooleanExtra(Intent.EXTRA_REPLACING, false));
<span style="white-space:pre">	</span>}
}
</span></div><div><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; ">
</span></div><div><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; "><receiver android:name=".PackageAddedBroadcastReceiver">
<span style="white-space:pre">	</span><intent-filter>
<span style="white-space:pre">		</span><action android:name="android.intent.action.PACKAGE_ADDED"/>
        <data android:scheme="package"/>
<span style="white-space:pre">	</span></intent-filter>
</receiver>
</span></div><div><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; ">
</span></div><div><span style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px; ">
</span></div>


應用解除安裝廣播

/**
 * 1.重新安裝本應用時會收到廣播
 * 2.重新安裝其他應用時會收到廣播
 * 3.其他應用被解除安裝時會收到廣播,但本應用被解除安裝時不會收到廣播
 */
public class PackageRemovedBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("CDH", "應用解除安裝廣播");
Log.i("CDH", "包名:"+intent.getDataString());
Log.i("CDH", "使用者ID:"+intent.getIntExtra(Intent.EXTRA_UID, 0));
Log.i("CDH", "整個應用被解除安裝?:"+intent.getBooleanExtra(Intent.EXTRA_DATA_REMOVED, false));
Log.i("CDH", "重新安裝應用?:"+intent.getBooleanExtra(Intent.EXTRA_REPLACING, false));
}
}

<receiver android:name=".PackageRemovedBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
        <data android:scheme="package"/>
</intent-filter>
</receiver>

需要新增許可權


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


應用重新安裝廣播

1.本應用重新安裝

收到解除安裝廣播 --> 重新安裝廣播

/**
 * 1.重新安裝本應用時會收到廣播
 * 2.重新安裝其他應用時會收到廣播
 */
public class PackageReplacedBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.i("CDH", "應用替換廣播");
Log.i("CDH", "包名:"+intent.getDataString());
Log.i("CDH", "使用者ID:"+intent.getIntExtra(Intent.EXTRA_UID, 0));
Log.i("CDH", "重新安裝應用?:"+intent.getBooleanExtra(Intent.EXTRA_REPLACING, false));
}
}


<receiver android:name=".PackageReplacedBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
        <data android:scheme="package"/>
</intent-filter>
</receiver>



2.其他應用重新安裝

收到解除安裝廣播 --> 應用增加廣播 --> 重新安裝廣播



相關文章