简体   繁体   English

为什么Android Studio 构建找不到符号变量MockPackageManager?

[英]Why can't Android Studio building find symbol variable MockPackageManager?

Here I want to find longitude and latitude and show it into the xml.在这里我想找到经度和纬度并将其显示到xml中。

package com.example.adeeb.gamecom;
import android.Manifest;
import android.content.pm.PackageManager;
 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.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity  {

Button btnShowLocation;
private static final int REQUEST_CODE_PERMISSION = 2;
String mPermission  = Manifest.permission.ACCESS_FINE_LOCATION;
GPSTracker gps;
TextView location;

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

        if(ActivityCompat.checkSelfPermission(this,mPermission)!= 
         MockPackageManager.PERMISSION_GRANTED ){

            ActivityCompat.requestPermissions(this,new String[] 
              {mPermission},REQUEST_CODE_PERMISSION);
        }


    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    btnShowLocation = (Button) findViewById(R.id.button);
    btnShowLocation.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View V)
        {
            gps = new GPSTracker(MainActivity.this);
            location = (TextView) findViewById(R.id.locationtext);
            if(gps.canGetLocation())
            {
                double latitude = gps.getLatitude();
                double longitude = gps.getLongitude();
                location.setText(latitude+" "+longitude);

            }

            else
            {
                gps.showSettingsAlert();
            }
        }
    });
}

} Why the compiler can is giving error cannot find symbol variable MockPackageManager.为什么编译器可以给出错误找不到符号变量 MockPackageManager。 if i use packagemanager the app is not showing longitude and latitude.如果我使用 packagemanager,该应用程序不会显示经度和纬度。 how can i resolve this?我该如何解决这个问题?

The reason is that MockManager is a (deprecated) test class for mocking the package manager behaviors.原因是 MockManager 是一个(不推荐使用的)测试类,用于模拟包管理器行为。

I had a similar issue when I changed my Target API to 28. And the below-given code worked well for me.当我将 Target API 更改为 28 时,我遇到了类似的问题。下面给出的代码对我来说效果很好。

 try {
        if (ActivityCompat.checkSelfPermission(this, mPermission)
                != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this, new String[]{mPermission},
                    REQUEST_CODE_PERMISSION);


        }
    } catch (Exception e) {
        e.printStackTrace();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM