I have made a Flash Air game. While playing it, if I press the Home button on my Android device, I get to the phone's menu yet I still get to listen to the game's music, which means that the game still keeps on fully working.
I would like my app to pause when not in the foreground.
I've seen these codes but apparently I'm doing something wrong cause they aren't working probably...
NativeApplication.nativeApplication.addEventListener(Event.DEACTIVATE, handleApplicationDeactivated);
NativeApplication.nativeApplication.addEventListener(Event.ACTIVATE, handleApplicationActivated);
I've listed the:
import flash.events.Event;
import flash.desktop.NativeApplication;
Could you help me with the proper code?
The code below will run the onPause when you press the home button, and onResume when you get back in your app.
SoundMixer.stopAll(); will stop ALL sounds that are playing.
this.stage.addEventListener(flash.events.Event.DEACTIVATE, onPause, false, 0, true);
this.stage.addEventListener(flash.events.Event.ACTIVATE, onResume, false, 0, true);
private function onPause(event:flash.events.Event):void
{
//here save your games state.
//then stop ALL sound from playing with the following line:
SoundMixer.stopAll();
//Remember to import flash.media.SoundMixer;
}
private function onResume(event:flash.events.Event):void
{
//here you start the sounds again, for example:
sound.play();
}
Related
I made a Music Player that operates in two ways:
If started as a normal app, it continues playing what it was playing when last closed, and it should start in the background;
If an audio file is selected in a fileManager, it plays the song clicked in the foreground.
Since I want only one instance of the app running, so I added to the mainfest:
android:launchMode="singleTask"
What I did to accomplish this was to execute this onCreate:
uri = getIntent.getData();
if (uri != null) {
newsong = uri.toString()...
play(newsong);
} else {
moveTaskToBack(true);
playoldsong();
}
It works great, except for the fact that, when started as in (1) and, after a while, I select a song in a filemanager, the main activity is brought to the front (which is what I want) BUT it continues playing what it was playing before the song selection. The player ignored the new song, and I want it to switch to it.
This actually makes sense, since the only place where I call the new song is in the onCreate.
Maybe the launchMode is not the correct one? Maybe I should #Overwrite the OnNewTask(Intent) method? Which is the 'cheaper' solution?
You have to override onNewIntent() to start the song the user clicked on in the file manager.
My app have a background service (Android) which look periodicly for new data. If there are new data a notification is started. By tapping the notification a little native code piece is running and this will bring back to front the Nativescript app.
Snippet from the Kotlin class to bring back the Nativescript app:
val launchIntent: Intent? = packageManager.getLaunchIntentForPackage(applicationContext.packageName)
launchIntent?.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT or Intent.FLAG_ACTIVITY_SINGLE_TOP)
launchIntent?.setAction(ACTION_NOTIFICATION_AVAILABLE)
startActivity(launchIntent)
In the AppComponent class my listener for a AndroidApplication.activityResumedEvent will listen:
export class AppComponent implements OnInit {
constructor(
private router: Router,
private ngZone: NgZone,
) {}
ngOnInit(): void {
android.on(AndroidApplication.activityResumedEvent, (args) => this.ngZone.run(() => {
let intent = args.activity.getIntent();
if (intent.getAction() === com.sample.app.NotificationTapReceiver.ACTION_NOTIFICATION_AVAILABLE) {
this.router.navigate(['/notificationList']);
}
}));
}
}
Most of the times this code works as aspected. But in my opinion we have three diffrent situations:
App is in foreground and running when a notification is incomming and the use tap on it. => all works fine
The app is not running only the background service. If the user tap the notification the app will fully start and move to the notification list. => all works fine
Problem: The App is running but not in the foreground. By tapping the notification the app resume (without restart) to the front and navigate to the notification list BUT the user interface are freezed. By tapping the elements on the screen, the inside code are running (consoloe.log(...)) but no action on the UI. Maybee the UI are not freezed only the routing/navigation don't work.
I find the question Nativescript - How to bring app to foreground when notification is pressed but in my case there are no difference between android emulator and (not connected) real android device.
Tests with the flags to start the activity will change the behavior but not in the right way
# FLAG_ACTIVITY_SINGLE_TOP might be the best solution but the described error above is very annoying
launchIntent?.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT or Intent.FLAG_ACTIVITY_SINGLE_TOP)
# even the app is in foreground after a tap on the notification the app is fully restarts
launchIntent?.addFlags(FLAG_ACTIVITY_NEW_TASK)
Have anyone an idea where the problem is or which flags i have to set to prevent the current state and the fully working UI (with router/navigation)? Or do i have to modify the nativescript/angular code the better handle the resume event?
Not the best solution but when i was able to trim the flags to get the in the foreground running app still running and in the other two states (app dead or app in the background) a fully restart is initiated, at the moment this will be fine too.
Update: It look like, that the UI not freezed. I added a RadListView and i can scroll to the list, still the routing/navigate don't work. Even i use the this.routerExtensions.back() nor i use the this.router.navigate(['notificationDetail']).
I have a basic Activity which mainly allows the user to change settings and save them. I also have a BroadcastReceiver which is launched on SMS_RECEIVED.
The main point of the app is to vibrate whenever a certain message is received until the user taps a button to make it stop. The activity is only there to allow the user to change settings and press the "Stop" button.
In my onReceive method (BroadcastReceiver), I get the content of the last message received and make the phone vibrate if the message is equal to a certain string. All of that is working perfectly, the problem is when I want to make it stop. Right now, I'm trying to make a "Stop" button appear in the Activity when the phone starts vibrating.
I understand that UI elements should remain in the Activity and so what I'm trying to do is communicate between the Activity and my BroadcastReceiver. I've found here how to do that with an Observer. The problem though is that I want the app to function at any time, even at boot time. It's very easy with a BroadcastReceiver but since it requires the Activity to be shown to allow the user to stop the vibration, I have to start the activity if it isn't started already.
So what I do is this:
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("SMSReceived", true);
context.startActivity(i);
ObservableObject.getInstance().updateValue(true);
The problem is, when there is no instance launched, it creates a new one and sends the extra boolean correctly but the updateValue method doesn't seem to get called at all (due to the previous instance I suppose?) and inversely, when there is an instance launched (in the background) the extra boolean doesn't get passed and the updateValue method gets called correctly.
I suppose I could just launch the Activity on boot and immediately put it in the background but it could cause problems if the user closes the application, at which point it would simply stop working until the user started it again since the Observer would have no instance to send data to.
Do you guys have any idea of what I could do to solve my problem?
If it's not clear I can try to explain further.
I got a bug on my MediaPlayer.
When i click on an item of my playlist, it create a mp3Player. If i press back the music play in background (all is normal). But if i choose another song from my app, the app create a new mediaplayer:
Then i got 2 music playing !
I tried to use "singleTask", "singleInstance", and it's not working.
I tried "intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);"
None of this work!
Maybe if i could resume the previous Activity, i could destroy it and then create a nex mediaplayer no?
Can someone help please? :D
You play music in backround it means you use Service ?
Problem is you need reconnect with Service (Service play music).
How to reconnect service android
Or try. Media Playback , EG: Universal Android Music Player Sample
you can simply make your activity android:launchMode="singleInstance" in manifest.
and then in your activity overide below method
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// do what you want here
}
in above method, you can reset value of media player
I'm currently porting a Flash game to OUYA (an Android powered device) via Adobe AIR.
Most of the work has been done already; it's packaged up and runs on Android devices.
However! When I double-tap the middle button to return to the system home screen (which I believe to be the equivalent of pressing the home button on an Android mobile), the app doesn't seem to pause or exit, and the music can still be heard.
Does anyone have any tips on how to suspend or exit an AIR app when the home button is pushed? Thanks!
Try listening for Event.DEACTIVATE. That dispatches when Flash/AIR loses focus in the context of the operating system/browser. Should be exactly what you are looking for. You should attach it to NativeApplication.nativeApplication, I believe. Attaching it to the Stage might also work, but I'm not entirely sure.
If you want to listen for the app to exit, listen for Event.EXITING on NativeApplication.
I ended up doing this:
this.stage.addEventListener(flash.events.Event.ACTIVATE, onResume, false, 0, true);
this.stage.addEventListener(flash.events.Event.DEACTIVATE, onPause, false, 0, true);
In my init, and then:
private function onPause(event:flash.events.Event):void
{
SoundMixer.stopAll();
}
private function onResume(event:flash.events.Event):void
{
FP.world = new MenuWorld();
}