在服务

时间:2018-03-09 05:33:10

标签: java android service alarmmanager google-geolocation

我想每小时跟踪用户的塔位置。所以我现在正在使用一个报警管理器。有时警报管理器会触发警报,有时则不会触发警报。

xiomi设备也存在问题,如果应用程序被ram杀死,则警报不会触发。

还有其他办法吗?我可以连续运行服务并按小时跟踪位置吗?

我想要一个解决方案,在服务中我应该能够每小时运行一次这个任务,可能正在使用处理程序或计时器?

如何使用Job Scheduler进行此操作?

现在我尝试使用闹钟管理器:

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

        }

        }

        }
    }
}

所以我希望上面的任务是在半小时之后准确地获取位置而不会因为任何原因而错过。

0 个答案:

没有答案