2017-08-29 58 views
0

我有一個BroadcastReceiver,當我把它作爲一個單獨的類並在Manifest中註冊時它工作正常。但是,我需要它作爲內部類,因爲如果應用程序位於前臺並且我必須以編程方式註冊它(因爲要將其註冊到Manifest文件中,它必須是靜態的,因此不能訪問非靜態外部類方法)。Android:以編程方式註冊接收器不工作

我試圖像這樣做,但它只是什麼也不做(我改變用於測試目的小時和分鐘)

registerReceiver(new DayCheckReceiver(), new IntentFilter()); 

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 
dayCheckIntent = new Intent(this, DayCheckReceiver.class); 

dayCheckAlarmIntent = PendingIntent.getBroadcast(this, 2000000000, dayCheckIntent, 0); // i use this high number because i use 1+ for a growing list 
Calendar calendar = Calendar.getInstance(); 
calendar.setTimeInMillis(System.currentTimeMillis()); 
calendar.set(Calendar.HOUR_OF_DAY, 14); 
calendar.set(Calendar.MINUTE, 19); 
if (calendar.before(Calendar.getInstance())) { 
    calendar.add(Calendar.DATE, 1); 
} 
alarmManager.setExact(AlarmManager.RTC, calendar.getTimeInMillis(), dayCheckAlarmIntent); 

這是廣播接收器作爲一個內部類:

public class DayCheckReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     //load 
     SharedPreferences sharedPrefs = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE); 
     Gson gson = new Gson(); 
     String json = sharedPrefs.getString(TASKLIST_FILE, null); 
     Type type = new TypeToken<ArrayList<TaskItem>>() { 
     }.getType(); 
     mTaskList = gson.fromJson(json, type); 

     //modifications 
     if (mTaskList != null && mTaskList.size() > 0) { 
      for (int i = 0; i < mTaskList.size(); i++) { 
       mTaskList.get(i).shiftDayChecks(); 
       mTaskList.get(i).resetTimeLeft(); 
      } 
     } 

     //save 
     SharedPreferences.Editor editor = sharedPrefs.edit(); 
     gson = new Gson(); 
     json = gson.toJson(mTaskList); 
     editor.putString(TASKLIST_FILE, json); 
     editor.apply(); 

     tasksFragment.loadTaskList(); 
     tasksFragment.updateTaskList(); 
    } 
} 
+0

發送廣播時,您也將使用相同的操作一個空的'IntentFilter'?我從來沒有嘗試過這樣,但我不認爲這有效。對於廣播,你通常會對一個動作進行過濾,並將相同的動作作爲參數傳遞給過濾器和意圖的構造函數。 – 0xDEADC0DE

+0

當我在manifestxt.xml中註冊它時,它沒有使用意圖過濾器。你能告訴我我會放在那裏嗎? –

回答

1

你需要修改你的註冊行。

registerReceiver(mReceiver, new IntentFilter(action)); 

其中mReceiver是廣播接收器和action的目的是一種用於該廣播接收器將監聽一個字符串。

然後在BroadcastReceiver裏面,你需要在做任何其他事情之前檢查行動。

public static final String CONSTANT_ACTION = "broadcast_action"; 

mReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (null != intent && CONSTANT_ACTION.equals(intent.getAction())) 
       // Write your code here 
     } 
    }; 

而且你這是怎麼播出事件

Intent intent = new Intent(CONSTANT_ACTION); 
sendBroadcast(intent); 
+0

我得到 java.lang.RuntimeException:接收廣播意圖錯誤{act = DAY_CHECK_ACTION flg = 0x10} 我也不明白我在哪裏引用正確的廣播類,如果我只是把這個常量放入新的意圖 –

+0

我現在意識到在哪裏錯誤來自:調用應該更新UI的方法。廣播的註冊工作正在進行,但將該方法放在那裏存在問題。我可能會想出來。 –

+0

@ Fr4nkWh1te當活動被破壞時,不要忘記取消註冊BroadcastReceiver。 –

1

你需要把一些動作在你的IntentFilter,說new IntentFilter("Alarm")。在使用sendBroadcast(new Intent("Alarm"))

+0

嘿,我試圖改變它,但我得到:錯誤接收廣播Intent {act = DAY_CHECK_ACTION flg = 0x10} 任何想法? –

+0

我現在意識到錯誤來自哪裏:從調用應該更新UI的方法。廣播的註冊工作正在進行,但將該方法放在那裏存在問題。我可能會想出來。 –