[問題] GPS 運作問題

作者: jerryxin (jerry)   2019-10-10 01:02:09
後來我嘗試了另外一個sample code做修改
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_LOCATION = 1;
Button button;
TextView textView;
LocationManager locationManager;
String lattitude,longitude;
String lattitude2,longitude2;
LocationListener locationListener;
public static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, new
String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
textView = (TextView)findViewById(R.id.text_location);
button = (Button)findViewById(R.id.button_location);
button.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
if
(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
} else if
(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
getLocation();
}
}
});
}
private void getLocation() {
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission
(MainActivity.this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new
String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Location location =
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Location location1 =
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location location2 =
locationManager.getLastKnownLocation(LocationManager. PASSIVE_PROVIDER);
locationManager.requestLocationUpdates("gps",5000,0,locationListener);
if (location != null) {
double latti = location.getLatitude();
double longi = location.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
textView.setText("Your current location is"+ "\n" +
"Lattitude = " + lattitude
+ "\n" + "Longitude = " + longitude);
Log.i(TAG,"location");
} else if (location1 != null) {
double latti = location1.getLatitude();
double longi = location1.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
Log.i(TAG,"location1");
textView.setText("Your current location is"+ "\n" +
"Lattitude = " + lattitude
+ "\n" + "Longitude = " + longitude);
} else if (location2 != null) {
double latti = location2.getLatitude();
double longi = location2.getLongitude();
lattitude = String.valueOf(latti);
longitude = String.valueOf(longi);
textView.setText("Your current location is"+ "\n" +
"Lattitude = " + lattitude
+ "\n" + "Longitude = " + longitude);
Log.i(TAG,"location2");
}else{
Toast.makeText(this,"Unble to Trace your
location",Toast.LENGTH_SHORT).show();
Log.i(TAG,"Unable");
}
locationListener =new LocationListener() {
@Override
public void onLocationChanged(Location location) {
double latti2 = location.getLatitude();
double longi2 = location.getLongitude();
lattitude2 = String.valueOf(latti2);
longitude2 = String.valueOf(longi2);
Log.i(TAG,"location1");
textView.setText("Your current location is"+ "\n" +
"Lattitude = " + lattitude2
+ "\n" + "Longitude = " + longitude2);
}
public void onStatusChanged (String provider,
int status,
Bundle extras){
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
};
}
}
protected void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Please Turn ON your GPS Connection")
.setCancelable(false)
.setPositiveButton("Yes", new
DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final
int id) {
startActivity(new
Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new
DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, final
int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
這個sample code原來是沒有locationListener
也沒有locationManager.requestLocationUpdates(,,,)
加了這串以後
logcat顯示出錯誤
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.currentgps, PID: 18899
java.lang.IllegalArgumentException: invalid listener: null
at
android.location.LocationManager.checkListener(LocationManager.java:2344)
at
android.location.LocationManager.requestLocationUpdates(LocationManager.java:557)
at
com.example.currentgps.MainActivity.getLocation(MainActivity.java:87)
at
com.example.currentgps.MainActivity.access$000(MainActivity.java:31)
at com.example.currentgps.MainActivity$1.onClick(MainActivity.java:60)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I/Process: Sending signal. PID: 18899 SIG: 9
Process 18899 terminated.
這個sample code能成功顯示出目前的GPS location
但是我希望5秒更新一次
後續我想要把所有的GPS location記錄下來,然後發到email上
但是目前嘗試了很多
看了網上的教學
只要用到locationManager.requestLocationUpdates()就不成功
可以請高手幫忙看看這個報錯是哪裡有問題嗎?
作者: LZN (秋)   2019-10-14 11:15:00
locationListener還沒init就被調用了

Links booklink

Contact Us: admin [ a t ] ucptt.com