我在SplashActivity中有这段代码,它请求ReadPhonestate权限来调用ASyncTask.在第一次运行时,活动结束(而不是崩溃),然后出现权限对话框.我授予权限并重新进入应用程序并正常启动.那么为什么首先在第一轮完成飞溅?
这是我的代码:
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(); } } }}
解决方法:
删除此完成();从暂停功能调用,因为当对话框出现时,您的活动将进入暂停状态,完成调用将破坏您的活动
总结以上是内存溢出为你收集整理的android – 应用程序在请求权限时退出全部内容,希望文章能够帮你解决android – 应用程序在请求权限时退出所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)