0

i want to use two variable from aff.java in MainActivity.java to take GPS coordinate from SMS. i tried with Gson and i added two other classes profil.java and profilstatic.java but my application didn t work. Please help me.

MainAvtivity.java

public class MainActivity extends Activity {


        double lat1,lon1;    
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              TextView view = new TextView(this);
              Uri uriSMSURI = Uri.parse("content://sms/inbox");
              Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
              String sms = "";
              while (cur.moveToNext()) {

                  String spn ="+21626938812";
                  String phNum =cur.getString(2);
                  if (spn.equals(phNum)){
                  sms +=cur.getString(12); 
                                  }      
                  }

                 String a =sms.substring(0,10);
                 String b =sms.substring(11,21); 
                 double lat = Double.parseDouble(a);
                 double lon = Double.parseDouble(b);



                lat1=lat;
                lon1=lon;
                Profil profil = new Gson().fromJson("", Profil.class); 
                profil.setLat(lat1);
                profil.setLon(lon1);
                ProfilStatique.setProfil(profil);
                Intent activity_main = new Intent(getApplicationContext(), aff.class) ;

                startActivity(activity_main);

          }




    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

aff.java

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

@SuppressWarnings("deprecation")
public class aff extends MapActivity {
    double lat1,lon1;

    Profil profil = ProfilStatique.getProfil();
    MapView maMap;
    MapController monControler;


    double latitude = profil.getLat() ;
    double longitude = profil.getLon() ;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        maMap = (MapView)findViewById(R.id.myGmap);
        maMap.setBuiltInZoomControls(true);

        GeoPoint point = new GeoPoint (microdegrees(latitude),microdegrees(longitude));

        MonOverlay object = new MonOverlay(getResources().getDrawable(R.drawable.marker_voisinage_rouge));
        object.addPoint(point);
        maMap.getOverlays().add(object);
        maMap.setSatellite(true);

        monControler = maMap.getController();
        monControler.setZoom(12);
        monControler.setCenter(point);

    }


    private int microdegrees (double value){
        return (int)(value*1000000);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    @Override
    protected boolean isRouteDisplayed() {
        // TODO Auto-generated method stub
        return false;
    }
    public class MonOverlay extends ItemizedOverlay<OverlayItem>{

        List<GeoPoint> points = new ArrayList<GeoPoint>();

        public MonOverlay(Drawable arg0) {
            super(boundCenterBottom(arg0));
            // TODO Auto-generated constructor stub
        }

        @Override
        protected OverlayItem createItem(int i) {
            GeoPoint point = points.get(i);
            return new OverlayItem(point,"titre","description");
        }

        @Override
        public int size() {

            return points.size();
        }

        public void addPoint (GeoPoint point){
            this.points.add(point);
            populate();
        }

    }

}

Profil.java

public class Profil {



  public double lat,lon;

public double getLat() {
    return lat;
}

public void setLat(double lat) {
    this.lat = lat;
}

public double getLon() {
    return lon;
}

public void setLon(double lon) {
    this.lon = lon;
}

}

ProfilStatic.java

public class ProfilStatique {

    private static Profil profil = new Profil();

    public static Profil getProfil() {
        return profil;
    }
    public static void setProfil(Profil profil) {
        ProfilStatique.profil = profil;
    }
}
2
  • If you wanting to pass data back to the calling activity, then you should be using startActivityForResult() in MainActivity.java. Commented May 4, 2013 at 23:48
  • i want to pass data to aff.java Commented May 4, 2013 at 23:51

2 Answers 2

1

If you're passing data from one activity to another, it should be done through the intent, via the Bundle. From the Activity that is passing the values, you'd do something like this:

Intent i = new Intent(context, RecievingActivity.class);
i.putExtra("lat", lat1);
i.putExtra("long", lon1);
startActivity(i);

Then, in the recieving activity's onCreate method, you'd do something like this:

Bundle bundle = getIntent().getExtras();
lat1 = bundle.getLong("lat");
lon1 = bundle.getLong("long");
Sign up to request clarification or add additional context in comments.

Comments

0

Best way is to create another class called Store.java and create two public static variables. In that way you will have those variables globally accessible. You can then change or access values in those two variables from anywhere within the application.

Hope this helps.

1 Comment

i am sorry, i didn t understand you, how can i take my variable lat and lon in Store.java? and when i add this classe, should i delete profil.java and profilstatic.java ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.