许可不工作后的GPS位置(SDK 23)android studio

时间:2016-11-07 06:33:36

标签: android android-gps

我已经创建了这个课程,我需要在哪里放置我的最后一个位置?

public class DescriptionActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks {

    TextView t;
    Button up;
    GoogleApiClient mGoogleApiClient;
    LocationManager locationManager;
    LocationListener locationListener;
    LocationServices mLastLocation;
    private static final int PERMS_REQUEST_CODE=123;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hr__description);
        Toolbar toolbar = (Toolbar) findViewById(R.id.hr_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_clear_white_24dp);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        t = (TextView) findViewById(R.id.test_boaz);
        up=(Button)findViewById(R.id.upload);
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Drive.API)
                .addScope(Drive.SCOPE_FILE)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener((GoogleApiClient.OnConnectionFailedListener) this)
                .build();
        if(hasPermission())
        {
            getGPS();
        }
        else {
            requestPerms();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case PERMS_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // All good!
                } else {
                   Log.d("bobbbbbbbbbbbbbbbbbb","boazboabzobaob");
                }

                break;
        }
    }

    private void getGPS() {
        up.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


            }
        });
    }

    private boolean hasPermission(){
            int res=0;
            String[] permissions=new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
            for(String params:permissions)
            {
                res=checkCallingOrSelfPermission(params);
                if(!(res==PackageManager.PERMISSION_GRANTED)) {
                    return false;
                }
            }
            return true;
        }

        private void requestPerms(){
            String[] permissions=new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,Manifest.permission.ACCESS_FINE_LOCATION};
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
            {
             requestPermissions(permissions,PERMS_REQUEST_CODE);
                ActivityCompat.requestPermissions(this, permissions, PERMS_REQUEST_CODE);
            }

        }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onConnected(@Nullable Bundle bundle) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null) {

        }
    }

    @Override
    public void onConnectionSuspended(int i) {

    }
}

我试图激活GPS,它总是希望我检查权限 怎么解决? 在互联网上看了很多例子我已经成功获得激活GPS设备的对话但是当我试图获得位置时我有问题

2 个答案:

答案 0 :(得分:0)

  

如果要与提供的某个Google API建立连接   在Google Play服务库中(例如Google登录,游戏或   驱动器),您需要创建一个GoogleApiClient实例(“Google API   客户端“).Google API客户端为所有人提供了一个公共入口点   Google Play服务和管理之间的网络连接   用户的设备和每个Google服务。

  • 自动管理与Google Play服务的连接。
  • 对任何Google执行同步和异步API调用 播放服务。

  • 手动管理与Google Play服务的连接 极少数情况下这是必要的。

  

将此添加到清单文件

imgview = (ImageView) findViewById(R.id.imgtrans);
Drawable d = getResources().getDrawable(R.drawable.image1);
int h = d.getIntrinsicHeight();
int w = d.getIntrinsicWidth();



if(d.getIntrinsicHeight()==797 && d.getIntrinsicWidth()==1218)
{
    imgtrans.getLayoutParams().width = ViewGroup.LayoutParams.MATCH_PARENT;
    imgtrans.getLayoutParams().height = someValue;
    imgtrans.requestLayout();
}
  

在您的活动中添加此代码

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.google.android.gms.location.sample.basiclocationsample" >

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

答案 1 :(得分:0)

  

活动中的onCreate方法