我想在aff.java
中使用MainActivity.java
中的两个变量来从SMS获取GPS坐标。我尝试使用Gson,我添加了另外两个类profil.java
和profilstatic.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();
}
}
}
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;
}
}
答案 0 :(得分:1)
如果您将数据从一个活动传递到另一个活动,则应通过Bundle通过意图完成。从传递值的活动中,您可以执行以下操作:
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");
答案 1 :(得分:0)
最好的方法是创建另一个名为Store.java的类并创建两个公共静态变量。通过这种方式,您可以全局访问这些变量。然后,您可以从应用程序中的任何位置更改或访问这两个变量中的值。
希望这有帮助。