-1
所以我有一個廣播接收器,工作得很好。問題是,當它被調用時,我不能改變片段,因爲我的類擴展了BroadcastReceiver而不是Activity或Fragment。因此,我無法訪問getFragmentManager()或getSupportFragmentManager()。我不確定如何繼續並得到任何幫助。從broadcastreceiver更改片段
廣播接收器類:
package lucaclock.moticlock;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmManagerBroadcastReceiver extends BroadcastReceiver {
public static final String numAlarmsPreferences = "numAlarmsPreferences";
public static final String numAlarmsKey = "numAlarmsKey";
public static final String alarm1Preferences = "alarm1Preferences";
public static final String alarm2Preferences = "alarm2Preferences";
public static final String alarm3Preferences = "alarm3Preferences";
public static final String alarm4Preferences = "alarm4Preferences";
public static final String alarmTimeDisplayKey = "alarmTimeDisplayKey";
public static final String alarmTimeRequestedKey = "alarmTimeRequestedKey";
public static final String alarmNameKey = "alarmNameKey";
public static final String alarmOccuranceKey = "alarmOccuranceKey";
public static final String alarmActiveKey = "alarmActiveKey";
public static final String alarmSoundUriKey = "alarmSoundUriKey";
public static final String alarmSoundTitleKey = "alarmSoundTitleKey";
public static final String alarmVolumeKey = "alarmVolumeKey";
public static final String alarmSnoozeKey = "alarmSnoozeKey";
public static final String alarmVisibleKey = "alarmVisibleKey";
public static final String alarmRunningKey = "alarmVisibleKey";
public boolean alarm1Active;
public boolean alarm2Active;
public boolean alarm3Active;
public boolean alarm4Active;
public void onReceive(Context context, Intent intent) {
WakeLocker.acquire(context);
MainActivity mA = new MainActivity();
mA.updateFragment(HomeFragment.class, null);
Toast.makeText(context, "ALARM ACTIVE!!!", Toast.LENGTH_LONG).show();
/*---------------THIS CODE IS RUN WHEN THE ALARM IS ACTIVE----------------------//
//
//------------------------------------------------------------------------------//
*/
//initialize preferences
AppSharedPreferences alarm1Prefs = AppSharedPreferences.getInstance(context, alarm1Preferences);
AppSharedPreferences alarm2Prefs = AppSharedPreferences.getInstance(context, alarm2Preferences);
AppSharedPreferences alarm3Prefs = AppSharedPreferences.getInstance(context, alarm3Preferences);
AppSharedPreferences alarm4Prefs = AppSharedPreferences.getInstance(context, alarm4Preferences);
alarm1Active = alarm1Prefs.getBoolean(alarmActiveKey, false);
alarm2Active = alarm2Prefs.getBoolean(alarmActiveKey, false);
alarm3Active = alarm3Prefs.getBoolean(alarmActiveKey, false);
alarm4Active = alarm4Prefs.getBoolean(alarmActiveKey, false);
}
public void cancelAlarm(Context context)
{
Toast.makeText(context, "Alarm canceled successfully", Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
AppSharedPreferences mPref = AppSharedPreferences.getInstance(context, alarm1Preferences);
mPref.putBoolean(alarmRunningKey, false);
mPref.putBoolean(alarmActiveKey, false);
WakeLocker.release();
}
}
謝謝!然而,我有一個問題可以調整。因爲這包括onReceive(),這是否意味着我將不得不改變我的清單指向這個活動作爲接收器? – 2JZIgnition
否,因爲您只爲活動的生命週期註冊它。我認爲廣播將由Manifest註冊的接收器發送,對嗎? – Toochka
我按照你的建議,用我的MainActivity類更新了我的帖子。我的BroadcastReceiver類似乎運行良好,因爲我所示的Toast在被調用時正在運行,但是我的onReceive在我的活動中似乎沒有調用。有可能我有2個onReceive方法,應該只有一個? – 2JZIgnition