gpt4 book ai didi

android - 请求权限对话框关闭 Activity 并按任意键关闭 Activity

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:19:22 25 4
gpt4 key购买 nike

我不知道为什么这只发生在一个应用程序上,因为我在其他应用程序上试过它工作正常。但是,每当我请求权限时,只有这个应用程序会关闭该 Activity ,并且只会出现权限对话框,然后按“允许”或“拒绝”将关闭该 Activity ,我需要重新打开它。我怀疑这种行为发生在主题的某些变体或 Activity 的扩展类型上,例如“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;
}
}
}

最佳答案

在为这个问题苦苦挣扎了两天之后,我终于找到了问题所在好吧,让我先发布问题的根源

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 添加了一些标志,以便当该 Activity 不再可见时,它将自动销毁,似乎请求权限确实阻止了 Activity ,就好像它不再可见一样,所以它被破坏了,然后权限对话框后面停电了,因为没有 Activity 了,然后按拒绝或允许只会关闭对话框并向 onRequestPermissionsResult 方法发送回调,但 Activity 未激活以接收回调

关于android - 请求权限对话框关闭 Activity 并按任意键关闭 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39119193/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com