开发者

android: simple alarm clock

开发者 https://www.devze.com 2023-02-20 16:35 出处:网络
I\'m writing simple alarm clock. It contains MainActivity with a ListView, WakeActivity with wake up message and a PlayerService that plays sound. I use the following code to set alarm clock:

I'm writing simple alarm clock. It contains MainActivity with a ListView, WakeActivity with wake up message and a PlayerService that plays sound. I use the following code to set alarm clock:

Intent i = new Intent(context, WakeupActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, a.getId(), i, 0);
    Calendar cal = getNextAlarm();
    if(cal!=null){
        alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
        if(toast) showToast(cal);
    }

In android manifest WakeupActivity is defined as

android:launchMode="singleTask"

In onCreate method I init close button. In onResume I start the PlayerService

Intent i = new Intent(this, PlayerService.class.); startActivity(i);

When user click close button I call fnish(); and, at last, in onPause() I stop PlayerService. It works but there is one case I can't understand.

  1. I set alarm and leave main activity on the top. WakeupActivity and PlayerService starts successfully. When I press close button I get back to MainActivity and everything's ok.
  2. I set alarm and press home button. WakeupActivity and PlayerService starts successfully. When I press close button then music stops and onDestroy() for WakeupActivity calls (like in 1 case)... Seems good but when I hold home button and choose my application then I get back to Wak开发者_StackOverflow社区eupActivity and the music starts again!!! What the hell? I finish that activity! Why so? Please, help!
0

精彩评论

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

关注公众号