2015-06-27 157 views
3

我在nexus 7 2013上運行我的應用程序,在logcat中,我注意到一些日誌運行了兩次。在寫了更多的日誌之後,我斷定每個活動都在平板電腦上運行兩次。奇怪的想法是,在手機上(LG G3或三星Galaxy S4 mini)日誌只能打印一次。Android活動正在運行兩次

在做了一些研究之後,我嘗試添加android:launchMode="singleTop"singleTasksingleInstance但沒有任何工作。此外,我還有一些意圖與國旗活動:intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);和2 AsyncTasks

有沒有一種方式,意圖或asynctask導致問題?

清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.marian.digimusicstream" > 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <!-- 
    To retrieve OAuth 2.0 tokens or invalidate tokens to disconnect a user. This disconnect 
    option is required to comply with the Google+ Sign-In developer policies 
    --> 
    <uses-permission android:name="android.permission.USE_CREDENTIALS" /> <!-- To retrieve the account name (email) as part of sign-in: --> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <!-- To auto-complete the email text field in the login form with the user's emails --> 
    <uses-permission android:name="android.permission.READ_PROFILE" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS" /> 

    <permission android:name="android.permission.MEDIA_CONTENT_CONTROL" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppBaseTheme"> 
     <activity 
      android:name=".LoginActivity" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.Light.NoTitleBar" 
      android:windowSoftInputMode="stateHidden" 
      android:launchMode="singleTop"> 

      <!-- <intent-filter> --> 
      <!-- <action android:name="android.intent.action.MAIN" /> --> 
      <!-- <category android:name="android.intent.category.LAUNCHER" /> --> 
      <!-- </intent-filter> --> 
     </activity> 

     <meta-data 
      android:name="com.google.android.gms.version" 
      android:value="@integer/google_play_services_version" /> 

     <activity 
      android:name=".musicPlayer" 
      android:label="@string/title_activity_music_player"> 

      <!-- android:theme="@android:style/Theme.DeviceDefault.Light.DarkActionBar" --> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <service android:name=".PlayerService" /> 
     <service android:name=".audioService" /> 

     <receiver android:name=".PlayPauseReceiver" /> 
     <receiver android:name=".NetworkChangeReceiver" > 
      <intent-filter> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
      </intent-filter> 
     </receiver> 
     <receiver android:name=".musicPlayer$MusicNoisyReceiver" > 
      <intent-filter> 
       <action android:name="android.media.AUDIO_BECOMING_NOISY" /> 
      </intent-filter> 
     </receiver> 

     <activity 
      android:name=".singleMusicScreen" 
      android:label="@string/title_activity_single_music_screen" > 
     </activity> 
    </application> 

</manifest> 

活動:

public class musicPlayer extends DrawerActivity { 

    @Override 
    protected void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_music_player); 

     setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); 
     mainLayout = (RelativeLayout) findViewById(R.id.mainLayout); 
     pullToRefresh = (PullRefreshLayout) findViewById(R.id.pullToReresh); 
     musicListLayout = new RelativeLayout(getApplicationContext()); 

     progressBar = new ProgressBar(getApplicationContext()); 

     pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
     editor = pref.edit(); 

     getUser = pref.getString("sharedUser", ""); 
     getPassword = pref.getString("sharedPass", ""); 

     new doLogin().execute(); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_music_player, menu); 
     return true; 
    } 

    @Override 
    public void onBackPressed() { 
     // Do Here what ever you want do on back press; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    public boolean isFirstItemCompletelyVisible() { 
     return mLayoutManager.findFirstCompletelyVisibleItemPosition() == 0; 
    } 

    @Override 
    public void onResume() { 
     super.onResume(); 
     musicList.post(new Runnable() { 
      @Override 
      public void run() { 
       pullToRefresh.setEnabled(isFirstItemCompletelyVisible()); 
      } 
     }); 
    } 

    private class doLogin extends AsyncTask<Void, Void, Boolean> { 

     @Override 
     protected void onPreExecute() { 

      Intent myIntent = getIntent(); 
      boolean button = myIntent.getBooleanExtra("LoginButton", false); 

      if (!pref.getBoolean("isCheckBoxChecked", false) && !button) { 
       Intent intent = new Intent(musicPlayer.this, LoginActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       getApplicationContext().startActivity(intent); 
       finish(); 
       cancel(true); 
      } 

      if (button) { 
       getUser = myIntent.getStringExtra("userLogin"); 
       getPassword = myIntent.getStringExtra("passLogin"); 
      } 

      Log.i("TEST", "User: " + getUser + " pass: " + getPassword); 
     } 

     @Override 
     protected Boolean doInBackground(Void... params) { 
      try { 
       Log.i("TEST", "Init"); 

       api = DefaultClientFactory.create(host, getUser, getPassword); 
       Log.i("TEST", "" + api.getUserInfo()); 

       return true; 
      } catch (StorageApiException e) { 
       e.printStackTrace(); 
       return false; 
      } 
     } 

     @Override 
     protected void onPostExecute(Boolean result) { 
      super.onPostExecute(result); 

      if (!result) { 
       Intent intent = new Intent(musicPlayer.this, LoginActivity.class); 
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       getApplicationContext().startActivity(intent); 
       Toast.makeText(getApplicationContext(), "Invalid E-mail or Password !", Toast.LENGTH_SHORT).show(); 
      } else { 
//    new backgroundTask().execute(); 
      } 

     } 
    } 
} 
+0

你確定它運行了兩次還是隻有兩次運行? –

+0

是的,一切都運行兩次,我的兩個asynctasks運行兩次,onCreate運行兩次。 –

+0

請發佈清單和活動 –

回答

0

活動啓動模式應謹慎使用。

機器人:launchMode =「singleTop」

如果活動的實例出現在任務堆棧的頂部,一個新的實例將無法通過onNewIntent創建,系統將路由你的意圖的信息()。如果它不在頂部,則會創建一個新實例。可以創建多個實例,每個實例可以屬於不同的任務。Good post on activity launch mode

  1. 爲什麼要檢查在onPreExecute()共享PREF,然後取消它?檢查完成後,更好的方法是執行asynctask。

  2. 取消(true)不保證一旦調用它就會停止異步任務。更好的方法是在doInBackground()中添加isCancelled()檢查,如果取消,則返回根據需要返回的結果。這將有助於儘早完成asynctask,而無需執行寫在那裏的任務。

  3. 在清單中,您可能想要添加android:conifgChanges以更好地處理方向。

    機器人:configChanges = [ 「MCC」, 「MNC」, 「區域」, 「觸屏」, 「鍵盤」, 「keyboardHidden」, 「導航」, 「屏幕布置」, 「fontScale」, 「uiMode」, 「方向」, 「屏幕尺寸」, 「smallestScreenSize」]

希望這有助於!