3

I want to track the tower location of the user hourly. So for that I am now using an alarm manager. With alarm manager sometimes the alarm is getting triggered sometimes it does not.

Also there is problem in xiomi devices, if the app is killed from ram then the alarm does not trigger.

Is there any other way to do this? Can I run a service continuous and track the location hourly?

I want a solution where in a service I should be able to run this task hourly, may be using an handler or timer??

How can I use Job Scheduler for this?

Right Now my attempt with alarm manager :

    public static boolean setAlarm(Context context) {

    Calendar calendarNow = Calendar.getInstance();

    Intent intent = new Intent(context,  SaveLocationReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1321, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendarNow.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);

    ComponentName receiver = new ComponentName(context,  SaveLocationReceiver.class);
    PackageManager pm = context.getPackageManager();

    pm.setComponentEnabledSetting(receiver,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

    return true;
}

SaveLocationReceiver.java:

public class SaveLocationReceiver extends BroadcastReceiver {

    private SharedPreferences preferences;
    String networkSubType="";

    SessionData mSessionData;

    @Override
    public void onReceive(final Context context, Intent intent) {
        Log.d("myapp", "service started");

        mSessionData = new SessionData(context);

        if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                && ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED)
        {

            Calendar calendarSet = Calendar.getInstance();
            Calendar calendarNow = Calendar.getInstance();

            calendarSet.set(Calendar.HOUR_OF_DAY, 7); // hour
            calendarSet.set(Calendar.MINUTE, 00); // minute
            calendarSet.set(Calendar.SECOND, 0); // second

            Calendar calendarEnd = Calendar.getInstance();

            calendarEnd.set(Calendar.HOUR_OF_DAY, 20); // hour
            calendarEnd.set(Calendar.MINUTE, 00); // minute
            calendarEnd.set(Calendar.SECOND, 0); // second


            SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println(calendarNow.getTime());
// Output "Wed Sep 26 14:23:28 EST 2012"

            String currentTime = format1.format(calendarNow.getTime());
            System.out.println(currentTime);

            if (calendarNow.compareTo(calendarSet) >= 0 && calendarNow.compareTo(calendarEnd) <= 0) {

        TelephonyManager telephonyManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
        GsmCellLocation cellLocation = (GsmCellLocation) telephonyManager.getCellLocation();

        ConnectivityManager connectivityManager = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);//?????????
        NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();

        if (cellLocation != null) {

            int cellid = cellLocation.getCid();
            int celllac = cellLocation.getLac();

            if(activeNetInfo != null) {
               networkSubType = activeNetInfo.getSubtypeName();
            }

            String networkOperator = telephonyManager.getNetworkOperator();

            int mcc = Integer.parseInt(networkOperator.substring(0, 3));
            int mnc = Integer.parseInt(networkOperator.substring(3));

            String networkOperatorName = telephonyManager.getNetworkOperatorName();
            int type = telephonyManager.getNetworkType();


            Log.d("CellLocation", cellLocation.toString());
            Log.d("GSM CELL ID", String.valueOf(cellid));
            Log.d("GSM Location Code", String.valueOf(celllac));


            Log.d("MCC", String.valueOf(mcc));
            Log.d("MNC", String.valueOf(mnc));
            Log.d("NetworkOperatorName", networkOperatorName);
            Log.d("radioType", String.valueOf(type));
            Log.d("Network subtype name", networkSubType);


            AddLocationAsyncTask addLocationAsyncTask = new AddLocationAsyncTask(context);
            addLocationAsyncTask.execute(mSessionData.getString("user_id",""), String.valueOf(cellid), String.valueOf(celllac), String.valueOf(mcc), String.valueOf(mnc),
                    networkOperatorName, networkSubType, currentTime);

        }

        }

        }
    }
}

So I want above task of fetching the location to run exactly after half an hour without miss for any reason.

Michal Kordas
  • 10,475
  • 7
  • 58
  • 103
Sid
  • 2,792
  • 9
  • 55
  • 111
  • Try making your service a foreground service...the chances of it being killed by the system reduces – raxerz Mar 09 '18 at 05:42
  • I am not using a service right now, I am using a broadcastReceiver. I want to know how can I do this in a service? @raxerz – Sid Mar 09 '18 at 05:46
  • start service , while receiving broadcast message . – iamkdblue Mar 09 '18 at 06:00
  • I miss the broadcastReceiver i.e. alarm sometimes, so how can I start service in a receiver only? I want a solution where in a service I should be able to run this task hourly may be using an handler or timer?? @kdblue – Sid Mar 09 '18 at 06:07
  • The functionality you trying to achieve is blocked by OS, some of Vendors block it more aggressively. So the only way to perform such task is to use JobScheduler on 21+ devices or alarmManager on <21 – Viktor Yakunin Mar 09 '18 at 13:13

0 Answers0