我希望我的MainActivity.java有一个按钮,当点击它时,它应该执行在MapsActivity.java中编写



我创建了一个MapsActivity和相应的activity_maps.xml.My activity_main.xml,当点击时,会有一个按钮显示标记指向的当前位置。

当在AndroidManifest.xml中将MapsActivity作为Launcher Activity时,代码运行良好,但希望我的MainActivity成为Launcher Activity。

如何将我的MainActivity设置为Launcher Activity。

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
public void onMapReady(GoogleMap googleMap) {
Toast.makeText(this, "inside onMapReady()", Toast.LENGTH_SHORT).show();//doesn't appear
Log.i("hurray:","inside onMapReady()");//doesn't appear
mMap = googleMap;
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
if(location != null){
Toast.makeText(MapsActivity.this, "Location is not null", Toast.LENGTH_SHORT).show();//doesn't appear
LatLng userLatLngLocation = new LatLng(location.getLatitude(),location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(userLatLngLocation);
markerOptions.title("Current position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ORANGE));
mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(userLatLngLocation));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10));
Toast.makeText(getApplicationContext(), location.toString(), Toast.LENGTH_SHORT).show();//does appears sometimes
}
else{
Toast.makeText(getApplicationContext(), "location is null !", Toast.LENGTH_SHORT).show();
}
}
public void requestLocationPermission() {
//if permission not granted ask for it
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
//else if location permission is already granted get location updates
else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);//every 0secs & 0meters
}
}
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, int grantResults[]) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
}
}
}

MainActivity.java:

public class MainActivity extends AppCompatActivity {
Button getPopLocationButton;//button to show the Maps
MapsActivity mapsActivity;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_screen);
mapsActivity = new MapsActivity();
getPopLocationButton = findViewById(R.id.buttonPopLocation); 

getPopLocationButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.activity_maps); 
}
});
}
}

好吧,

public void onClick(View view){
setContentView(R.layout.map); 
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(MainActivity.this);}

最新更新