2013-05-04 108 views
0

我想使用aff.java中的兩個變量MainActivity.java來從SMS獲取GPS座標。我嘗試了Gson,並且添加了兩個其他類profil.javaprofilstatic.java,但我的應用程序沒有工作。請幫幫我。Android - 使用另一個類的變量

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

如果你想要將數據傳遞迴調用活動,那麼您應該在'MainActivity.java'中使用'startActivityForResult()'。 – 2013-05-04 23:48:18

+0

我想將數據傳遞給aff.java – user2207848 2013-05-04 23:51:17

回答

1

如果您將數據從一個活動傳遞到另一個活動,則應通過Bundle通過intent完成。從所傳遞的價值觀的活動,你會做這樣的事情:

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

然後,在recieving活動的onCreate方法,你會做這樣的事情:

Bundle bundle = getIntent().getExtras(); 
lat1 = bundle.getLong("lat"); 
lon1 = bundle.getLong("long"); 
0

最好的方法是創建另一個名爲Store.java的類並創建兩個公共靜態變量。這樣你就可以在全局訪問這些變量。然後,您可以從應用程序中的任何位置更改或訪問這兩個變量中的值。

希望這會有所幫助。

+0

我很抱歉,我沒有理解你,我如何將Store變量lat和lon放在Store.java中?當我添加這個類,我應該刪除profil.java和profilstatic.java? – user2207848 2013-05-04 23:58:00