0

我想使用 in 中的两个变量 从 SMSaff.javaMainActivity.java获取 GPS 坐标。我尝试使用 Gson 并添加了另外两个类profil.javaprofilstatic.java但我的应用程序不起作用。请帮我。

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

    }

}

简介.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;
    }
}
4

2 回答 2

1

如果您将数据从一个活动传递到另一个活动,则应该通过意图通过 Bundle 来完成。从传递值的 Activity 中,您可以执行以下操作:

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

然后,在接收活动的 onCreate 方法中,您将执行以下操作:

Bundle bundle = getIntent().getExtras();
lat1 = bundle.getLong("lat");
lon1 = bundle.getLong("long");
于 2013-05-05T01:32:41.143 回答
0

最好的方法是创建另一个名为 Store.java 的类并创建两个公共静态变量。这样,您将可以全局访问这些变量。然后,您可以从应用程序中的任何位置更改或访问这两个变量中的值。

希望这可以帮助。

于 2013-05-04T23:52:07.473 回答