public class SDCardCast extends BroadcastReceiver{
public void onReceive(Context context, Intent){
String str = intent.getAction();
if ("android.intent.action.MEDIA_MOUNTED".equals(str)){
}else if(){
//業務邏輯.......
}
}
}
開機廣播接收者
// BOOT_COMPLETED是廣播Action的名字
public class BootReceiver extends BroadcastReceiver{
public void onReceive(Context context, Intent intent){
Intent it = new Intent(context, XXX.class);
//在context外部啟動Activity必須設定這個flag,實際就是建立一個新的Activity棧
it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(it);
}
}
應用狀态廣播
應用狀态有三種:安裝、更新、解除安裝
// PACKAGE_ADDED\PAGEAGE_REMOVED\PACKAGE_REPLACED
//intent-fliter還要定義一個data android:scheme="package"
public class AppStatusReceiver extends BroadcastReceiver{
public void onReceive(Context c, Intent it){
String action = it.getAction();
Uri uri = it.getData();
if (action.equals("PACKAGE_ADDED")){
System.out.println(uri.toString());
}else if(){
/*
其他邏輯.....
*/
}
}
}