开发者

Service question

When user checks in a CheckBox and sets time in a TimePickerDialog and when time has come a service is started through a BroadcastReceiver and music is playing every 6 seconds. Thanks to this is a Service even if I restart the app the music is still playing every 6 secs. Similarly I created another Service to save the state of the CheckBox, because if time has not come and user closes and then opens the app the CheckBox still must be checked in. I created a global variable to manage the state of the 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();

This is easy AlarmReceiver starts service:

public final class AlarmReceiverA extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Int开发者_JAVA技巧ent intent) {
        Toast.makeText(context, "AlarmA worked.", Toast.LENGTH_LONG).show();
         context.startService(new Intent(context, MyServiceA.class));
     }
}

And this is the 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);
}

I get the "My ServiceA Started" toast message, so this has been working so far. Problem is even if I set the variable to 1 GlobalVars.setTimeWatch(1) I am not getting it back when I restart the app.

In main.java I am inspecting the value of this variable:

public void onResume() {
    super.onResume();
    Toast.makeText(main.this, String.valueOf(GlobalVars.getTimeWatch()),
            Toast.LENGTH_SHORT).show();
}

But all I am getting is 0. Problem must with the service, because when I open up a new screen (e.g. by click on a button) and then go back, I get the Toast message with the value of "1".

  1. Is this the way to save the state of a CheckBox?
  2. What is wrong with the code?

I am also trying with this:

  @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 is boolean variable. In the onCreate method i am inspecting the value of 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();
        }  

After restarting app i always get "Not checked".


When browsing my open questions I found this one and even if I asked it almost 2 years ago I need to answer it to help others. I am not going to add the solution to this specific problem but I will tell what I should have done based on one of my working examples. So this is not the way to retrieve a value in a BroadcastReceiver. When using intents we need to use the put command to save a variable.

Here in an activity I am setting a repeating alarm with three variables:

  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);

In the onReceive() method of the BroadcastReceiver class I use a Bundle to retrieve the values:

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 + "");

      }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜