2012-11-05 205 views
0

我有一個廣播接收機(BackgroundReceiver),該監聽去除包,然後廣播包的名稱到一個活動(AnalyzeActivity活動不從廣播接收廣播定製意向接收器

BackgroundReceiver.java

public static final String REMOVED_INTENT = "com.analyze.project.RemovedReceiver"; 

    public void onReceive(Context context, Intent intent) { 
     String actionStr = intent.getAction(); 
     if (Intent.ACTION_PACKAGE_REMOVED.equals(actionStr)) { 
      Uri uri = intent.getData(); 
      String pkg = uri != null ? uri.getSchemeSpecificPart() : null; 
      Log.i("Logging Service", "Package Removed: " + pkg); 
      //Toast.makeText(context, "Package Removed:" + pkg, Toast.LENGTH_SHORT).show(); 

      Intent c = new Intent(); 
      c.setAction(REMOVED_INTENT); 
      c.putExtra("Package", pkg); 
      context.sendBroadcast(c); 
     } 
    } 

我動態註冊我的AnalyzeActivity內的接收器監聽定製廣播意圖BackgroundReceiver

AnalyzeActivity.java

@Override 
     protected void onResume() { 
      super.onResume(); 
      // Register Sync Recievers 
      IntentFilter intentToReceiveFilter = new IntentFilter(); 
      intentToReceiveFilter.addAction(BackgroundReceiver.REMOVED_INTENT); 
      this.registerReceiver(mIntentReceiver, intentToReceiveFilter, null, mHandler); 
      mReceiversRegistered = true; 

     } 

    final BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      String Package = intent.getStringExtra("Package"); 
     if (intent.getAction().equals(BackgroundReceiver.REMOVED_INTENT)) { 
       Toast.makeText(context, "(A) Package Removed: " + Package, Toast.LENGTH_SHORT).show(); 
       Log.i("Package Removed", "(A) Package Removed: " + Package); 
      } 

     } 
    }; 

但我的活動似乎並不能夠在所有接收廣播意圖。

+0

第一BR是否工作? (BackgroundReceiver) –

+0

我只是想在你的虛擬項目代碼(在這裏我提出從onOptionsItemSelected()方法廣播),它是工作的罰款。當您收到一個自定義的播放您能收到來自系統軟件包刪除廣播... –

+0

是您的活動(如廣播註冊)是可見的? –

回答

0

使用此功能在您的onResume

IntentFilter filter = new IntentFilter(BackgroundReceiver.REMOVED_INTENT); 

mIntentReceiver intentToReceiveFilter = new mIntentReceiver(); 

registerReceiver(intentToReceiveFilter,filter); 

註冊broacast並用它來從包裝中取出廣播接收器發送廣播

Intent intent = new Intent(BackgroundReceiver.REMOVED_INTENT); 
intent.putExtra("Package", pkg); 
context.sendBroadcast(intent); 

請讓我知道,如果它解決您的問題