public class GetBroadcast extends BroadcastReceiver {

  private static GetBroadcast mReceiver = new GetBroadcast();

  private static IntentFilter mIntentFilter;

  public static void registerReceiver(CoNtext coNtext) {

  mIntentFilter = new IntentFilter();

  mIntentFilter.addDataScheme("package");

  mIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);

  mIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);

  mIntentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED);

  coNtext.registerReceiver(mReceiver, mIntentFilter);

  }

  public static void unregisterReceiver(CoNtext coNtext) {

  coNtext.unregisterReceiver(mReceiver);

  }

  @Override

  public void onReceive(CoNtext coNtext, Intent intent) {

  String action = intent.getAction();

  if (Intent.ACTION_PACKAGE_ADDED.equals(action)) {

  Toast.makeText(coNtext, "有應用被添加", Toast.LENGTH_LONG).show();

  } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) {

  Toast.makeText(coNtext, "有應用被刪除", Toast.LENGTH_LONG).show();

  }

  /*

  * else if(Intent.ACTION_PACKAGE_CHANGED.equals(action)){

  * Toast.makeText(coNtext, "有應用被改變", Toast.LENGTH_LONG).show(); }

  */

  else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) {

  Toast.makeText(coNtext, "有應用被替換", Toast.LENGTH_LONG).show();

  }

  /*

  * else if(Intent.ACTION_PACKAGE_RESTARTED.equals(action)){

  * Toast.makeText(coNtext, "有應用被重啟", Toast.LENGTH_LONG).show(); }

  */

  /*

  * else if(Intent.ACTION_PACKAGE_INSTALL.equals(action)){

  * Toast.makeText(coNtext, "有應用被安裝", Toast.LENGTH_LONG).show(); }

  */

  }

  }
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()