package com.example.simranjeet.myapplication43;
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class GpsLocation extends AppCompatActivity {
Context context;
private LocationManager locationManager;
private LocationListener listener;
void findloc(){
locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
listener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
};
configure_button();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 10:
configure_button();
break;
default:
break;
}
}
void configure_button(){
// first check for permissions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.INTERNET}
,10);
}
return;
}
locationManager.requestLocationUpdates("gps", 500, 0, listener);
// locationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER,listener,null);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Toast.makeText(context,"location finded",Toast.LENGTH_LONG ).show();
locationManager.removeUpdates(listener);
// Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
}
我从findloc
方法中调用onCreate
方法为
s = new GpsLocation();
s.findloc();
每次我调用此函数时,它都会崩溃,但是当它运行时它运行良好 不放在单独的类中。我添加了所有权限,它是 在onCreate方法下,它工作得很好,谁能告诉我为什么它突然崩溃了。
CRASH LOG //我正在使用模拟器
进程:com.example.simranjeet.myapplication43,PID:12956 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.simranjeet.myapplication43 / com.example.simranjeet.myapplication43.MainActivity}: java.lang.IllegalStateException:系统服务不适用于 onCreate()之前的活动 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 原因:java.lang.IllegalStateException:onCreate()之前Activities无法使用系统服务 在android.app.Activity.getSystemService(Activity.java:5253) 在com.example.simranjeet.myapplication43.GpsLocation.findloc(GpsLocation.java:25) 在com.example.simranjeet.myapplication43.MainActivity.onCreate(MainActivity.java:36) 在android.app.Activity.performCreate(Activity.java:6237) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 在android.app.ActivityThread.-wrap11(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5417) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 07-20 13:01:03.413 3353-12581 / com.google.android.gms.persistent E / NetworkScheduler:提供了无法识别的操作: android.intent.action.PACKAGE_REPLACED
答案 0 :(得分:1)
访问https://www.androidhive.info/2012/07/android-gps-location-manager-tutorial/,了解使用GPS和位置管理器的详细教程。
并且您的日志明确指定您需要在gps
方法中而不是在之前调用onCreate()
方法。
答案 1 :(得分:0)
尝试使用。
vfork
代替
locationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);