0

When I start my app I show a splashscreen where I show my logo and in the background I'm getting the GPS coordinations (latitude and longitude). Afterwards it goes to my MainFragmentActivity where I set my viewpager, FragmentManager and MyFragmentPagerAdapter. I set the fragments up in MyFragmentPagerAdapter. And that is my problem, I can't seem to get the GPS coordinations to my fragment.

A brief summary: GPS coordinations are calculated in the splashscreen, after splashscreen is done MainFragmentActivity opens up and everything is being setted up so my viewpager works. Then I want to be able to access those GPS coordinations that are calculated in my splashscreen in my fragments. I tried passing them by extra, but my fragment isn't being opened by an intent startActivty. So I'm stuck here.

SplashScreen

//Calculating GPS coordinations ...

     CountDown tik;
     tik = new CountDown(3000, 1000, this, MainActivity.class);
     tik.start();
     StartAnimations();

CountDown

//After SplashScreen is done, start MainActivity
public class CountDown extends CountDownTimer{
    private Activity myActivity;
    private Class myClass;

    public CountDown(long millisInFuture, long countDownInterval, Activity act, Class cls) {
        super(millisInFuture, countDownInterval);
        myActivity = act;
        myClass = cls;
    }

    @Override
    public void onFinish() {
        myActivity.startActivity(new Intent(myActivity, myClass));
        myActivity.finish();
    }

    @Override
    public void onTick(long millisUntilFinished) {}
}

MainActivity

//Setting viewpager up

public class MainActivity extends FragmentActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Tried getting coordinations like this and then passing it to fragment, but didn't succeed.
        /*Bundle extras = getIntent().getExtras();
        myLat = Double.toString(extras.getDouble("lat"));
        myLong = Double.toString(extras.getDouble("lng"));*/

        /** Getting a reference to the ViewPager defined the layout file */
        final ViewPager pager = (ViewPager) findViewById(R.id.pager);

        /** Getting fragment manager */
        FragmentManager fm = getSupportFragmentManager();

        /** Instantiating FragmentPagerAdapter */
        MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);

        /** Setting the pagerAdapter to the pager object */
        pager.setAdapter(pagerAdapter);

        pager.setPageTransformer(true, new ZoomOutPageTransformer());

        PageListener pagelistener = new PageListener();
        pager.setOnPageChangeListener(pagelistener);
}

MyFragmentPagerAdapter

public class MyFragmentPagerAdapter extends FragmentPagerAdapter{

    final int PAGE_COUNT = 6;

    /** Constructor of the class */
    public MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    /** This method will be invoked when a page is requested to create */
    @Override
    public Fragment getItem(int arg0) {

        switch(arg0){

        case 0:

            return new FragmentOne();           

        case 1:
            return new FragmentTwo();

        case 2:
            return new FragmentThree();

        case 3:
            return new FragmentFour();

        case 4:
            return new FragmentFive();

        case 5:
            return new SettingsFragment();          

        default:
            return null;

        }       
    }

    /** Returns the number of pages */
    @Override
    public int getCount() {
        return PAGE_COUNT;
    }    
}

**Example of a Fragment, in this instance I just used FragmentOne

public class FragmentOneextends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        //Want to be able to access those coordinations here.
        View v = inflater.inflate(R.layout.frag_one, container, false);
    }
}

LocationHelper

    public class LocationHelper extends Activity{
        private static LocationHelper mInstance = new LocationHelper();
        private LatLng mLocation;
        public static double location_latitude;
        public static double location_longitude;
        LocationManager locationManager;
        LocationListener locationListener;

        private LocationHelper() {
        super();
            // start getting location
     // Acquire a reference to the system Location Manager
            locationManager = (LocationManager) this
                    .getSystemService(Context.LOCATION_SERVICE);
    //If I remove 'extends Activity, then I get this error: The method getSystemService(String) is undefined for the type LocationHelper

            // Define a listener that responds to location updates
            locationListener = new LocationListener() {
                public void onLocationChanged(Location location) {
                    // Called when a new location is found by the network location
                    // provider.
                    location_latitude = location.getLatitude();
                    location_longitude = location.getLongitude();

                    Log.v("Location", "set" + location.getLatitude());

                    if (location.getLatitude() != 0.0) {
                        Log.v("Location", "stop looking");
                        locationManager.removeUpdates(locationListener);

                        FileOutputStream fos;
                        try {
//Getting the same error here The method openFileOutput(String, int) is undefined for the type new LocationListener(){}
                            fos = openFileOutput("my_latitude", Context.MODE_PRIVATE);
                            fos.write((""+location_latitude).getBytes());
                            fos.close();

                            fos = openFileOutput("my_longitude", Context.MODE_PRIVATE);
                            fos.write((""+location_longitude).getBytes());
                            fos.close();

                        } catch (Exception e) {
                            e.printStackTrace();
                        }

                    } else {
                        Log.v("Location", "keep looking");
                    }
                }

                public void onStatusChanged(String provider, int status,
                        Bundle extras) {
                    Log.v("Location", provider + ", " + status + " Status changed");
                }

                public void onProviderEnabled(String provider) {
                    Log.v("Location", provider + " onProviderEnabled");
                }

                public void onProviderDisabled(String provider) {
                    Log.v("Location", provider + " onProviderDisabled");
                }
            };
        }

        public static LocationHelper getInstance() {
            return mInstance;
        }

        public LatLng getLocation() {
            return mLocation;
        }

    }
4

2 回答 2

1

问题是你没有把纬度和经度放在Intent你用来开始你的活动的地方。你需要这样的东西:

@Override
public void onFinish() {
    Intent intent = new Intent(myActivity, myClass);
    intent.addExtra("lat", latitude);
    intent.addExtra("lon", longitude);
    myActivity.startActivity(intent);
    myActivity.finish();
}

要将数据从您传递Activity给您,Fragment您需要调用setArguments(Bundle args)您的Fragment,然后传入Bundle您从Intent. 像这样的东西:

myFragment.setArguments(getIntent().getExtras());
于 2013-06-05T14:38:29.727 回答
0

我会考虑一种解决方案,它不涉及您始终通过 Intents 和 Bundles 将值传递给所有活动和片段。这是一种这样的方法:

制作一个封装与位置相关的代码的单例:

public class LocationHelper {
    private static LocationHelper mInstance = new LocationHelper();
    private LatLng mLocation;

    private LocationHelper() {
    super();
        // start getting location
    }

    public static LocationHelper getInstance() {
        return mInstance;
    }

    public LatLng getLocation() {
        return mLocation;
    }

    /* ... */
}

LocationHelper.getInstance()当您的应用程序开始初始化它并开始获取位置时调用。每当您想使用该位置时,只需获取单例并调用getLocation()它即可。您可以在任何地方执行此操作。

LocationHelper helper = LocationHelper.getInstance();
LatLng location = helper.getLocation();
于 2013-06-05T14:58:31.303 回答