I have an intent that launches an Audio Player Intent, and plays an Audio File. The Audio Player offcourse is external to my application. When the user presses the back button, my activity is again displayed to the user. I want the Audio Player to close when the user presses the back button. How can i control this.
There are two options to achieve your requirement
call "finish()" after startActivity() which is used to start audioplayer. finish() function will close your application and when user press back button from audioplayer, Android will display previous activity most probably home screen.
User CLEAR_TOP flag when you start the activity. Clear top flag will clear all top activity so when user press back from audio player, Android OS will display Home Screen.
Intent i = new Intent(......);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Thank You
I placed this code on the OnResume() of my calling activity and it works like a GEM.
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
AudioManager mAudioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if (mAudioManager.isMusicActive())
{
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
this.sendBroadcast(i);
}
Related
I'm trying to create an application that will only let you in a another activity if you plug in your headphones. I got a code, that works, but the problem is that it's checking the headphone is plugged in or not when the activity starts only. I want the application to always check the headphone state. Not only when the activity creates. How to do this?
AudioManager audioManager = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
if(audioManager.isWiredHeadsetOn()) {
Intent intent1= new Intent(this,Main2Activity.class);
startActivity(intent1);
}
Here's the code, it's fully functional, but how to make it to always check the state of the headphones if you in the activity? Because the app only checks the state of the headset when it's starts, so you have to plug in your headphone at first, and then start the application to make the new intent work.
Thanks for every answer.
In my app would like user to click on a button on actionbar. This action to invoke default mediaplayer, so that user can select any music as they like. Need ability to return to the app while the music is played in the background.
Any help is appreciated.
Following code works as intended...
Intent intent = new Intent();
intent=Intent.makeMainSelectorActivity(Intent.ACTION_MAIN,
Intent.CATEGORY_APP_MUSIC);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//Min SDK 15
startActivity(intent);
I am looking for a way to pause Spotify playback. I have tried searching, and using the AudioManager, but I believe that only works if it is part of your activity. Does anyone know a way to pause Spotify playback?
How to stop or pause Pandora and Spotify
That issue is similar and shows how to pause and stop most media players.
I found that this works best:
Intent pauseSpotify = new Intent("com.spotify.mobile.android.ui.widget.PLAY");
pauseSpotify.setPackage("com.spotify.music");
sendBroadcast(pauseSpotify);
Essentially, what you are doing is, creating a new intent which action is the built in "PLAY" action from the spotify app. Next you set the package and finally call the intent.
I got the idea from an article and applied it to normal android.
//play
Intent intent = new
Intent("com.spotify.mobile.android.ui.widget.PLAY");
intent.putExtra("paused",true);
intent.setPackage("com.spotify.music");
sendBroadcast(intent);
//pause
Intent intent = new
Intent("com.spotify.mobile.android.ui.widget.PLAY");
intent.putExtra("paused",false);
intent.setPackage("com.spotify.music");
sendBroadcast(intent);
I've analyzed Spotify's apk file and found the command file.
This would work perfectly on version 8.4.77
You can skip song by replacing "PLAY" to "NEXT".
I tried "PREVIOUS" but it didn't work, guessing it as spotify's default design.
In my small Android application I am calling the inbuilt voice recorder using
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, REQUEST_CODE_RECORD_SOUND);
and I don't want the list button in the voice recorder. I tried a lot but am not able to hide it.
Also I want to ask that can my activity sense the actions taken on voice recorder like Record, start, stop, pause.
how can I return back to the previous activity once finish recording video ? , In my situation If I captured video then two options shown " Save" and " Discard"... . I want to remove these option and back to previous activity automatically.
Thanks in advance
call finish() when recording is completed
When Recording gets finished just after that part of code fire an Intent which returns you back to previous activity. Intent firing is done like this ->
Intent intent = new Intent(this, YourPreviousActivityName.class);
startActivity(intent);