我在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();
}
}
}
}