我做了两项活动,主要活动和GPS活动。
我在主要活动中使用意图定向到GPS活动, 但每当我按下按钮时,它会一直重定向回主要活动。我不知道为什么。
我尝试了其他活动的意图并且成功了。此外,GPS活动单独工作。 有人可以帮忙吗?
我试图创建一个新项目,但它也没有成功 并且它没有显示任何错误或例外。
我正在使用min SDK 21。
主要活动:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button two = (Button) findViewById(R.id.button2);
two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent Intent = new Intent(MainActivity.this,Gps.class);
startActivity( Intent);
}
});
}
GPS活动:
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient.*;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationListener;
public class Gps extends AppCompatActivity implements ConnectionCallbacks, OnConnectionFailedListener, LocationListener {
TextView x;
TextView y;
LocationRequest LR;
private GoogleApiClient GAC;
//public String lat="Latitude";
//public String lon="Longitude";
@Override
public void onConnected(Bundle connectionHint) {
createLocationRequest();
startLocationUpdates();
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
Location Location = LocationServices.FusedLocationApi.getLastLocation(GAC);
if (Location != null) {
Double Lat = Location.getLatitude();
Double lon = Location.getLongitude();
x = (TextView) findViewById(R.id.textView);
y = (TextView) findViewById(R.id.textView2);
x.setText("Ltitude is " + String.valueOf(Lat));
y.setText("Longitude is " + String.valueOf(lon));
}
}
@Override
protected void onStart() {
super.onStart();
if (GAC != null) {
GAC.connect();
}
}
@Override
protected void onStop() {
GAC.disconnect();
super.onStop();
}
@Override
public void onConnectionSuspended(int cause) {
Toast.makeText(this, "the connection suspended", Toast.LENGTH_LONG).show();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Toast.makeText(this, "the connection failed", Toast.LENGTH_LONG).show();
}
@Override
public void onLocationChanged(Location location) {
x = (TextView) findViewById(R.id.textView);
y = (TextView) findViewById(R.id.textView2);
x.setText("latitude is " + String.valueOf(location.getLatitude()));
y.setText("longitude is " + String.valueOf(location.getLongitude()));
}
protected void createLocationRequest() {
LR = new LocationRequest();
LR.setInterval(5000);
LR.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
protected void startLocationUpdates() {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(
GAC, LR, (this));
}
protected void build_GAC(){
GAC =new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
build_GAC();
}
}
app.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "com.example.toshiba.intent"
minSdkVersion 21
targetSdkVersion 24
versionCode 1
versionName "1.0"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
compile 'com.google.android.gms:play-services:10.2.1'
compile 'com.android.support:multidex:1.0.0'
}
答案 0 :(得分:3)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
build_GAC();
}
您在R.layout.activity_main
中使用了主要活动的布局(Gps Activity
),因此活动正在发生变化,但您无法看到差异。