Alarmmanager立刻熄灭

时间:2016-04-07 11:22:09

标签: java android android-studio android-intent alarmmanager

我目前正在使用AndroidStudio中的AlarmClock。我的项目在我的模拟器上完全正常工作,这意味着当我从我的TimePicker中选择一个时间时,闹钟会在所选择的时间响起,这很可能是它应该如何。但是,如果我在真正的Android手机上运行应用程序,闹钟会立即响起,并且根本不考虑我的TimePicker。 也许有人可以告诉我什么是错的,因为它可以在模拟器上运行但不能在我的真实手机上运行? 我在某处看到它可能与“alarmmanager.set”有关,我选择了“calendar.getTimeInMillis()”。但我很困惑的是它应该是什么,以及为什么它在我的模拟器上工作但不在我的真实手机上。 请麻烦帮助一个菜鸟!

这是主要的课程:

public class SleepActivity extends AppCompatActivity {


AlarmManager alarmmanager;
TimePicker alarmtimepicker;


PendingIntent pendingintent;



private SleepRepository sp;
private Date date;

@Override
protected void onCreate(Bundle savedInstanceState) {





    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sleep);
    sp = new SleepRepository(SleepActivity.this);
    date = new Date();


    //Create an Intent to the Receiver Class
    final Intent receiver = new Intent(getApplicationContext(), AlarmReceiver.class);

    alarmmanager = (AlarmManager) getSystemService(ALARM_SERVICE);

    final Calendar calendar = Calendar.getInstance();

    alarmtimepicker = (TimePicker) findViewById(R.id.sleep_timepicker);
    alarmtimepicker.setIs24HourView(true);









    // Initializing the two Buttons in activity_sleep.xml
    Button startsleep = (Button) findViewById(R.id.sleep_startlog_button);
    Button endsleep = (Button) findViewById(R.id.sleep_endlog_button);

    final SleepRepository database = new SleepRepository(SleepActivity.this);
    final Date date = new Date();


    List<Sleep> sleepList = database.getAllRecords();
    Log.v("MOINMOINMOIN", "HALLO HALLO HALLO");
    Log.v("SleepActivity", sleepList.toString());


            // onClickListener for the two Buttons in activity_sleep.xml
            startsleep.setOnClickListener(new View.OnClickListener() {

                @TargetApi(Build.VERSION_CODES.M)

                @Override
                public void onClick(View v) {

                    database.insertRecord(33.0, date, 33);




                    //Setting Calendar instance with the Hour and Minute that we have picked on the TimePicker
                    int currentApiVersion = android.os.Build.VERSION.SDK_INT;
                    if (currentApiVersion > android.os.Build.VERSION_CODES.LOLLIPOP_MR1){
                        calendar.set(Calendar.HOUR_OF_DAY, alarmtimepicker.getHour());
                        calendar.set(Calendar.MINUTE, alarmtimepicker.getMinute());


                    } else {
                        calendar.set(Calendar.HOUR_OF_DAY, alarmtimepicker.getCurrentHour());
                        calendar.set(Calendar.MINUTE, alarmtimepicker.getCurrentHour());

                    }








                    //Put in extra String into Receiver Intent
                    //Tells the Clock that you have pressed the "Go to Bed" button
                    receiver.putExtra("extra", "alarm on");

                    //Create a pending Intent that delays the Intent until the specified Calendar-Time
                    pendingintent = PendingIntent.getBroadcast(getApplicationContext(), 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);

                    //Set the AlarmManager

                    alarmmanager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingintent);



                }
            });





    endsleep.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            //Put in extra String into Receiver Intent
            //Tells the clock that you have pressed the "Wake Up" button
            receiver.putExtra("extra", "alarm off");


            //Stop the Ringtone
            sendBroadcast(receiver);

            alarmmanager.cancel(pendingintent);


        }
    });

这是我的BroadcastReceiver:

public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Log.e("We are in the Receiver!", "yay");


    //fetch extra Strings from intent
    String get_your_string = intent.getExtras().getString("extra");


    Log.e("What is the key??", get_your_string);


    //Create an Intent for the RingtoneService
    Intent service_intent = new Intent(context, RingtoneService.class);


    //pass the extra Strings from SleepActivity to the Ringtone Playing Service
    service_intent.putExtra("extra", get_your_string);


    //Start the Ringtone Service
    context.startService(service_intent);

}

}

最后我的RingtoneService告诉媒体播放器播放铃声:

public class RingtoneService extends Service {

MediaPlayer media;
String message;
boolean isRunning;


@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    Log.i("LocalService", "Receive start id" + startId + ":" + intent);


    //fetch the extra String Values
    String state = intent.getExtras().getString("extra");


    // assert = If there is a "NullPointerException", then do not go through the code below

    Log.e("Ringtone extra is", state);

    // assert state != null;
    //this converts the extra Strings from the intent to start ID's, value 0 or 1;

    switch (state) {
        case "alarm on":
            startId = 1;
            break;
        case "alarm off":
            startId = 0;
            break;
        default:
            startId = 0;
            break;
    }





    // if else - statements to either start or stop the ringtone

    //if there is no music playing, and the user pressed "Go to Sleep"
    //Music should start playing.
    if (!this.isRunning && startId == 1) {

        Log.e("there is no music", "and it should play");

        //Create an Instance of the MediaPlayer and start it
        media = MediaPlayer.create(this, R.raw.ringtone);
        media.start();

        this.isRunning = true;
        startId = 0;

    }

    //if there is music playing, and the user pressed "Wake up"
    //Music should stop playing.
    else if (this.isRunning && startId == 0) {

        Log.e("there is no music", "and it should not play");

        //stop the ringtone
        media.stop();
        media.reset();

        this.isRunning = false;
        startId = 0;


    }


    //these are if the user presses random buttons
    //bug-preventive

    //if there is no music playing, and the user pressed "Wake Up"
    //do nothing.
    else if (!this.isRunning && startId == 0) {

        Log.e("there is no music", "and it should not play");

        this.isRunning = false;
        startId = 0;

    }

    //if there is music playing, and the user pressed "Go to sleep"
    //do nothing.
    else if (this.isRunning && startId == 1) {

        Log.e("there is music", "and it should play");

        this.isRunning = true;
        startId = 1;

    }

    //catch the odd event.
    else {

        Log.e("else", "somehow you reached this");

    }







    return START_NOT_STICKY;
}

0 个答案:

没有答案