我在制作可查找用户 lat/lng 和设备 IMEI 的应用程序时遇到问题

I'm having problems making an app that finds user lat/lng and device IMEI

我想制作一个简单的 android 应用程序,向我显示用户位置和设备 IMEI。我不知道是什么问题,因为我是 android-studio 的新手,构建输出没有向我显示错误行。

package com.example.mainactivity;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnSuccessListener;

import static android.Manifest.permission.ACCESS_FINE_LOCATION;

public class MainActivity extends AppCompatActivity {

private FusedLocationProviderClient client;


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

    requestPermission();

    client = LocationServices.getFusedLocationProviderClient(this);

    Button button = findViewById(R.id.Locatie);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (ActivityCompat.checkSelfPermission(MainActivity.this, 
  ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                return;
            }

  client.getLastLocation().addOnSuccessListener(MainActivity.this, new 
 OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {

                    if(location!= null) {

                    }


                }
            })
        }
    });

}
private void requestPermission() {
    ActivityCompat.requestPermissions(this, new String[] 
{ACCESS_FINE_LOCATION},1);
}

public String getDeviceIMEI() {
    String deviceUniqueIdentifier = null;
    TelephonyManager tm = (TelephonyManager) 
this.getSystemService(Context.TELEPHONY_SERVICE);
    if(null != tm) {
        deviceUniqueIdentifier = tm.getDeviceId();

    }
    if(null == deviceUniqueIdentifier || 0 == 
deviceUniqueIdentifier.length()) {
        deviceUniqueIdentifier = 
Settings.Secure.getString(this.getContentResolver(), 
Settings.Secure.ANDROID_ID);
    }
    Toast toast = Toast.makeText(this, deviceUniqueIdentifier, 300);
    toast.show();
    return deviceUniqueIdentifier;

}

}

我希望在 toast 上看到设备的纬度和经纬度以及 imei windows。我认为这不是很有用,但我是 android 的新手。

添加这 3 permission :

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET"/>

根据您的Activity更改详细信息:

public class MainActivity extends AppCompatActivity {

    private FusedLocationProviderClient client;


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

        client = LocationServices.getFusedLocationProviderClient(this);
        Button button = findViewById(R.id.Chk);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                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) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                client.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
                    @Override
                    public void onSuccess(Location location) {
                        if (location!=null){
                            TextView textView = findViewById(R.id.textView);

                            Double longitude = location.getLongitude();
                            Double latitude = location.getLatitude();

                            textView.setText(String.valueOf(longitude+ "  "+latitude));

                        }
                    }
                });
            }
        });

    }
} 

这里是 xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

        <LinearLayout
            android:layout_centerVertical="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="Text"
                android:id="@+id/textView"
                android:layout_gravity="center_horizontal" />



            <Button
                android:id="@+id/Chk"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dip"
                android:text="Check Location"
                android:textAllCaps="false"
                android:textColor="@color/colorAccent" />

        </LinearLayout>

</RelativeLayout>

在 build.gradle(应用程序)中添加:

allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url "https://maven.google.com"
        }

    }
}

并在 build.gradle(Module: app) 中添加此依赖项:

implementation 'com.google.android.gms:play-services:11.0.0'

这是我的截图:

i used locationManager but in the textView i set to show me the lat and long, it doesn't show me anything
package com.example.locatieimei;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements LocationListener {

private TextView textView;
private LocationManager locationManager;

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

    textView = (TextView) findViewById(R.id.id_textview);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, 
 Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
 ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) 
 != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] 
 permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the 
 documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    Location location = 
 locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);

    onLocationChanged(location);




}

@Override
public void onLocationChanged(Location location) {

    double longitude = location.getLongitude();
    double latitute  = location.getLatitude();
    textView.setText("Longitudine"+longitude+ "\n"+"Latitudine"+latitute);

}

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

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

 }
}
i use the locationManager but on the textView i set to show me the lat and long, 
 still show me the Hello World. 
this is the xml
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/id_textview"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

是的,我使用了清单中的权限