Harry T.
Harry T.

Reputation: 3527

Running a service with Calendar time checking

I'm writing an application about notification. I want to notify user at 6PM everyday (no matter minute or sec) even if they closed app. I used a Service to let my program run and cannot be close (Service -> onStartCommand -> STICKY). I know in function onStartCommand can help me check the time. But it only can check 1 time, not everytime. I'm finding the way to writing some code lines to my service class to let it check everytime. This is my Service class:

package com.truonghau.smstoxls.service;

import android.app.NotificationManager;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.truonghau.xmeasure.commons.Constants;
import com.truonghau.xmeasure.commons.SmSUtils;

import java.util.Calendar;

/**
 * Created by HieuK on 12/05/16.
 */
public class Services extends Service {
    private static final String LOG_TAG = "ForegroundService";
    public static boolean IS_SERVICE_RUNNING = false;

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

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        NotificationManager mNM = (NotificationManager)getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        if (intent.getAction().equals("BD"))
        {
            Calendar cal = Calendar.getInstance();
            int h18 = cal.get(Calendar.HOUR_OF_DAY);
            if (h18 == 18)
            {
                //solve
            }
            SmSUtils.showNotification(getApplicationContext(),mNM,"Started",false);
        }
        if (intent.getAction().equals("KT"))
        {
            SmSUtils.showNotification(getApplicationContext(),mNM,"Ended",false);
            stopForeground(true);
            stopSelf();
        }

        return START_STICKY;
    }
}

Upvotes: 0

Views: 63

Answers (2)

Md. Sajedul Karim
Md. Sajedul Karim

Reputation: 7075

AlarmManager is best things to do your staff but you have to do more things. Those are:

When you reboot the device your service didn't start automatically. To start your service you have to add a broadcast receiver that will fire when device restart. in onReceive of your broadcast receiver you just start your service. Here is sample code:

    public class BootStartUpReciever extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            //start your service here
        }}

Add this line of code in your menifest.xml file

            <receiver
                android:name="MY_PACKAGE_NAME.BootStartUpReciever"
                android:enabled="true"
                android:exported="true" >
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED" />
                    <action android:name="android.intent.action.QUICKBOOT_POWERON" />
                </intent-filter>
            </receiver>

Hope, this will help you :)

Upvotes: 1

Oleksandr
Oleksandr

Reputation: 902

Will be better using alarm manager. Also read this tutorial.

Upvotes: 0

Related Questions