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.
Related
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.
I'm developing a scientific app in Android Studio. It works smoothy.
The set of source code files is not small, but, as I don't have practically user interface, there is only one activity and there is no intent.
All initialization code is inside OnCreate. Most of times, my app preserves all data, when he gets out of the foreground.
However, maybe (I cannot find a pattern of this event) he loses all data and restart (shows a white screen for 2 / 3 seconds), even if the cell phone don't enter in lock screen and there are just 2 apps running.
There are situations that I comute for another app (like WhatsApp) and resumes for my app, and my data was gone. The app restart again.
There is no error message, no logcat. Nothing.
Mostly, when I lock the screen and enter again, all my app data is there.
PS: My orientation is locked.
PS 2: I've read all related question and there is no hint for me. Based in one answer, I've tried to put in onCreate the following code.
if (!isTaskRoot() {
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}
No changes for me.
Update:
I've stumbled in solution. it can be read in my own answer. it's related to undesired back button effect for one-activity-app (read here and here ).
For me, as my application has only one activity, back needs to be like a home button: exit the app but preserve all activity data. My app has a real exit button, where the user shows that really wants to do this.
It's my first app that I developing in mobile world and, for extension, Android world
Some problems seems to me like that it is only possible find the solution if one has a hint about its solution. it's a contradiction. One doesn't know but has to know to solve that don't know!
And, in this situation, it's not the case. No hints. Just question marks.
Before, I had not noticed any pattern. People sometimes act so automatically ... However, suddenly the penny dropped.
I've stumbled in solution. Fortunately!
Not in a million years could I suppose that if someone has an activity and presses Back button, (right button in the bottom), you practically quit the application, even if it remains as a running app for the left button in the bottom (app switcher button)
When I've noticed it, I start to research with another focus. And I've discovered two sources: Disable back button in android and Android - Simulate Home click
So the solution is simply to make the Back button act like the Home button (middle button in the bottom). Return to the home screen without losing application data.
And this is done simply by putting in the onCreate, for my only activity, the following code.
override fun onBackPressed() {
val i = Intent(Intent.ACTION_MAIN)
i.addCategory(Intent.CATEGORY_HOME)
startActivity(i)
}
So, if you haven't noticed. Many new launchers give you the option to select an application to be launched as default for a specific category.
My launcher consists of different fragments which displays cards, these cards hold a button which has a function where when the user touches the button, they're prompted with a window where they can select which application to run for that specific view. To make things easier for the user, the intent or app they selected should be saved, so if they select the button again, they don't have to select the app again.
If you would like a few examples to what I mean, take a look at 9 Cards Launcher or Smart Launcher, or any Windows 7 Phone Launcher. They prompt you with a small pop up which lets you select the application you want to run and its set.
how can I achieve this? Please I have literally searched everywhere, but nothings making sense. There are no tutorials on this, I have also decompiled various launchers to see how this works but I don't know where to start.
It'd be great if you could help.
1st of all you need to get the Installed Applications.
Then you will get the application package info from the user selected Application.
Finally launch the intent.
Here's what you can try out:
Create an intent with action=MAIN and category=LAUNCHER
Get the PackageManager from the current context using context.getPackageManager
packageManager.queryIntentActivity(<intent>, 0) where intent has category=LAUNCHER, action=MAIN or packageManager.resolveActivity(intent, 0) to get the first activity with main/launcher
Get the ActivityInfo you're interested in
From the ActivityInfo, get the packageName and name
Finally, create another intent with with category=LAUNCHER, action=MAIN, componentName = new ComponentName(packageName, name) and setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
Finally, context.startActivity(newIntent)
I would also suggest to take a look at FreeTaskManager
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'm currently working on my first android project, which is to modify an existing sample program ("Tic Tac Toe" game). I'm going through the tutorials on the official website, but looking at the sample code I don't think I'll be able to figure everything out on my own in the time I have.
The modifications include being able to select a custom background, setting up a scoring system, and implementing a timed "blitz" mode. My basic questions are:
In which subfolder would the code that sets the background color/image for the game be?
Is there a way to create an Intent function that opens up a file search window to allow a user to select this custom background image?
I'd like to start here, I'm sure I'll have more questions as I go along. As always, any help is appreciated. (By the way, the code from the game is from the standard sample problems that come installed with the Android SDK for Eclipse).
Update 1:
So far I found this in a class called GameView:
mDrawableBg = getResources().getDrawable(R.drawable.lib_bg);
setBackgroundDrawable(mDrawableBg);
mDrawableBg is a Drawable object, I'm not sure what this part is refrencing:
R.drawable.lib_bg
What would be the proper way to modify the background in this piece of the code?
Update 2:
Here's where I'm at:
I have the getDrawable function taking another function as an argument:
mDrawableBg = getResources().getDrawable(getImage());
getImage() is suppose to return a integer referencing the selected image, here is the code (so far) for that function:
public int getImage(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 10);
}
This is suppose to open the gallery and let the user select an image, I'm not sure how to return a reference ID to that selected image though. Also, the startActivityForResult function is not working properly, I don't think I'm using the Activity class properly.
The background is probably defined as either a drawable (in /res/drawable-*) or a color value (in /res/values/colors.xml or something like that). It will be referenced in one of the layout files in /res/layout. The layout file will be referenced by one of the activity classes in the Java source folders.
You can declare in code an array of drawable resource IDs and use that to dynamically generate a dialog and/or activity. The HorizontalScrollView widget might be useful for this. If you start a selection activity with an intent, use startActivityForResult instead of startActivity. You can then set the background of your top view using setBackgroundResource().