Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使线程在启动屏幕中等待权限检查?_Android - Fatal编程技术网

Android 如何使线程在启动屏幕中等待权限检查?

Android 如何使线程在启动屏幕中等待权限检查?,android,Android,我有一个启动屏幕活动,在这个活动中,我想从用户那里获得位置权限。但是,我不能让线程在启动活动之前等待权限检查。 以下是我所拥有的: public class SplashScreen extends AppCompatActivity { private boolean permissionGranted; private static final int PERMISSION_COARSE_LOCATION = 99; private FusedLocationPro

我有一个启动屏幕活动,在这个活动中,我想从用户那里获得位置权限。但是,我不能让线程在启动活动之前等待权限检查。 以下是我所拥有的:

public class SplashScreen extends AppCompatActivity {

    private boolean permissionGranted;
    private static final int PERMISSION_COARSE_LOCATION = 99;
    private FusedLocationProviderClient fusedLocationProviderClient;
    public static final int REQUEST_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_splash_screen);
        permissionGranted = false;
        fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
        final Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                checkPermission();
            }
        },3000);

        Thread background = new Thread(){
            public void run(){
                try{
                    sleep(4000);
                    startActivity(new Intent(SplashScreen.this,TutorialPages.class));
                    finish();
                }catch (InterruptedException e){
                    Toast.makeText(getApplicationContext(),"Try again, location permission denied",
                            Toast.LENGTH_SHORT).show();
                }

            }
        };

        background.start();


    }
和检查权限:

public void checkPermission(){
        requestPermission();
        if (ContextCompat.checkSelfPermission(SplashScreen.this,
                Manifest.permission.ACCESS_COARSE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {

           // Toast.makeText(SplashScreen.this,"Permission Denied!", Toast.LENGTH_SHORT).show();
            if (ActivityCompat.shouldShowRequestPermissionRationale(SplashScreen.this,
                    Manifest.permission.ACCESS_COARSE_LOCATION)) {

                new AlertDialog.Builder(this)
                        .setTitle("Required Location Permission")
                        .setMessage("Location permission is needed to run PassingBuy.")
                        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                ActivityCompat.requestPermissions(SplashScreen.this,
                                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                                        PERMISSION_COARSE_LOCATION);
                            }
                        })
                        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .create()
                        .show();
            } else {
                // No explanation needed; request the permission
                ActivityCompat.requestPermissions(SplashScreen.this,
                        new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                        PERMISSION_COARSE_LOCATION);

            }
        } else {
            // Permission has already been granted
            fusedLocationProviderClient.getLastLocation()
                    .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                        @Override
                        public void onSuccess(Location location) {
                            if(location != null){
                                Double lattitude = location.getLatitude();
                                Double altitude = location.getAltitude();


                            }
                        }
                    });
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if(requestCode == PERMISSION_COARSE_LOCATION){
            if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
                Toast.makeText(this,"GREAT",Toast.LENGTH_SHORT).show();
                permissionGranted = true;
            }else{
                Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();
                permissionGranted = false;
            }
        }
    }

    public void requestPermission(){
        ActivityCompat.requestPermissions(SplashScreen.this,
                new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
                PERMISSION_COARSE_LOCATION);
    }
public void checkPermission(){
requestPermission();
如果(ContextCompat.checkSelfPermission)(SplashScreen.this,
清单.权限.访问(位置)
!=PackageManager.权限(已授予){
//Toast.makeText(SplashScreen.this,“权限被拒绝!”,Toast.LENGTH_SHORT.show();
如果(活动公司)应显示请求许可理由(SplashScreen.this,
清单.权限.访问(位置){
新建AlertDialog.Builder(此)
.setTitle(“所需的位置许可”)
.setMessage(“运行PassingBuy需要位置权限”)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface dialog,int which){
ActivityCompat.requestPermissions(SplashScreen.this、,
新字符串[]{Manifest.permission.ACCESS\u\u LOCATION},
许可(地点);;
}
})
.setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface dialog,int which){
dialog.dismise();
}
})
.create()
.show();
}否则{
//无需解释;请求许可
ActivityCompat.requestPermissions(SplashScreen.this、,
新字符串[]{Manifest.permission.ACCESS\u\u LOCATION},
许可(地点);;
}
}否则{
//已授予许可
fusedLocationProviderClient.getLastLocation()文件
.addOnSuccessListener(此,新的OnSuccessListener(){
@凌驾
成功时的公共无效(位置){
如果(位置!=null){
Double latitude=location.getLatitude();
双高度=位置。getAltitude();
}
}
});
}
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
if(requestCode==权限\u位置){
if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION\u已授予){
Toast.makeText(这个“很棒”,Toast.LENGTH_SHORT).show();
permissiongrated=true;
}否则{
Toast.makeText(这是“运行应用程序所需的权限”,Toast.LENGTH_LONG.show();
许可证授予=假;
}
}
}
公共void requestPermission(){
ActivityCompat.requestPermissions(SplashScreen.this、,
新字符串[]{Manifest.permission.ACCESS\u\u LOCATION},
许可(地点);;
}
我尝试更改布尔变量permissionGranted,并在while(permissionGranted)中放置一个
background.start()之前的code>但它不起作用。有什么想法吗

将代码(用于启动活动)从单独的线程移动到onRequestPermissionsResult方法(授予权限的地方),如下所示

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if(requestCode == PERMISSION_COARSE_LOCATION){
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            Toast.makeText(this,"GREAT",Toast.LENGTH_SHORT).show();
            permissionGranted = true;

            // Code to start activity
            startActivity(new Intent(SplashScreen.this,TutorialPages.class));
            finish();

        }else{
            Toast.makeText(this, "Permission Needed To Run The App", Toast.LENGTH_LONG).show();
            permissionGranted = false;
        }
    }
}

干杯帮了很多忙,用了你推荐的方法。你教了我关键字volatile作为奖励:)