待定意向服务

编程入门 行业动态 更新时间:2024-10-25 08:18:31
本文介绍了待定意向服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在启动 pendingIntent 时遇到问题.我已经使用logcat等进行了一些故障排除,最后我几乎肯定我的问题实际上出在我的 pendingIntent 方法中.我设置的时间是正确的,并且该方法被调用,但是在计划的时间没有任何反应.这是我用来创建 pendingIntent

I am having a problem getting my pendingIntent to fire. I have done some troubleshooting using the logcat etc. and in the end I am almost positive that my problem is actually in my pendingIntent method. The times I have set are correct, and the method is getting called, but nothing happens at the scheduled times. Here is the method that I use to create the pendingIntent

public void scheduleAlarm(){ Log.d("Alarm scheduler","Alarm is being scheduled"); Intent changeVol = new Intent(); changeVol.setClass(this, VolumeService.class); PendingIntent sender = PendingIntent.getService(this, 0, changeVol, 0); AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, time, sender); //Toast.makeText(this, "Volume Adjusted!", Toast.LENGTH_LONG).show(); }

这是服务类别:

public class VolumeService extends Service{ @Override public void onCreate() { super.onCreate(); Log.d("Service", "Service has been called."); Toast.makeText(getApplicationContext(), "Service Called!", Toast.LENGTH_LONG).show(); } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } }

scheduleAlarm()类中的日志按我的计划工作,但是什么也没发生,所以我认为它是我的 pendingIntent .预先感谢!

The log in the scheduleAlarm() class is working as I planned but then nothing happens, so I assume it is my pendingIntent. Thanks in advance!

推荐答案

想通了!问题出在Service类中,我也改变了其他一些东西.但是,我认为主要的问题是在服务类的 onCreate 方法中,我试图运行代码.但这需要在 onStartCommand 方法

Figured it out! The problem was in the Service class, I changed a few other things around too. However, I believe the main problem was that in my service class in the onCreate method I was trying to run my code. But this needed to be done in the onStartCommand method

public class VolumeService extends Service{ @Override public void onCreate() { super.onCreate(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Toast.makeText(getApplicationContext(), "Service started", Toast.LENGTH_LONG).show(); return START_NOT_STICKY; } @Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } }

并在启动服务的类中进行了一些更改,如下所示:

and a few changes were made in the class starting the service as seen here:

public void scheduleAlarm(){ Log.d("Alarm scheduler","Alarm is being scheduled"); Intent intent = new Intent(AlarmSettings.this, VolumeService.class); PendingIntent pintent = PendingIntent.getService(AlarmSettings.this, 0, intent, 0); AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE); alarm.set(AlarmManager.RTC_WAKEUP, time, pintent); }

更多推荐

待定意向服务

本文发布于:2023-11-27 06:53:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:待定   意向

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!