簡體   English   中英

Android Studio:無法解析符號“ requestLocationUpdates”

[英]Android Studio: Unable to resolve Symbol 'requestLocationUpdates'

使用基本的GPS代碼,新的Android Studio遇到了問題-我的意思是,到目前為止,其預覽軟件已經足夠可靠,可以長時間使用。 所以這是給我問題的代碼:

import android.app.Activity;
import android.app.AlertDialog; 
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;

public class WhereAmI extends Activity implements LocationListener{

TextView lat, lon, alt, status;
Context myContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.whereami);
    lat = (TextView)findViewById(R.id.latTxt);
    lon = (TextView)findViewById(R.id.lonTxt);
    alt = (TextView)findViewById(R.id.altTxt);
    status = (TextView)findViewById(R.id.gpsStatus);
}

/* Use the LocationManager class to obtain GPS locations */
LocationManager LM = (LocationManager) getSystemService(LOCATION_SERVICE);
//Gather GPS data at a certain time interval.
LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1f, myContext);
//Check to see if GPS is on
boolean isGPS = LM
        .isProviderEnabled(LocationManager.GPS_PROVIDER);

//If its off, request to turn it on.
if (isGPS == false)
{
    //Enable GPS pop-up notification.
    AlertDialog.Builder adb = new AlertDialog.Builder(myContext);

    // set title
    adb.setTitle("Enable GPS?");

    // set dialog message
    adb.setMessage("Enable GPS to get full function from the app.");
    adb.setCancelable(false);

    //Yes Button
    adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            startActivityForResult(
                    new Intent(
                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS),
                    0);
        }
    });

    //No Button
    adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    // create GPS Alert [Pop-up]
    AlertDialog alertDialog = adb.create();

    // show it
    alertDialog.show();
}
else
{
    //Added this so when the app gets refreshed it will show the true GPS info status.
    status.setText("GPS Status: Enabled");
}

@Override
public void onLocationChanged(Location location) {
    lat.setText("Latitude: " + location.getLatitude());
    lon.setText("Longitude: " + location.getLongitude());
    alt.setText("Altitude: " + location.getAltitude());
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {
    status.setText("GPS Status: Enabled");
}

@Override
public void onProviderDisabled(String s) {

}
}

由於某種原因,它不斷出現,無法解析requestLocationUpdates的符號。

還有其他人遇到這個問題或知道解決方案嗎?

沒有requestLocationUpdates()函數將Context作為輸入。 以下是requestLocationUpdates()函數的允許參數:

requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent)
requestLocationUpdates(long minTime, float minDistance, Criteria criteria, LocationListener listener, Looper looper)
requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)
requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener, Looper looper)
requestLocationUpdates(String provider, long minTime, float minDistance, PendingIntent intent)

也請參見LocationManager頁面。 希望這可以幫助。

更新

oncreate()移動以下代碼:

/* Use the LocationManager class to obtain GPS locations */
LocationManager LM = (LocationManager) getSystemService(LOCATION_SERVICE);
//Gather GPS data at a certain time interval.
LM.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1, 1f, myContext);
//Check to see if GPS is on
boolean isGPS = LM
        .isProviderEnabled(LocationManager.GPS_PROVIDER);

//If its off, request to turn it on.
if (isGPS == false)
{
    //Enable GPS pop-up notification.
    AlertDialog.Builder adb = new AlertDialog.Builder(myContext);

    // set title
    adb.setTitle("Enable GPS?");

    // set dialog message
    adb.setMessage("Enable GPS to get full function from the app.");
    adb.setCancelable(false);

    //Yes Button
    adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            startActivityForResult(
                    new Intent(
                            android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS),
                    0);
        }
    });

    //No Button
    adb.setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });

    // create GPS Alert [Pop-up]
    AlertDialog alertDialog = adb.create();

    // show it
    alertDialog.show();
}
else
{
    //Added this so when the app gets refreshed it will show the true GPS info status.
    status.setText("GPS Status: Enabled");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM