2015-05-06 71 views
0

我想調用MainActivity類到另一個活動。這是我對MainActivity.java代碼:如何從另一個活動調用課程?

package com.blinkedup.geolocationchat; 

import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.Service; 
import android.view.Menu; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    TextView textView; 
    LocationManager locationManager; 
    MyLocationListener locationListener = new MyLocationListener(); 

    Criteria criteria; 
    String bestProvider; 
    String listOfBestProviders; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     criteria = new Criteria(); 
     textView = (TextView) findViewById(R.id.textView1); 

     criteria.setAccuracy(Criteria.ACCURACY_COARSE); 
     criteria.setPowerRequirement(Criteria.POWER_LOW); 
     criteria.setAltitudeRequired(false); 
     criteria.setBearingRequired(false); 
     criteria.setCostAllowed(true); 

     locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE); 
     //locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 

     locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

     bestProvider = locationManager.getBestProvider(criteria, true); 

     Toast.makeText(getApplicationContext(), bestProvider, 3).show(); 
    } 

    protected void onPause(){ 
     super.onPause(); 
     locationManager.removeUpdates(locationListener); 
    } 

    private class MyLocationListener implements LocationListener{ 

     @Override 
     public void onLocationChanged(Location location) { 
      // TODO Auto-generated method stub 
      textView.setText("Latitude: " + location.getLatitude() + 
        "Longitude: " + location.getLongitude()); 
     } 

     @Override 
     public void onProviderDisabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onProviderEnabled(String provider) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      // TODO Auto-generated method stub 

     } 

    } 

} 

在另一個活動,我想打電話給lat和長上面的代碼,但不希望將代碼重寫到活動中。我只想調用它並在另一個活動中顯示結果。請幫忙。謝謝

回答

0

使你的變量是靜態的,並在onCreate()方法通過像這樣的調用在另一個活動中使用該變量來定義它。

YourMainActivity.yourstaticvariable 
相關問題