嗨,我有从API 18到API 22的应用程序正常工作。 对于新的Android API 23,我正在尝试设置我在Manifest上设置的检查权限。我正在使用Dexter库。现在我想在启动屏幕之前显示启用权限的对话框设置,当我在对话框权限上单击“确定”时,应用程序保持打开并显示我的介绍视图和主视图之后。 我已设置此代码在启动屏幕运行期间工作,当我设置好启用权限应用程序关闭而不是保持打开状态。 这是我的启动画面和dexter库的代码:
public class SplashScreen extends Activity {
//Set waktu lama splashscreen
private static int splashInterval = 3000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Fabric.with(this, new Answers());
Dexter.initialize(getApplicationContext());
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash_fragment);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
View v = findViewById(android.R.id.content);
if (ActivityCompat.checkSelfPermission(getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager
.PERMISSION_GRANTED && ActivityCompat
.checkSelfPermission(getApplicationContext(),
android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager
.PERMISSION_GRANTED) {
MultiplePermissionsListener snackbarMultiplePermissionsListener =
SnackbarOnAnyDeniedMultiplePermissionsListener.Builder
.with((ViewGroup) v.getRootView(),"")
.withOpenSettingsButton("Settings")
.withCallback(new Snackbar.Callback() {
@Override
public void onShown(Snackbar snackbar) {
// Event handler for when the given Snackbar has been dismissed
}
@Override
public void onDismissed(Snackbar snackbar, int event) {
// Event handler for when the given Snackbar is visible
}
})
.build();
Dexter.checkPermissions(snackbarMultiplePermissionsListener, android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION, android.Manifest.permission.ACCESS_NETWORK_STATE, android.Manifest.permission.GET_ACCOUNTS);
return;
}
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
//jeda selesai Splashscreen
this.finish();
}
private void finish() {
// TODO Auto-generated method stub
}
}, splashInterval);
};
}
如何在启动画面之前显示此对话框设置,并在单击“确定”以启用权限后保持应用程序打开?
由于
答案 0 :(得分:0)
private void displaySplashScreen() {
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
@Override
public void run() {
Intent i = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
private void setPermissionForM() {
String[] PERMISSIONS = {
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.CAMERA,
Manifest.permission.READ_PHONE_STATE};
if (!utils.hasPermissions(this, PERMISSIONS)) {
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_ALL);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initial
perms.put(Manifest.permission.WRITE_EXTERNAL_STORAGE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.CAMERA, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_PHONE_STATE, PackageManager.PERMISSION_GRANTED);
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
if (perms.get(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED) {
// All Permissions Granted
displaySplashScreen();
} else {
// Permission Denied
Toast.makeText(SplashActivity.this, "Some Permission is Denied", Toast.LENGTH_SHORT)
.show();
}
}