numberformat exception using alarmamanager - crashes before on start

前端 未结 3 419
走了就别回头了
走了就别回头了 2020-12-21 22:15

---------UPDATE -----------------------------------------

When the app starts I receive numberformat exception at line :

final long thetime=Long.par         


        
3条回答
  •  醉梦人生
    2020-12-21 22:47

    how can I select to send the data in time intervals that the user will define
    

    You can use Timer and TimerTask. Take the user input for time interval and pass it to schedule() of Timer. Thing that should be taken into consideration is when app is closed or user changes time interval then cancel previous TimerTask and purge Timer, if any.

    For Alarm, do the following in your activity class:

    PendingIntent Sender = null;
            Intent AlarmIntent = new Intent("com.example.gpstrackdemo.RECEIVEALARM");
            AlarmManager AlmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
            Sender = PendingIntent.getBroadcast(GpsTrackActivity.this, 0,
                    AlarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            AlmMgr.setRepeating(AlarmManager.RTC_WAKEUP, 0, 15 * 60 * 1000,
                    Sender);
    

    AlarmIntent is the intent for BroadcastReceiver. Here is its code:

    public class StartServiceReceiver extends BroadcastReceiver 
    {
    private static final String TAG = "StartServiceReceiver";
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Intent serviceIntent = new Intent(context, MyLocationService.class);
        context.startService(serviceIntent);
        Log.v(TAG, "onReceive called");
    }
    }
    

    On receiving the broadcast, it will start Location service, in which we will get current location of user.

    Service Class:

    public class MyLocationService extends Service implements
        OnLocationReceivedListener {
    
    private LocationManager manager;
    private Location location = null;
    PowerManager powerManager;
    private WakeLock wakeLock;
    private String country;
    GPSLocationListener mGPSLocationListener;
    NetworkLocationListener mNetworkLocationListener;
    private static final int MAX_ATTEMPTS = 250;
    private static String TAG = "MyLocationService";
    LocTimerTask mTimerTask;
    int mSattelites;
    Timer myLocTimer;
    int count = 0;
    boolean isGPS;
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.v(TAG, "onStartCommand called");
        getCurrentLocation();
        return START_STICKY;
    }
    
    @Override
    public void onCreate() {
        Log.v(TAG, "onCreate called");
        powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
                "mywakelock");
        mGPSLocationListener = new GPSLocationListener();
        mNetworkLocationListener = new NetworkLocationListener();
        wakeLock.acquire();
        super.onCreate();
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    public void getCurrentLocation() {
        manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        manager.addGpsStatusListener(mGPSStatusListener);
        mTimerTask = new LocTimerTask(LocationManager.GPS_PROVIDER);
    
        if (manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
            Log.v(TAG, "GPS ENABLED");
            manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L,
                    50.0f, mGPSLocationListener);
        }
    
        if(manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
            manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000L,
                    50.0f, mNetworkLocationListener);
        }
    
        myLocTimer = new Timer("LocationRunner", true);
        myLocTimer.schedule(mTimerTask, 0, 500);
    }
    
    public class GPSLocationListener implements LocationListener {
    
        @Override
        public void onLocationChanged(Location argLocation) {
            location = argLocation;
        }
    
        public void onProviderDisabled(String provider) {
    
        }
    
        public void onProviderEnabled(String provider) {
    
        }
    
        public void onStatusChanged(String provider, int status, Bundle extras) {
    
        }
    }
    
    public class NetworkLocationListener implements LocationListener {
    
        @Override
        public void onLocationChanged(Location argLocation) {
            location = argLocation;
        }
    
        public void onProviderDisabled(String provider) {
    
        }
    
        public void onProviderEnabled(String provider) {
    
        }
    
        public void onStatusChanged(String provider, int status, Bundle extras) {
    
        }
    }
    class LocTimerTask extends TimerTask {
        String provider;
    
        public LocTimerTask(String provider) {
            this.provider = provider;
        }
    
        final Handler mHandler = new Handler(Looper.getMainLooper());
    
        Runnable r = new Runnable() {
            @Override
            public void run() {
                count++;
                Log.v(TAG, "Timer Task run" + i);
                location = manager.getLastKnownLocation(provider);
    
                if (location != null) {
                    Log.v(TAG, "in timer task run in if location not null");
                    onLocationReceived(location);
                    myLocTimer.cancel();
                    myLocTimer.purge();
                    mTimerTask.cancel();
                    return;
                } else {
                    isGPS = false;
                    if (location == null && count == MAX_ATTEMPTS) {
                        turnGPSOff();
                        location = manager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
    
                            onLocationReceived(location);
                            myLocTimer.cancel();
                            myLocTimer.purge();
                            mTimerTask.cancel();
                            return;
                        }
                    } else {
                        return;
                    }
                }
                count = 0;
            }
        };
    
        public void run() {
            mHandler.post(r);
        }
    }
    
    private GpsStatus.Listener mGPSStatusListener = new GpsStatus.Listener() {
    
        @Override
        public synchronized void onGpsStatusChanged(int event) {
            switch (event) {
            case GpsStatus.GPS_EVENT_SATELLITE_STATUS:
                GpsStatus status = manager.getGpsStatus(null);
                mSattelites = 0;
                Iterable list = status.getSatellites();
                for (GpsSatellite satellite : list) {
                    if (satellite.usedInFix()) {
                        mSattelites++;
                    }
                }
                break;
    
            case GpsStatus.GPS_EVENT_FIRST_FIX:
                break;
    
            case GpsStatus.GPS_EVENT_STARTED:
                break;
    
            case GpsStatus.GPS_EVENT_STOPPED:
                break;
            default:
                break;
            }
        }
    };
    
    public void onDestroy() {
        super.onDestroy();
        if (myLocTimer != null) {
            myLocTimer.cancel();
            myLocTimer.purge(); //Timer not required any more
        }
    
        if (mTimerTask != null) {
            if (mTimerTask.r != null) {
                mTimerTask.mHandler.removeCallbacks(mTimerTask.r);
            }
        }
    
        if (manager != null) {
            if (mGPSLocationListener != null) {
                manager.removeUpdates(mGPSLocationListener);
            }
    
            //remove location updates for listener once your work is done, otherwise it will drain battery
            if (mNetworkLocationListener != null) {
                manager.removeUpdates(mNetworkLocationListener);
            }
    
            if (mGPSStatusListener != null) {
                manager.removeGpsStatusListener(mGPSStatusListener);
            }
        }
    }
    
    @Override
    public void onLocationReceived(Location mLoc) {
        //Send data to http server once you get location.
    }
    }
    

    Here my service class implements a listener which have a callback method onLocationReceived in which you can do your stuff after you get location.

    public interface OnLocationReceivedListener {
        public void onLocationReceived(Location mLoc);
    }
    

    And in your manifest, declare broadcast receiver and service respectively:

    
            
                
    
            
        
    
    

提交回复
热议问题