stopService() works on only one device

Asked

Viewed 16 times

0

I am developing a streaming application and whenever a new song is chosen the application for the service that is playing the current song and then starts it again with the new song chosen by the user, however, this works perfectly on my mobile (Moto G7 Play, Android 10), already if I use the same app on another phone (Moto G7 Play, Android 10), IE, another phone exactly like mine, this no longer works. I realize the problem is that on another cell phone the stopService() It’s not working for some reason, but my phone works, which ends up leaving me very confused, because how can something work perfectly on my phone and not another. I tried to look at the logcat and the same is presented in the two cell phones, but in the other cell (not mine) sometimes appears the message service not registered but still stopService() is not working.

My code:

private boolean mBound = false;
private SimpleExoPlayer player;
private PlayerView playerView;
private PlayerControlView mPlayerControl;
private AudioPlayerService mService;
final Handler handler = new Handler();

private ServiceConnection mConnection = new ServiceConnection() {
    @Override
    public void onServiceConnected(ComponentName name, IBinder service) {
        AudioPlayerService.LocalBinder binder = (AudioPlayerService.LocalBinder) service;
        mService = binder.getService();
        mBound = true;
        inicializarExoPlayer();
    }

    @Override
    public void onServiceDisconnected(ComponentName name) {
        mBound = false;
    }
};

@SuppressLint({"ClickableViewAccessibility", "CutPasteId"})
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);

    // ATTENTION: This was auto-generated to handle app links.
    Intent appLinkIntent = getIntent();
    String appLinkAction = appLinkIntent.getAction();
    Uri appLinkData = appLinkIntent.getData();
    if (appLinkIntent != null) {
        try {
            params = appLinkData.getPathSegments();
            String id = params.get(params.size() - 1);

            firebaseFirestore.collection("musicas").document(id)
                    .get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot.exists()) {
                        SharedPreferences.Editor editor = getSharedPreferences("NomeAutorForNotification", MODE_PRIVATE).edit();
                        editor.putString("link_imagem", documentSnapshot.getString("link_imagem"));
                        editor.putString("nome_musica", documentSnapshot.getString("nome_musica"));
                        editor.putString("autor_musica", documentSnapshot.getString("autor"));
                        editor.putString("id_musica", documentSnapshot.getString("music_id"));
                        editor.putString("link_musica", documentSnapshot.getString("link_musica"));
                        editor.putString("aid", documentSnapshot.getString("aid"));
                        editor.apply();
                        inicializarPlayerActivity();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(PlayerActivity.this, "Ocorreu um erro!", Toast.LENGTH_SHORT).show();
                }
            });

        } catch (Exception e) {
            e.printStackTrace();
            inicializarPlayerActivity();
        }
    }
}

private void inicializarPlayerActivity() {
    editor = getSharedPreferences("NomeAutorForNotification", Context.MODE_PRIVATE);
    link_imagem = editor.getString("link_imagem", null);
    nome_musica = editor.getString("nome_musica", null);
    autor_musica = editor.getString("autor_musica", null);
    id_musica = editor.getString("id_musica", null);
    link_musica = editor.getString("link_musica", null);
    aid = editor.getString("aid", null);

    try {
        musicPropertiesBackup = getSharedPreferences("MusicPropertiesBackup", MODE_PRIVATE);
        link_imagem_backup = musicPropertiesBackup.getString("link_imagem", null);

        System.out.println("Link Imagem é: " + link_imagem_backup);

        if (link_imagem_backup != null && link_imagem_backup.equals(link_imagem)) {
            System.out.println("A música ecexutada é a mesma!");
            if (sharedPreferences.getString("initialize_player_before_exit", null) != null &&
                    sharedPreferences.getString("initialize_player_before_exit", null).equals("ok")) {
                inicializarNovaMusica();
            } else {
                bindService(intent, mConnection, BIND_AUTO_CREATE);
                inicializarExoPlayer();
            }
        } else {
            System.out.println("A música executada não é a mesma!");
            SharedPreferences.Editor editor = getSharedPreferences("MusicPropertiesBackup", MODE_PRIVATE).edit();
            editor.putString("link_imagem", link_imagem);
            editor.apply();
            inicializarNovaMusica();
        }

    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Não há nenhum link de backup salvo!");
        SharedPreferences.Editor editor = getSharedPreferences("MusicPropertiesBackup", MODE_PRIVATE).edit();
        editor.putString("link_imagem", link_imagem);
        editor.apply();
    }

    Picasso.get()
            .load(link_imagem)
            .into(imageMusica);

    nomeMusica.setText(nome_musica);
    autorMusica.setText(autor_musica);

    mDocUserRef = FirebaseFirestore.getInstance().collection("usuarios").document(userId)
            .collection("user_propriedades").document("propriedades").collection("favoritos")
            .document(id_musica);
    mDocUserRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            if (documentSnapshot.exists()) {
                favoriteReference = documentSnapshot.getString("music_id");
                starFav.setImageResource(R.drawable.ic_baseline_star_fav);
                fav = true;
            }
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            System.out.println("Não foi possível encontrar suas músicas favoritas");
        }
    });
}

private void inicializarExoPlayer() {
    if (mBound) {
        player = mService.getPlayerInstance();
        playerView.setPlayer(player);
        mPlayerControl.setPlayer(player);
        addToRecent();
    }
}

private void inicializarNovaMusica() {
    stopService(intent);
    bindService(intent, mConnection, BIND_AUTO_CREATE);
    Util.startForegroundService(PlayerActivity.this, intent);
    inicializarExoPlayer();
    addViews();
    SharedPreferences.Editor editor = getSharedPreferences("Preferences", MODE_PRIVATE).edit();
    editor.putString("initialize_player_before_exit", null);
    editor.apply();
}

The stopService() this in the method inicializarNovaMusica() since this is responsible for 'reset' the service when there is a new song. I see no error in the code and much less an alternative so I can solve this problem. If anyone has another idea of how to reset the service in the way that works on all devices, I’ll be accepting. Remember that the app is available for apps from SKD 22 Android.

1 answer

0


I was able to solve the problem by adding a System.out.println('Stop Service') in the method OnDestroy() service. I found it very strange that it solved the problem, but at least it solved.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.