当用户签入CheckBox
并在TimePickerDialog
中设置时间时,时间已到,服务通过BroadcastReceiver
启动,音乐每6秒播放一次。感谢这是一个Service
,即使我重启应用程序,音乐仍然每6秒播放一次。
类似地,我创建了另一个Service来保存CheckBox的状态,因为如果时间还没到,用户关闭然后打开应用程序,CheckBox仍然必须签入。我创建了一个全局变量来管理复选框的状态:
main.java:
Intent intentA = new Intent(main.this, AlarmReceiverA.class);
PendingIntent pendingIntentA = PendingIntent.getBroadcast(main.this, 0,
intentA, PendingIntent.FLAG_ONE_SHOT);
alarmManagerA = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar now = Calendar.getInstance();
alarmManager.set(AlarmManager.RTC_WAKEUP, now.getTimeInMillis(), pendingIntentA);
Toast.makeText(main.this, "Present time: " +
String.valueOf(now.getTime()), Toast.LENGTH_SHORT).show();
这很简单AlarmReceiver
启动服务:
public final class AlarmReceiverA extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "AlarmA worked.", Toast.LENGTH_LONG).show();
context.startService(new Intent(context, MyServiceA.class));
}
}
这是MyServiceA.class:
@Override
public void onCreate() {
Toast.makeText(this, "My ServiceA Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
GlobalVars.setTimeWatch(1);
}
@Override
public void onDestroy() {
Toast.makeText(this, "My ServiceA Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
@Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My ServiceA Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
GlobalVars.setTimeWatch(1);
}
我收到了“My ServiceA Started”toast的消息,所以到目前为止这一直有效。问题是即使我将变量设置为1 GlobalVars.setTimeWatch(1)
我在重新启动应用程序时也没有收到它。
在main.java中我正在检查这个变量的值:
public void onResume() {
super.onResume();
Toast.makeText(main.this, String.valueOf(GlobalVars.getTimeWatch()),
Toast.LENGTH_SHORT).show();
}
但我得到的只是0.问题必须与服务,因为当我打开一个新的屏幕(例如通过点击一个按钮)然后回去,我得到值为“1”的Toast消息
我也在尝试这个:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putBoolean("MyBoolean", cbstate);
super.onSaveInstanceState(savedInstanceState);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
myb = savedInstanceState.getBoolean("MyBoolean");
}
cbstate是布尔变量。在onCreate方法中,我正在检查cbstate的值:
if (myb == true)
{
cb1.setChecked(true);
Toast.makeText(main.this, "checked", Toast.LENGTH_SHORT).show();
}
else
{
cb1.setChecked(false);
Toast.makeText(main.this, "Not checked", Toast.LENGTH_SHORT).show();
}
重新启动应用程序后,我总是“未选中”。
答案 0 :(得分:0)
在浏览我的开放式问题时,我发现了这个问题,即使我差不多2年前问过它,我也需要回答它以帮助其他人。我不会为这个特定的问题添加解决方案,但我会根据我的一个工作示例告诉我应该做些什么。所以这不是在BroadcastReceiver中检索值的方法。当使用意图时,我们需要使用put命令来保存变量。
在一项活动中,我设置了一个包含三个变量的重复警报:
int REQUEST_CODE = (int) (cal_now.getTimeInMillis()/1000);
Intent intent = new Intent(NewSchedule.this, RepeatingAlarm.class);
intent.putExtra("endTime", cal_alarmend.getTimeInMillis()/1000);
intent.putExtra("reqCode", REQUEST_CODE);
intent.putExtra("startTime", cal_alarmstart.getTimeInMillis()/1000);
PendingIntent sender = PendingIntent.getBroadcast(NewSchedule.this, REQUEST_CODE, intent, 0);
AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, cal_alarmstart.getTimeInMillis(), 6000, sender);
在BroadcastReceiver类的onReceive()方法中,我使用Bundle来检索值:
public class RepeatingAlarm extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
long end = b.getLong("endTime") * 1000;
int rc = b.getInt("reqCode");
long start = b.getLong("startTime") * 1000;
Log.i("rc", rc + "");
Log.i("now", now + "");
Log.i("start", start + "");
}
}