Android的AlarmManager和服务问题 [英] Android AlarmManager and Service question

查看:150
本文介绍了Android的AlarmManager和服务问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些文件在我的应用程序,但只有3人,现在是非常重要的。这是一个与警报声和通知提醒应用程序。 我哈瓦包含一个复选框和它的听众主要code.java文件。如果用户检查中chechbox的AlarmManager发送意图AlarmReceiver.java,启动MyService.java。为MyService的Java包含播放声音code。 code是局部的。 MyService.java:

There are some files in my app, but only 3 of them is important now. It's a reminder app with alarm sound and notifications. I hava a maincode.java file containing a checkbox and its listener. If user checks in the chechbox an AlarmManager sends an intent to AlarmReceiver.java, which starts MyService.java. MyService java contains code about playing sound. Code is partial. MyService.java:

public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

    player = MediaPlayer.create(this, R.raw.sound);
    player.setLooping(false); // Set looping
}

@Override
public void onDestroy() {
    Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onDestroy");
    player.stop();
}

@Override
public void onStart(Intent intent, int startid) {
    Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onStart");
    player.start();
}

AlarmReceiver.java:

AlarmReceiver.java:

public void onCreate() {
    Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
    Log.d(TAG, "onCreate");

    player = MediaPlayer.create(this, R.raw.sound);
    player.setLooping(false); // Set looping

主要code.java重要组成部分:

Important part of maincode.java:

    cb1 = (CheckBox) findViewById(R.id.CheckBox01);
    cb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
        {           
            if (cb1.isChecked()) 
                {
                 if (GlobalVars.getHourOfDay() >= 0) 
                 {
                     Toast.makeText(maincode.this, "ok", Toast.LENGTH_SHORT).show();
                     rem1.setText(GlobalVars.getReminder1name());
                        Intent intent = new Intent(maincode.this, AlarmReceiver.class);
                        PendingIntent pendingIntent = PendingIntent.getBroadcast(bInsulinReminder.this, 0,
                          intent, PendingIntent.FLAG_UPDATE_CURRENT);
                        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
                        Calendar cal = Calendar.getInstance();
                        cal.set(Calendar.HOUR_OF_DAY, GlobalVars.getHourOfDay());
                        cal.set(Calendar.MINUTE, GlobalVars.getMinute());
                        cal.set(Calendar.SECOND, 0);
                        cal.set(Calendar.MILLISECOND, 0);
                        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis()+ 3000, 6000, pendingIntent);

                 }
                 Toast.makeText(maincode.this, "Checked", Toast.LENGTH_SHORT).show();
                } else {
                    rem1.setText("No reminder set");
                    Toast.makeText(maincode.this, "Not checked", Toast.LENGTH_SHORT).show();
                }
        }

        });

(REM1是提醒器按钮,其文本取决于任何用户想要的名称)

(rem1 is the reminder button whose text depends on the name of anything the user wants)

与code问题是,如果我开始报警,我不能阻止它。我知道有在MyService.java的player.stop()命令,但如何从主code.java到底哪里的复选框被选中打电话了吗?

Problem with the code is that if i start the alarm, i cannot stop it. I know there is the player.stop() command in MyService.java, but how can i call it from the end of maincode.java where the checkbox is unchecked?

推荐答案

不,你不能这样做,直接从监听器。您可以禁用报警是这样的:

No you can't do that directly from listener. You can disable the alarm this way:

Intent intent = new Intent(maincode.this, AlarmReceiver.class);
PendingIntent pendingIntent =  PendingIntent.getBroadcast(bInsulinReminder.this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
pendingItem.cancel();
alarmManager.cancel(pendingItem);

或者,如果(我想)AlarmReceiver是执行的BroadcastReceiver,并从的onReceive方法,你开始你的MyService这是实现服务类。

Or if (I suppose) AlarmReceiver is implementation of BroadcastReceiver and from onReceive method you start your MyService which is implementation of Service class.

所以,如果你想从你的主要code.java监听器内停止此报警你可以通过重新PendingIntent你AlarmReceiver使用和执行stopService方法停止为MyService。

So, if you want to stop this alarm from inside of your maincode.java listener you can just stop MyService by recreating PendingIntent you used in AlarmReceiver and executing stopService method.

希望有所帮助。

这篇关于Android的AlarmManager和服务问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆