2016-11-07 64 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(); 
    } 
相關問題