我在启动 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); }更多推荐
待定意向服务
发布评论