请求权限对话框中断活动并按任意键即可关闭活动

时间:2016-08-24 09:17:19

标签: android permissions runtime android-6.0-marshmallow

我不知道为什么这只发生在一个应用程序,因为我在其他应用程序上尝试它工作正常。但只有这个应用程序,每当我请求权限时活动消隐,只有权限对话框出现,然后按“允许”或“拒绝”将关闭活动,我需要重新打开它。 我怀疑这种行为是在主题或扩展类型的活动上发生的,如“AppCompactActivity”或“ActionBarActivity”。

这是我的代码

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
{
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
{
    switch (requestCode)
    {
        case 1:
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                Log.e("Permission","Granted");
            }
            else
            {
                Log.e("Permission","Not Granted");
            }

            break;
    }
}

这是主题,但它唯一怀疑主题是

的原因
<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowBackground">@color/primary_orange_lighter</item>
</style>

<style name="AppTheme.Splash_Activity" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:statusBarColor">@color/primary_orange_lighter</item>
</style>

<style name="AppTheme.Login_Activity" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:statusBarColor">@color/primary_orange_lighter</item>
</style>

<style name="AppTheme.Main_Activity" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:statusBarColor">@color/primary_orange_lighter</item>
</style>


<style name="StarRatingBar" parent="@android:style/Widget.RatingBar">
    <item name="android:progressDrawable">@drawable/star_rating_bar_full</item>
    <item name="android:minHeight">48dip</item>
    <item name="android:maxHeight">48dip</item>
</style>

<style name="Dialog" parent="@android:style/Theme.Holo.Dialog.NoActionBar">
    <item name="android:color">#FFFFFFFF</item>
    <item name="android:windowBackground">@color/trans_white</item>
    <item name="android:windowMinWidthMajor">90%</item>
    <item name="android:windowMinWidthMinor">90%</item>

</style>

<style name="ToolBarStyle" parent="AppTheme.Main_Activity">
    <item name="android:statusBarColor">@color/primary_orange_lighter</item>
    <item name="android:background">@color/primary_orange_lighter</item>
    <item name="android:textColorPrimary">@android:color/white</item>
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>

<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">false</item>
    <item name="color">@android:color/white</item>
</style>

<style name="CustomLoadingDialog" parent="SpotsDialogDefault">
    <item name="DialogTitleAppearance">@android:style/TextAppearance.Medium</item>
    <item name="DialogTitleText">Loading</item>
    <item name="android:textColor">@color/white</item>
    <item name="android:background">@color/black</item>
    <item name="DialogSpotColor">@color/white</item>
    <item name="DialogSpotCount">5</item>
</style>


<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="SwitchButtonStyle">
    <item name="android:paddingLeft">10dp</item>
    <item name="android:paddingRight">10dp</item>
    <item name="android:paddingTop">4dp</item>
    <item name="android:paddingBottom">4dp</item>
</style>



<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

这是发生请求的类

import android.Manifest;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import syncworx.com.kunhadi_driver.R;
import syncworx.com.kunhadi_driver.classes.Constants;

public class Test_Activity extends AppCompatActivity {

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

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED&& ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
    {
        ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
{
    switch (requestCode)
    {
        case 1:
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                Log.e("Permission","Granted");
            }
            else
            {
                Log.e("Permission","Not Granted");
            }

            break;
    }
}
}

1 个答案:

答案 0 :(得分:8)

最后经过两天努力解决这个问题后,我发现了什么问题 好吧,让我先发布问题来源

Intent intent = new Intent(Splash_Activity.this, Test_Activity.class);
intent .addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

正如你在这里看到的,当我启动Test_Activity时,我正在请求其中的Activity,我正在为启动它的intent添加一些标志,这样当这个活动不再可见时,它将被自动销毁,似乎请求权限会阻止活动,就好像它不再可见,因此它被破坏,然后在权限对话框后面有停电,因为没有活动了,然后按deny或allow将简单地解除对话框并向onRequestPermissionsResult方法发送回调但该活动未处于活动状态以接收回调