2016-11-07 99 views
3

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

     } 
    } 
} 

回答

6

onpause功能删除此finish();调用,因为当对话框出现您的活动将在onpause状态去和finish通话将destroy您的活动

0

删除return语句,并把您在其他块中的启动代码,并从暂停中删除结束:

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(); 
    } 
相关问题