我在应用程序的某些活动和片段中使用Media Player。一切正常,没有问题。导入,开始,停止,释放。
问题
我的家庭活动根本不使用MediaPlayer。没有导入或任何相对的代码。当我启动该应用程序时,会播放音乐。关闭应用程序(onDestroy())音乐停止。再次启动活动,音乐开始。 onStop()继续播放。家庭活动是MainActivity,这是第一个启动活动。这怎么可能?即使在我启动第一个应用程序活动(HomeActivity)时仍保留其他活动(不停止,不发布任何东西),也不应播放任何内容。
澄清
不需要代码,因为它与媒体播放器没有任何关系。
编辑
根据要求,我发布活动以防万一:
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import java.text.SimpleDateFormat;
import java.util.Date;
import eu.healthydev.triviaarena.Interface.RankingCallBack;
import eu.healthydev.triviaarena.Intro.PrefManager;
import eu.healthydev.triviaarena.Model.QuestionScore;
import eu.healthydev.triviaarena.Model.Ranking;
import eu.healthydev.triviaarena.Model.User;
public class UserProfile extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
private FirebaseDatabase database;
private ImageView userAvatar;
private TextView userName;
private TextView userScore;
private TextView PvPScore;
private TextView PvPWins;
private TextView userClass, userGender, dateofJoin;
private Button btnEnterDungeons;
private String imageURL, user_class;
long totalScore;
private static String TAG = "";
DatabaseReference questionScore, rankingTbl;
int sum = 0;
GoogleApiClient mGoogleApiClient;
PrefManager prefManager;
@Override
protected void onStart() {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
mGoogleApiClient.connect();
super.onStart();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_profile);
firebaseAuth = FirebaseAuth.getInstance().getInstance();
database = FirebaseDatabase.getInstance();
questionScore = database.getReference("Question_Score");
rankingTbl = database.getReference("Ranking");
prefManager = new PrefManager(this);
//Checking whether a user as already Logged In
if (firebaseAuth.getCurrentUser() == null) {
finish();
//Starting the User Login Activity if the user is not Logged in
startActivity(new Intent(this, MainActivity.class));
}
final FirebaseUser user = firebaseAuth.getCurrentUser();
userAvatar = findViewById(R.id.userAvatar);
userName = findViewById(R.id.nickName);
btnEnterDungeons = findViewById(R.id.enterGame);
userClass = findViewById(R.id.userClass);
userGender = findViewById(R.id.user_genderA);
userScore = findViewById(R.id.userScore);
PvPWins = findViewById(R.id.pvpWins);
PvPScore = findViewById(R.id.pvpScore);
dateofJoin = findViewById(R.id.dateJoined);
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.setTitle("");
toolbar.setSubtitle("");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
btnEnterDungeons.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(UserProfile.this, GameChooseActivity.class);
prefManager.setFirstTimeLaunch(false);
startActivity(intent);
finish();
}
});
getData();
updateScore(firebaseAuth.getCurrentUser().getDisplayName(), new RankingCallBack<Ranking>() {
@Override
public void callBack(Ranking ranking) {
//Update to Ranking table
rankingTbl.child(ranking.getUserName())
.setValue(ranking);
rankingTbl.child(ranking.getUserName()).child("avatar").setValue(imageURL);
rankingTbl.child(ranking.getUserName()).child("user_class").setValue(user_class);
//showRanking(); //After upload we will short ranking table and show result
}
});
getScore();
}
//Method to get data from Firebase user and show them in profile
private void getData() {
final Context mContext = UserProfile.this;
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String userid = user.getUid();
final long joinedDate = user.getMetadata().getCreationTimestamp();
final SimpleDateFormat sdf = new SimpleDateFormat("MMM dd,yyyy HH:mm");
final Date resultdate = new Date(joinedDate);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Users");
reference.child(userid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
userName.setText(user.getUserName());
userClass.setText(user.getUser_class());
imageURL = user.getAvatar();
Picasso.with(mContext).load(imageURL).into(userAvatar);
dateofJoin.setText((sdf.format(resultdate)));
PvPWins.setText(String.valueOf(user.getPvP_Wins()));
PvPScore.setText(String.valueOf(user.getPvP_scores()));
userScore.setText(String.valueOf(totalScore));
userGender.setText(user.getUser_gender());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
//Method to get the userScore from Ranking table in firebase
public void getScore() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String name = user.getDisplayName();
DatabaseReference rankingScore;
rankingScore = database.getReference("Ranking");
rankingScore.orderByChild("userName").equalTo(name)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
Ranking ranking = data.getValue(Ranking.class);
totalScore = ranking.getScore();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void updateScore(final String userName, final RankingCallBack<Ranking> callback) {
questionScore.orderByChild("user").equalTo(userName)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
QuestionScore ques = data.getValue(QuestionScore.class);
sum += Integer.parseInt(ques.getScore());
}
//After summary all score, we need process sum variable here because firebase is async DB
Ranking ranking = new Ranking(userName, sum, imageURL, user_class);
callback.callBack(ranking);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@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_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.about_game:
Intent intent = new Intent(this, AboutGameActivity.class);
startActivity(intent);
return (true);
case R.id.about_us:
Intent intentus = new Intent(this, AboutUsActivity.class);
startActivity(intentus);
return (true);
case R.id.switch_user:
FirebaseAuth.getInstance().signOut();
googleLogout();
Intent switchuser = new Intent(this, MainActivity.class);
startActivity(switchuser);
finish();
return (true);
case R.id.logout:
FirebaseAuth.getInstance().signOut();
Intent logout = new Intent(this, MainActivity.class);
startActivity(logout);
finish();
return (true);
}
return (super.onOptionsItemSelected(item));
}
public void googleLogout() {
Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
}
});
}
}
就像我说的与mediaPlayer无关。因此,完全退出该应用程序,点击应用程序图标,这是第一个活动,正在播放音乐...。我无法想象这是怎么可能的。可能从其他地方获取音乐...但是如何?