0

This is my code:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_getgps);
    Intent cas = getIntent();
    String setcas = cas.getStringExtra(Login.EXTRA_CAS);
    //Toast.makeText(getApplicationContext(), setcas, Toast.LENGTH_SHORT).show(); -na overenie ci sa premenna preniesla z predchadzajucej aktvity
    Timer timer = new Timer( );
        //timer.scheduleAtFixedRate(void getit, long 5000, long 5000);
    timer.schedule(new TimerTask() {
        @Override
        public void run() {


        }
    }, Long.parseLong(setcas));}

        public void getit(Bundle savedInstanceState){
            LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
            LocationListener mlocListener = new MyLocationListener();
            mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, mlocListener);

I would like to call method getit from run. but idk how to do it, when I tried use code from method getit in method run, it didn t accept because it found some error with Bundle savedInstanceState... Can someone what I should do?

Thanks

Filip Glemba
  • 23
  • 1
  • 6

1 Answers1

0

In your onCreate method, declare your Bundle savedInstanceState as final:

protected void onCreate(final Bundle savedInstanceState) {
    //...
}

Now you can do

Timer timer = new Timer( );
timer.schedule(new TimerTask() {
    @Override
    public void run() {
        getit(savedInstanceState);
    }
}, Long.parseLong(setcas));

Edit, using Handler:

Handler handler = new android.os.Handler();
handler.postDelayed(new Runnable() {
    public void run() {
        getit(savedInstanceState);
    }
}, Long.parseLong(setcas));
antonio
  • 18,044
  • 4
  • 45
  • 61
  • This is the correct answer to the OP's specific question, but it calls methods on `LocationManager` in a worker thread. Use a `Handler`, not a `Timer`, for two reasons. First, a `Timer` creates a thread, and a thread is a very heavyweight solution for a simple delay. Second, unless documented otherwise, you should assume that everything in the Android API should be called in the main thread. There is almost never a valid reason to use a `Timer` in Android. – Kevin Krumwiede Nov 14 '15 at 18:05
  • I m using timer because delay will be setable by user. 5 sec is just for test bc part where user set time is not finished yet. – Filip Glemba Nov 14 '15 at 18:07
  • hmm now I have another isue. When I start app in modulator it suddenly crash when method run start, in log it writes error: 11-14 18:37:04.719 20387-20407/? E/GMPM﹕ getGoogleAppId failed with status: 10 11-14 18:37:04.720 20387-20407/? E/GMPM﹕ Uploading is not possible. App measurement disabled It should just post toast with gps location. – Filip Glemba Nov 14 '15 at 18:45
  • It seems that this new error is GCM related. I have never used GCM so I can't help you with it. Maybe this threads can help you http://stackoverflow.com/questions/32812305/android-wear-app-resulting-in-getgoogleappid-failed-with-status-error or http://stackoverflow.com/questions/32879230/cant-get-my-instanceid-with-gcm – antonio Nov 14 '15 at 18:57
  • I will try handler as a kevin said. – Filip Glemba Nov 14 '15 at 19:05
  • I have updater my answer with an example using `Handler` if it's any help – antonio Nov 14 '15 at 19:09
  • You can try like this: final Handler handler = new Handler(); Runnable repeat = new Runnable() {@Override public void run() {getit(savedInstanceState); handler.postDelayed(this, Long.parseLong(setcas));}}; handler.postDelayed(repeat, Long.parseLong(setcas)); but take into account that you will need to make your setcas final – antonio Nov 14 '15 at 21:08
  • So I have tried to use handler: final Handler handler = new Handler(); final Runnable repaet = null; final Runnable repeat = new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "5 sec gone.", Toast.LENGTH_LONG).show(); //getit(savedInstanceState); handler.postDelayed(repaet, 5000/*Long.parseLong(setcas)*/); } }; aplication start, but nothing happend. something is wrong. Can u see my mistake? – Filip Glemba Nov 14 '15 at 21:09
  • damn... now I don t know what I have made wrong. but code u post in ur last comment is working.. but I tried the same before and it reported error on repeat... i am mindfked... thanks – Filip Glemba Nov 14 '15 at 21:19