应用程序没有找到GPS信号

时间:2018-02-08 19:57:39

标签: java android android-studio gps android-gps

我为我的Android设备制作了速度计。我使用三星Galaxy平板电脑测试我的应用程序,一切都很完美。我完成后,我想在我的手机上尝试它,但无法让它工作。它只显示"搜索gps" ..我问我的朋友他是否可以在他的手机上试用但仍然是同样的问题。有没有人有类似的问题?我改变了API级别,但没有区别。这两款手机都运行最新的Android版本,但我的平板电脑正在运行5.1.1。

MainActivity

public class MainActivity extends AppCompatActivity {

LocationService myService;
static boolean status;
LocationManager locationManager;
static TextView dist, time, speed, timetext, disttext;
static long startTime, endTime;
ImageView image;
static ProgressDialog locate;
static int p = 0;
public SharedPreferences preferences;
public SharedPreferences preferences2;

private ServiceConnection sc = new ServiceConnection() {
    public void onServiceConnected(ComponentName name, IBinder service) {
        LocationService.LocalBinder binder = (LocationService.LocalBinder) service;
        myService = binder.getService();
        status = true;
    }

    public void onServiceDisconnected(ComponentName name) {
        status = false;
    }
};

public ImageButton androidImmageButton;
public void init() {
    androidImmageButton = findViewById(R.id.button);
    androidImmageButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent toy = new Intent(MainActivity.this, Einstellungen.class);
            startActivity(toy);
        }
    });
}

void bindService() {
    if (status == true)
        return;
    Intent i = new Intent(getApplicationContext(), LocationService.class);
    bindService(i, sc, BIND_AUTO_CREATE);
    status = true;
    startTime = System.currentTimeMillis();
}

void unbindService() {
    if (status == false)
        return;
    Intent i = new Intent(getApplicationContext(), LocationService.class);
    unbindService(sc);
    status = false;
}

protected void onResume() {
    super.onResume();

}

protected void onStart() {
    super.onStart();

}

protected void onDestroy() {
    super.onDestroy();
    if (status == true)
        unbindService();
}

public void onBackPressed() {
    if (status == false)
        super.onBackPressed();
    else
        moveTaskToBack(true);
}

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

    dist = findViewById(R.id.distancetext);
    time = findViewById(R.id.timetext);
    speed = findViewById(R.id.speedtext);
    disttext = findViewById(R.id.textView3);
    timetext = findViewById(R.id.textView4);

    image = findViewById(R.id.image);

    preferences2 = getSharedPreferences(Einstellungen.SHARED_PREFERENCES2, MODE_PRIVATE);
    int size = preferences2.getInt(Einstellungen.SELECTED_COLOR2, 0);
    speed.setTextSize(size);

    preferences = getSharedPreferences(Einstellungen.SHARED_PREFERENCES, MODE_PRIVATE);
    int color = preferences.getInt(Einstellungen.SELECTED_COLOR, Color.WHITE);
    speed.setTextColor(color);
    time.setTextColor(color);
    dist.setTextColor(color);
    timetext.setTextColor(color);
    disttext.setTextColor(color);
    start();
    init();
}

        public void start() {
            checkGps();
            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {

                return;
            }


            if (status == false)
                bindService();
            locate = new ProgressDialog(MainActivity.this);
            locate.setIndeterminate(true);
            locate.setCancelable(false);
            locate.setMessage("searching gps");
            locate.show();
        }

void checkGps() {
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {


        showGPSDisabledAlertToUser();
    }
}

private void showGPSDisabledAlertToUser() {
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("please activate your gps")
            .setCancelable(false)
            .setPositiveButton("activate gps",
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Intent callGPSSettingIntent = new Intent(
                                    android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                            startActivity(callGPSSettingIntent);
                        }
                    });
    alertDialogBuilder.setNegativeButton("cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });
    AlertDialog alert = alertDialogBuilder.create();
    alert.show();
}

}

LocationServices

public class LocationService extends Service implements
    LocationListener,
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {

private static final long INTERVAL = 1000 * 2;
private static final long FASTEST_INTERVAL = 1000 * 1;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mCurrentLocation, lStart, lEnd;
static double distance = 0;
double speed;


private final IBinder mBinder = new LocalBinder();

public IBinder onBind(Intent intent) {
    createLocationRequest();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .build();
    mGoogleApiClient.connect();
    return mBinder;
}

protected void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}

public int onStartCommand(Intent intent, int flags, int startId) {

    return super.onStartCommand(intent, flags, startId);
}

public void onConnected(Bundle bundle) {
    try {
        LocationServices.FusedLocationApi.requestLocationUpdates(
                mGoogleApiClient, mLocationRequest, this);
    } catch (SecurityException e) {
    }
}


protected void stopLocationUpdates() {
    LocationServices.FusedLocationApi.removeLocationUpdates(
            mGoogleApiClient, this);
    distance = 0;
}

public void onConnectionSuspended(int i) {

}

public void onLocationChanged(Location location) {
    MainActivity.locate.dismiss();
    mCurrentLocation = location;
    if (lStart == null) {
        lStart = mCurrentLocation;
        lEnd = mCurrentLocation;
    } else
        lEnd = mCurrentLocation;
    updateUI();
    speed = location.getSpeed() * 18 / 5;

}

public void onConnectionFailed(ConnectionResult connectionResult) {

}

public class LocalBinder extends Binder {

    public LocationService getService() {
        return LocationService.this;
    }


}

private void updateUI() {
    if (MainActivity.p == 0) {
        distance = distance + (lStart.distanceTo(lEnd) / 1000.00);
        MainActivity.endTime = System.currentTimeMillis();
        long diff = MainActivity.endTime - MainActivity.startTime;
        diff = TimeUnit.MILLISECONDS.toMinutes(diff);
        MainActivity.time.setText(" " + diff + " min");
        if (speed > 0.0)
            MainActivity.speed.setText(" " + new DecimalFormat("#").format(speed) + " ");
        else
            MainActivity.speed.setText("0");

        MainActivity.dist.setText(" " + new DecimalFormat("#.##").format(distance) + " km");

        lStart = lEnd;

    }

}

public boolean onUnbind(Intent intent) {
    stopLocationUpdates();
    if (mGoogleApiClient.isConnected())
        mGoogleApiClient.disconnect();
    lStart = null;
    lEnd = null;
    distance = 0;
    return super.onUnbind(intent);
}

}

设置

public class Einstellungen extends AppCompatActivity {

String[] names2 = {"Small", "Normal", "Big"};
String[] des2 = {"Small", "Normal", "Big"};
ArrayAdapter<String> adapter2;
Spinner spinner2;
TextView description2;
public static final String SHARED_PREFERENCES2 = "SHARED_PREDS2";
public static final String SELECTED_COLOR2 = "SELECTED_COLOR2";
public SharedPreferences preferences2;
public static final String SELECTED_COLOR_POSITION2 = "SELECTED_COLOR_POSITION2";

String[] names = {"White", "Blue", "Red", "Green", "Yellow", "Gray", "Cyan", "Magenta"};
String[] des = {"White", "Blue", "Red", "Green", "Yellow", "Gray", "Cyan", "Magenta"};
ArrayAdapter<String> adapter;
Spinner spinner;
TextView description;
public static final String SHARED_PREFERENCES = "SHARED_PREFS";
public static final String SELECTED_COLOR = "SELECTED_COLOR";
public SharedPreferences preferences;
public static final String SELECTED_COLOR_POSITION = "SELECTED_COLOR_POSITION";
public ImageButton androidImmageButton;
public ImageButton androidImmageButton2;
public ImageButton androidImmageButton3;

public Button button;

public void init() {
    androidImmageButton = findViewById(R.id.button);
    androidImmageButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent toy = new Intent(Einstellungen.this, MainActivity.class);
            startActivity(toy);
        }
    });
}

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_einstellungen);

    preferences2 = getSharedPreferences(SHARED_PREFERENCES2, MODE_PRIVATE);
    spinner2 = findViewById(R.id.spinner2);
    description2 = findViewById(R.id.text);
    adapter2 = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, names2);
    spinner2.setAdapter(adapter2);
    int position2 = preferences2.getInt(SELECTED_COLOR_POSITION2, 0);
    spinner2.setSelection(position2);
    spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapterView, View view, int d, long l) {
            preferences2.edit().putInt(SELECTED_COLOR_POSITION2, d).apply();
            switch (d) {
                case 0:
                    description2.setText(des2[d]);
                    preferences2.edit().putInt(SELECTED_COLOR2, 140).apply();
                    break;
                case 1:
                    description2.setText(des2[d]);
                    preferences2.edit().putInt(SELECTED_COLOR2, 190).apply();
                    break;
                case 2:
                    description2.setText(des[d]);
                    preferences2.edit().putInt(SELECTED_COLOR2, 240).apply();
                    break;
            }
        }

        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

    preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
    spinner = findViewById(R.id.spinner);
    description = findViewById(R.id.text);
    adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, names);
    spinner.setAdapter(adapter);
    int position = preferences.getInt(SELECTED_COLOR_POSITION, 0);
    spinner.setSelection(position);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            preferences.edit().putInt(SELECTED_COLOR_POSITION, i).apply();
            switch (i) {
                case 0:
                    description.setText(des[i]);
                    preferences.edit().putInt(SELECTED_COLOR, Color.WHITE).apply();
                    break;
                case 1:
                    description.setText(des[i]);
                    preferences.edit().putInt(SELECTED_COLOR, Color.BLUE).apply();
                    break;
                case 2:
                    description.setText(des[i]);
                    preferences.edit().putInt(SELECTED_COLOR, Color.RED).apply();
                    break;
                case 3:
                    description.setText(des[i]);
                    preferences.edit().putInt(SELECTED_COLOR, Color.GREEN).apply();
                    break;
                case 4:
                    description.setText(des[i]);
                    preferences.edit().putInt(SELECTED_COLOR, Color.YELLOW).apply();
                    break;
                case 5:
                    description.setText(des[i]);
                    preferences.edit().putInt(SELECTED_COLOR, Color.GRAY).apply();
                    break;
                case 6:
                    description.setText(des[i]);
                    preferences.edit().putInt(SELECTED_COLOR, Color.CYAN).apply();
                    break;
                case 7:
                    description.setText(des[i]);
                    preferences.edit().putInt(SELECTED_COLOR, Color.MAGENTA).apply();
                    break;
            }
        }

        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
    init();
}

}

0 个答案:

没有答案