Waiting for an intention to receive a service

I am having trouble starting my pendingIntent . I have some problems using logcat, etc., and in the end I am pretty sure that my problem is really in my pendingIntent method. The times that I set are correct, and the method gets called, but nothing happens at the appointed time. Here is the method I use to create 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(); } 

Here is the class of service:

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

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

+4
source share
1 answer

I thought! The problem was in the Service class, I also changed some other things. However, I believe that the main problem was that in my service class in the onCreate method onCreate I tried to run my code. But it needs 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 to the class starting the service, as shown 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); } 
+6
source

Source: https://habr.com/ru/post/1494385/


All Articles