0

I am trying to implement alarm application using AlarmManager and Service

In my Activity, i have declared the following to initiate a alarmservice

Intent intent = new Intent(getApplicationContext(), OnAlarmManager.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
        234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pendingIntent);
Toast.makeText(getApplicationContext(), "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();

My OnAlarmManager code looks like this

public class OnAlarmManager extends Service{
    private static final String TAG = "OnAlarmReceiver";

    private NotificationManager nm;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent mainActivityIntent = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, mainActivityIntent, 0);

        //setting up notification
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("New Notification")
                .setContentText("This is a successfull app on service")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        nm.notify(0, notification);
        Toast.makeText(getApplicationContext(), "Alarm has begun", Toast.LENGTH_LONG).show();
        Log.i(TAG, "Alarm has begun");
         return START_STICKY;
    }
}

Now when i try running this, the notification is triggered at given time in device running on JellyBean even when i close the app after setting the alarm. But in the device running on Marshmallow, the service class is never triggered when the app is closed

Is the something i am missing here?

Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
  • You may want to check this questions first https://stackoverflow.com/questions/34378707/alarm-manager-does-not-work-in-background-on-android-6-0 – Roman Gherta Jun 03 '17 at 11:13
  • does not help. Notification is triggered when app is running or when it is running in background. But once i swipe the app away or close it, everything associated with it(receiver, service) is being destroyed – Abhishek Koirala Jun 03 '17 at 11:30

3 Answers3

1

Could you try foreground Services? http://www.vogella.com/tutorials/AndroidServices/article.html

From what you say it seems like the service is running on the main thread and it gets killed together with the app.

Roman Gherta
  • 821
  • 2
  • 15
  • 27
0

Just for the sake, try using AlarmManager.setExact with IntentService. Your code will look something like this:

Intent intent = new Intent(OnAlarmManager.ACTION_TRIGGER_ALARM);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),
        234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Toast.makeText(getApplicationContext(), "Alarm set in " + i + " seconds", Toast.LENGTH_LONG).show();

if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
    alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pendingIntent);
} else {
    alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+(i * 1000), pendingIntent);
}

And your service will look something like:

public class OnAlarmManager extends IntentService {
    public static final String ACTION_TRIGGER_ALARM = "com.example.myapplication.ACTION_TRIGGER_ALARM";

    private static final String TAG = "OnAlarmReceiver";

    private NotificationManager nm;

    public OnAlarmManager() {
         super("OnAlarmManager");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent mainActivityIntent = new Intent(this, MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 0, mainActivityIntent, 0);

        //setting up notification
        Notification notification = new NotificationCompat.Builder(this)
                .setContentTitle("New Notification")
                .setContentText("This is a successfull app on service")
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentIntent(pIntent)
                .build();
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        nm.notify(0, notification);
        Log.i(TAG, "Alarm has begun");
    }
}

and add the action information in your AndroidManifest.xml:

<service
    android:name=".OnAlarmManager"
    android:exported="true">
    <intent-filter>
        <action android:name="com.example.myapplication.ACTION_TRIGGER_ALARM" />
    </intent-filter>
</service>
Kamran Ahmed
  • 7,661
  • 4
  • 30
  • 55
  • While using IntentService , its asking me to add a constructor in service class. While i do it, the following constructor class is added public OnAlarmManager(String name) { super(name); } Adding this part gives me a error in manifest declaration telling me that there is no default constructor in Onalarmmanager. Also the app crashes – Abhishek Koirala Jun 03 '17 at 12:15
  • Oh sorry, I missed it in the sample code. Check the updated answer. – Kamran Ahmed Jun 03 '17 at 12:17
  • Added it, just like in the questions i mentioned, the notification is triggered while running in the background , but once i close the app, everything is destroyed – Abhishek Koirala Jun 03 '17 at 12:21
  • Alarms should be triggered, being in foreground or background shouldn't affect its working. That's strange. – Kamran Ahmed Jun 03 '17 at 12:24
  • Which device are you testing it on? – Kamran Ahmed Jun 03 '17 at 12:25
  • Huawai G Play Mini, Marshmallow 6.0 and Xiaomi Redemy Note 2 , Lollipop – Abhishek Koirala Jun 03 '17 at 12:26
  • Can you try using a broadcast receiver in place of that `Service`? I'll update the answer with code for the same if you need. – Kamran Ahmed Jun 03 '17 at 12:29
  • Already tried it. After unsuccessful attempts with BroadcastReceiver, i moved towards service. – Abhishek Koirala Jun 03 '17 at 12:31
  • https://github.com/developeravsk/AlarmClock You may take a look at it. Any suggestion would be of big help – Abhishek Koirala Jun 03 '17 at 12:35
  • Works on my OnePlus2 like a charm. I know about the strict changes in Xiaomi Redmi Note 2 framework that saves a lot of battery by killing stuff in background, not sure about Huawei. AlarmManager should work perfectly in any case though, if that doesn't work, the phones won't be selling so good. – Kamran Ahmed Jun 03 '17 at 12:44
-1

Just register the service in Manifest.xml file as

<service android:name=".YourServiceClassName" />
yogi
  • 293
  • 1
  • 7