I'm developing an app that uses the sensor accelerometer. I want the display to go black for instance when a value reach 8.1 and go back to normal when 7 is reached. Is it possible without using screen brightness? I want it to be complete black and this is not possible what i know with screen brightness.
Hope I gave enough information.
Try this in your activity. Let your main.xml load a black image -
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
Since you also need the screen to be black while you are using other apps. Then you need to write a background service that receives the accelerometer values and when it reaches 8.1, you can use the above snippet of code in your service.
Related
I'm writing my first Android app. Essentially it shows and controls the score of a straight pool match. I've read articles about the lifecycle of an activity but I still have a problem I don't understand:
When I switch back to my app with a running game (so a score different from the initial 0:0 is shown) the activity sometimes loses its state and shows 0:0 instead of the score when I left the app. I overloaded the methods onSaveInstanceState and onRestoreInstanceState. The former get's called when I press the home button of my device. The latter never gets called. I've read that the method only gets called when onCreate is called. The onCreate method doesn't get called although the app needs longer than usual to reload after switching to some other apps in between. So I think the activity does get rebuild but obviously not by onCreate and the saved score is not loaded by onRestoreInstanceState.
Can you explain me what's happening and how to achieve the desired behaviour? Thank you very much!
edit: I was asked to post my onCreate() and onSaveInstanceState() methods. I tried to shorten them in a useful way. Please tell me If there is anything unclear or missing.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final GameSetting gameSetting = getGameSettings();
final GameData gameData = new GameData(gameSetting.getLeague());
game = new GameLogic(gameData);
scoreViews.put(PlayerId.PLAYER_A, (TextView) findViewById(R.id.playerAScore));
}
#Override
protected void onSaveInstanceState(#NonNull final Bundle outState) {
GameDataInstanceSupplier.saveInstance(game.getGameData(), outState);
// inside the method the data gets saved like
// outState.putInt(STATE_SCORE_A, data.getScore(PlayerId.PLAYER_A));
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(#NonNull final Bundle inState) {
super.onRestoreInstanceState(inState);
GameDataInstanceSupplier.restoreInstance(game.getGameData(), inState);
// inside the method the data gets loaded like
// data.setScore(PlayerId.PLAYER_A, inState.getInt(STATE_SCORE_A));
}
I put a "debug output" inside the onCreate() method and it did not appear in the debug log. The same was true for onRestoreInstanceState(). A message was printed inside onSaveInstanceState() when I pushed the home button.
I think your app is not the problem, you see, I came to find this page because I have the same problem switching aplications in my android phone, it's specially noticiable when usig 2 factor authentication and have to switch to text message, mail or other app to get the sent code, when switch back to the code input screen on the browser or app (teams for example) it is no longer there anymore and it has return to the begining, the login screen for example. This is very annoying because I have disable every thing that has to do with energy savings but still have the issue. I discovered that if I open first the message app and then do the authentication while switching very quickly just to see the code in 1 or 2 seconds and then return sometimes it works. It could be memory issue, or may be an unfulfilled politic on the energy savings. I think is probably the last because other explicitly stated properties like setting the screen brightness automatic adjust off and set to maximun works for a while and then returns to a default. It may even be a planned obsolescence. My phone is a Huawei P smart 2019 (POT-LX3) with android 10. I'll may be try a factory reset. Any suggestions are welcome.
As a beginner android developer I have faced with the problem concerning the fragments and activities handling when screen orientation is changed.
This is my situation:
I have two fragments (F_A and F_B). F_A contains ListView (LV), and F_B contains combination of several Views: Description of LV (lets call it as Desc).
When app runs in portrait mode first activity shows LV and after clicking on an item the second activity runs. In this case when the orientation of the screen is changed from portrait to landscape mode I want to change the layout to show both fragments as it is done when the app initially runs in landscape mode.
I hope the provided pieces of codes will not confuse my description of the situation more :)
first activity
main_activity.java file contains:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // there is two actitivy_main.xml files
// one in layout/ directory and the other one in layout-land/ directory.
...
}
#Override
public void respond (int index) {
// this method got the second fragment if it is visible and not null.
// otherwise starts new activity for only description fragment (F_B)
}
second activity (for portrait mode only)
DescriptionActivity.java file contains:
#Override
protected void onSaveInstanceState(Bundle outState) {
// saving some stuff for further handling when orientation will changed from port to land.
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_description); // This layout contains only F_B fragment
... //here some initialization is done for the description.
}
Considering that during the screen rotation the Life Cycle guard methods such as onPause(), onStart() etc. are called I think that I have to check the orientation in that methods then destroy the second activity and run the first: main_activity which will itself check what layout should be shown (in this case the layout from layout-land/ directory, I am not sure weather this is a good idea, so I think to ask for help to developers with more experience.
Many thanks in advance,
Arsen
The best way to achieve what you want is described in Fragments Android documentation, there you have an example with the full explanation.
But just as an advice, you don't have to use 2 diff activities nor check the orientation of the device, you just have to create an activity_main.xml for diff configurations in layout-port (portrait layout) and layout-land (landscape layout) you can learn how in here: Supporting Different Screens and Orientations. I know it's kind of hard at the beginning but working with fragments is one of the best practices for android. Read those tutorials and you'll be able to achieve your goal. Good luck :)
I have seen the following links before posting this question
http://www.devx.com/wireless/Article/40792/1954
Saving Android Activity state using Save Instance State
http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html
How to save state during orientation change in Android if the state is made of my classes?
I am not getting how should i override the following function :
#Override
public Object onRetainNonConfigurationInstance() {
return someExpensiveObject;
}
In my application i have layout with one editext visible and other editext get visible when the data of first editext validates to true.I have set the visbility of all other editextes and textviews to false and make them visible after validating.
So in my activity if the screen orientation is changed then all the items having android:visibility="false" get invisible.
I have also came to know that when our activities screen orientation changes it calls onStop() followed by onDestroy() and then again starts a fresh activity by calling onCreate()
This is the cause .. But i am not getting how to resolve it ..
Here You can see the screenshots of my application :
in this image all fields are loaded
and in another image when the screen orientation is changed to landscape they are all gone
Any link to tutorial or piece of code will be highly appreciable.
And also my application crashes when a progress dialog is shown up and i try to change screen orientation.How to handle this ??
Thanks
Well if you have the same layout for both screens then there is no need to do so just add below line in your manifest in Activity node
android:configChanges="keyboardHidden|orientation"
for Android 3.2 (API level 13) and newer:
android:configChanges="keyboardHidden|orientation|screenSize"
because the "screen size" also changes when the device switches between portrait and landscape orientation.
From documentation here: http://developer.android.com/guide/topics/manifest/activity-element.html
There is another possibility using which you can keep the state as it is even on Orientation change using the onConfigurationChanged(Configuration newConfig).
Called by the system when the device configuration changes while your activity is running. Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest. If any configuration change occurs that is not selected to be reported by that attribute, then instead of reporting it the system will stop and restart the activity (to have it launched with the new configuration).
At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.
There are 2 ways of doing this, the first one is in the AndroidManifest.xml file. You can add this to your activity's tag. This documentation will give you an in depth explanation, but put simply it uses these values and tells the activity not to restart when one of these values changes.
android:configChanges="keyboardHidden|orientation|screenSize|screenLayout"
And the second one is: overriding onSaveInstanceState and onRestoreInstanceState. This method requires some more effort, but arguably is better. onSaveInstanceState saves the values set (manually by the developer) from the activity before it's killed, and onRestoreInstanceState restores that information after onStart() Refer to the official documentation for a more in depth look. You don't have to implement onRestoreInstanceState, but that would involve sticking that code in onCreate().
In my sample code below, I am saving 2 int values, the current position of the spinner as well as a radio button.
#Override
public void onSaveInstanceState(#NonNull Bundle savedInstanceState) {
spinPosition = options.getSelectedItemPosition();
savedInstanceState.putInt(Constants.KEY, spinPosition);
savedInstanceState.putInt(Constants.KEY_RADIO, radioPosition);
super.onSaveInstanceState(savedInstanceState);
}
// And we restore those values with `getInt`, then we can pass those stored values into the spinner and radio button group, for example, to select the same values that we saved earlier.
#Override
public void onRestoreInstanceState(#NotNull Bundle savedInstanceState) {
spinPosition = savedInstanceState.getInt(Constants.KEY);
radioPosition = savedInstanceState.getInt(Constants.KEY_RADIO);
options.setSelection(spinPosition, true);
type.check(radioPosition);
super.onRestoreInstanceState(savedInstanceState);
}
I have weird situation with EditText.
Situation:
set device to landscape mode
enter text to EditText and press
"DONE"
change orientation
EditText contains doubled text that
was typed
the code:
private EditText enterText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
enterText = (EditText) findViewById(R.id.enterText);
}
If I comment the line with findViewById, everything is working properly.
Any ideas why this happens?
EDIT: I've double checked and it happens every time, not only if the line I was written about is commented.. :-\
EDIT2: It happens only on first orientation change event. Every next is OK..
Every time you rotate your phone the Activity restarts, which probably calls something that causes your text to be written again somehow. This is part of the OS and a bit annoying, here is that to do.
If you in your manifest add:
android:configChanges="orientation"
to the activity node in which the rotation is done, you'll have to override the onConfigurationChanged.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.myLayout);
}
Whenever your phone rotates the overriden method will call and you can do a special initialization like ignore the input if there was an input earlier and such. Hope this gets you on the right track!
PS: You can also pipeline the configChanges with different other "values/configs". Such as keyboardHidden and many others.
They are listed on the following link
I found bunch of samples how to remove title/notification bar on Android. However, it's still not a full screen.
I still see a panel on my table with search, home, back and some other button.
Do you have any ideas how to hide this panel?
Generally speaking, I want kind of kiosk mode.
Use this code in onCreate of activity:
getWindow().setFlags(LayoutParams.FLAG_FULLSCREEN, LayoutParams.FLAG_FULLSCREEN);
Sample code for a fullscreen activity:
public class HellofullActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
where you request that the window has not the green title bar and take full screen.
The core problem was device specific. I was doing development for Archos and Archos has it's own bar, which isn't not removed by FLAG_FULLSCREEN and FEATURE_NO_TITLE