我在nexus 7 2013上運行我的應用程序,在logcat中,我注意到一些日誌運行了兩次。在寫了更多的日誌之後,我斷定每個活動都在平板電腦上運行兩次。奇怪的想法是,在手機上(LG G3或三星Galaxy S4 mini)日誌只能打印一次。Android活動正在運行兩次
在做了一些研究之後,我嘗試添加android:launchMode="singleTop"
或singleTask
或singleInstance
但沒有任何工作。此外,我還有一些意圖與國旗活動: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();
}
}
}
}
你確定它運行了兩次還是隻有兩次運行? –
是的,一切都運行兩次,我的兩個asynctasks運行兩次,onCreate運行兩次。 –
請發佈清單和活動 –