I would like to show a notification and play a sound when the user taps onto that notification. It works somehow when I use an activity to play the sound, as I have written in my own answer to this question: How can I create a notification that plays an audio file when being tapped? (in this question and answer there is also the source code showing how I create the notification and how my PlaySoundActivity looks like.
Yet, I have realized, that while the sound is playing, the appearance of my main application changes and it will not be restored without closing the application.
I have created my application from the "Tabbed Activity" project template.
This is how it looks after being started:
And this is how it looks when I have tapped onto the sound notification (the sections are gone):
Can anyone explain why this happens? Is it a wrong approach to play sound using an activity? But it does not work here when I use a service, I hear nothing! How to solve that?
According to the Android developer reference, "almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)".
I had not done this. Therefore a new window was displayed, but there was no content.
The better solution for playing a sound is to use a PlaySoundService (service!!!) instead of an activity. It contains almost the same code as an activity would do, but the pending intent is created with PendingIntent.getService(...) instead of PendingIntent.getActivity(...). Using the wrong method does not result in an obvious error message, but the service won't work as expected.
Related
I am trying to make an App which is going to display Some Images and Videos. So I am planning to add a splash screen of around 2seconds. After 2 seconds the user will be taken to the Main Screen of the App.
I want to start the loading of the Images, Video when then user is at the splash screen itself so that the user should wait for the least time when he is at the Main Screen.
So the loading will be started at the Splash Screen and then after two second the user will be taken to main screen irrespective of the completion of the loading.
Now since this involves two activities should I use a Async task or should I Use a service with an Async Task(For the callback of completion of code) within it?
Which one would be better. Also in Android 8.0 are there any restriction in using Services?
I think using a Async Task between two screens may cause Memory leak if not coded properly.
Any help would be really grateful.
EDIT: My app is having one more feature hence cannot make the user wait in the Splash Screen till the loading is over.
It is not very good to use AsyncTask for sharing results between 2 activities, because AsyncTack created in Splash activity will be destoyed (stopped) when switched to Main activity. Better to use service in this case and Main screen will subscribe for result.
So basically you want to start the download in the splash screen and continue the download in the activity that follows. In this way, you still have to implement a loading animation. In your case, I would recommend finishing your splash screen, as soon as everything is downloaded. In that way, you don't have to download anything anymore inside the app's lifecycle.
AsyncTasks continue to run even after switching to a new activity. You can try the following flow:
1. Splash screen
2. Trigger Async Task
3. Main Activity
4. Show Images/Videos
The only catch is, you will not be able to fix a time for #2 to complete to be able to start #4. This is the nature of AsyncTasks. You can workaround by using the OnPostExecute within AsyncTask.
Example: OnPostExecute call another method that will enable a button. Users can click on the button to view Images/Videos. But then, this might not be a good user experience to see some button suddenly getting enabled.
In that case I would rather create some Singleton with own Handler (that works in separate Thread), that will be started at Splash screen. After Main screen will start, it should ask that Singleton about respective data or should sign himself for receiving that data.
I want to execute a piece of code (say, for example, display a Toast) every time that the app is opened. So far I have managed to do this every time the app is launched by putting the code into my MyApp.java file that extends Application.
However, if I press the homescreen or back out of the app and then go into it, the message doesn't reappear. It only does when I relaunch the app. Any idea how to do this?
EDIT:
basically im asking how to execute code everytime the whole APP is brought to foreground (this can be first time open, after another app was used, after user backed out of app, etc). Where would I place onResume code? It wouldn't be in a particular activity, would it, since I want it to apply when entire app appears in foreground, not just particular activity.
You can try writing that code in your activity's #Override-d onResume() method.
The only way to do this is,
Determine which app is currently in the foreground
.Follow this discussion for getting an idea for the best way to do it.
[Determining the current foreground application from a background task or service
Suppose, if the function name is 'getCurrentForgroundApp()',
You need a service to execute getCurrentForgroundApp(); every one second
(1-second interval is depending on your purpose, can be lower or higher).
Now, you can identify which app is running foreground in every second.
So, check if your app is the one running foreground. If true, then execute the toast or code you need.
This is how app-locker apps showing lock screen over selected apps, whenever they come to the foreground.
You have to use the onResume callback:
Android API
Example of use from previous SO question
In activity class:
#Override
protected void onResume() {
super.onResume();
//your code here
}
I am sure this is a basic question, but I'm developing an Android app using ADK and Eclipse.
When I try to transition Activities, the screen goes black and then briefly shows the previous activity screen before "flipping" to the new screen.
I don't have any custom transitions; I'm using the default.
I don't have much going on in my onCreate event (EXCEPT: I load my background image during onCreate! Could it be this?)
I really am looking at a very snappy transition, like a game like Words with Friends, where it appears to switch "instantly".
This depends on what phone you're using since different phones use different anamations. Try to call finish(); on every activity but your main one for example.. Or finish them all when the user switches activities
I am java programmer, but i never put my hand on android before. I just want to clear some basic stuff that the different between programming a window app and a android app.
I know how to programm a window app that has pop up windows inside the app.
for example:
MyWindowClass m= new MyWindowClass(new java.awt.Frame(), true);
m.setVisible(true);
But i do not know how to open a new view or layer at Android.
Can someone give me some hinds.
If you just want to display a pop-up window, just use a Dialog.
If you are looking to add an entirely new layer to your application, consider starting a new Activity (you can use either Context.startActivity or Context.startActivityForResult if you are interested in returning a value (placed in a Bundle) from that Activity.
You can also have another layer by starting a new Activity with a transparent background, but there are some limitations when it comes to that such as user input not being passed to the Activity behind it.
EDIT:
If you want to have "multiple windows" and not have them lose state (unless they are closed by the system), you can also use startActivity with an Intent on which you've added the FLAG_ACTIVITY_REORDER_TO_FRONT flag.
I am using the default Android Media Player in an Activity, trying to play back a couple of video files. However I have a problem with programming what I want. The desired program outcome would be the following:
A video file is played back
After that, a dialog is shown, asking the user something
A pause of 5 seconds occurs
The next video is shown
and so forth
How am I to program this? Currently, I use a paradigm like the following:
I have a method that sets up the player for theĀ a file, e.g. playVideo(int) So I pass it the first file.
When the player is prepared, it will be started in onPrepared(MediaPlayer).
When the video is finished, the onCompletion(MediaPlayer) listener of the media player shows the dialog to the user by calling showDialog().
The user accepts the dialog. Before calling dismiss() on the dialog, the player object is started for the next file by calling playVideo(int).
This works, but it feels a bit quirky and not clean. And it's of course very procedural. The problems are:
that I can't figure out how to insert a pause after the dialog is dismissed.
that I maybe want to change the presentation order of dialogs and videos and this ain't too easy now
Has anyone got an idea?
For the pause, you could use the AlarmManager to schedule an alarm five seconds from now. It will launch an intent, and that intent could call playVideo(int).