应用程序在请求权限时退出

Application exits while requesting permission

我在 SplashActivity 中有这段代码请求 ReadPhoneState 权限以调用 ASyncTask。在第一个 运行 上,activity 完成(不是崩溃),然后出现权限对话框。我授予权限并重新进入该应用程序,它正常启动。那么为什么splash排在第一个运行呢?

这是我的代码:

public class SplashActivity extends Activity {

    boolean noConMessage = false, granted = false;
    boolean firstRun;
    int caller = 0;
    int channelId = 0;
    Bundle bundle;
    String deviceId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        MyApplication.crashBundle = this.getIntent().getExtras();
        final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
        MyApplication.fontSize = Integer.parseInt(settings.getString(getResources().getString(R.string.textsize_key), "15").toString());
        firstRun = settings.getBoolean(getResources().getString(R.string.firstRun_key), true);
        deviceId = settings.getString(getResources().getString(R.string.deviceId_key), "-1");


        /*if (ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED
                ) {
            ActivityCompat.requestPermissions(SplashActivity.this, new String[]{
                    Manifest.permission.READ_PHONE_STATE}, 1);
                    Launching mLaunching = new Launching();
                    mLaunching.execute();
        }else{
            Launching mLaunching = new Launching();
            mLaunching.execute();
        }*/

        int hasReadPermission = ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_PHONE_STATE);
        if (hasReadPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(SplashActivity.this, new String[] {Manifest.permission.READ_PHONE_STATE},
                    123);
            return;
        }

        //  CheckNewVersionAsyncTask mCheckNewVersionAsyncTask=new CheckNewVersionAsyncTask(this);
        //  mCheckNewVersionAsyncTask.execute();

        Launching mLaunching = new Launching();
        mLaunching.execute();

    }



    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 123:
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    Launching mLaunching = new Launching();
                    mLaunching.execute();
                } else {
                    // Permission Denied

                }
                break;
            default:
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    public void loadPage() {
        Intent intent;

        intent = new Intent(SplashActivity.this,
                ChannelListActivity.class);

        intent.putExtra(Extra.IMAGES, Constants.IMAGES);
        startActivity(intent);


    }

    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        if (noConMessage) {
            Toast.makeText(SplashActivity.this, "No Internet Connection", Toast.LENGTH_LONG).show();
        }
        finish();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();


    }

    protected class Launching extends AsyncTask<Void, Void, Integer> {

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected Integer doInBackground(Void... a) {
            try {
                if (deviceId.equals("-1")) {

                    ServerUtilities.addDevice(SplashActivity.this);
                    GCMRegistrar.unregister(SplashActivity.this);
                } else {
                    TimeUnit.SECONDS.sleep((long) 0.25);
                }
                if (true) {
                    Actions.copyFile(SplashActivity.this, "tahoma.ttf");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 0;

        }

        @Override
        protected void onPostExecute(Integer result) {
            if (firstRun) {
                PushNotificationActions.registerNotification(SplashActivity.this);
            } else {
                loadPage();
            }

        }
    }
}

onpause 函数中删除此 finish(); 调用,因为当对话框出现时,您的 activity 将进入 onpause 状态,而 finish 调用将 destroy 你的 activity

删除 return 语句并将您的启动代码放在 else 块中,同时从暂停中删除完成:

int hasReadPermission = ContextCompat.checkSelfPermission(SplashActivity.this, Manifest.permission.READ_PHONE_STATE);
        if (hasReadPermission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(SplashActivity.this, new String[] {Manifest.permission.READ_PHONE_STATE},
                    123);

        }else {
        Launching mLaunching = new Launching();
        mLaunching.execute();
    }