Android AlarmManager 和服务问题

发布于 2024-10-14 18:43:59 字数 2970 浏览 4 评论 0原文

我的应用程序中有一些文件,但现在只有 3 个是重要的。这是一个带有闹钟声音和通知的提醒应用程序。 我有一个 maincode.java 文件,其中包含一个复选框及其侦听器。如果用户选中复选框,则 AlarmManager 会向 AlarmReceiver.java 发送一个意图,从而启动 MyService.java。 MyService java 包含有关播放声音的代码。代码是部分的。 MyService.java:AlarmReceiver.java:maincode.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();
}

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

重要部分:

    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 是提醒按钮,其文本取决于用户想要的任何内容的名称)

代码的问题是,如果我启动警报,我无法停止它。我知道 MyService.java 中有player.stop()命令,但是我如何从 maincode.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:

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

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 is the reminder button whose text depends on the name of anything the user wants)

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哽咽笑 2024-10-21 18:43:59

不,你不能直接从听众那里做到这一点。您可以通过这种方式禁用警报:

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,它是 Service 类的实现。

因此,如果您想从 maincode.java 侦听器内部停止此警报,您可以通过重新创建在 AlarmReceiver 中使用的 PendingIntent 并执行 stopService 方法来停止 MyService 。

希望有帮助。

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

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

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.

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文