Lockscreen Lifecycle Question - Java for Android App Development

Hey!
Is it normal that when the Lockscreen is toggled by the Power button, that onResume is called immediately after onPause?
Because I want to stop the Background music during the lock screen and at the moment it only pauses with home button because I resume it in onResume...
Thx for your help!

I think you can capture the keydown event to decide this.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME)//and others KEYCODE_POWER ...
{
//pause your job
}
}

Related

[Q] App losing focus

I decided to fix up some crummy UI elements in an app that I'm writing (and self-learning to code in the process) and a focus issue has arisen.
Previously I had a menu option appear when my phones menu button was pressed which would either start a request to turn on bluetooth if it was not on, or start a request to turn on bluetooth visibility for 180 seconds if bluetooth was already on. Essentially, if bluetooth was not on, you would have to use the menu button option twice in order to turn it on AND turn BT visibility on.
I've modified my static menu into a dynamic menu so that the menu option text differs, depending on whether BT is on or not. It still requires you to access the menu option twice to turn BT and visibility on (the thread isn't about that, but if anyone knows how to turn this into one step, instead of two, I'm all ears )
This issue is that if BT is on, and the menu option asks you to turn on visibility, choosing this option minimises my app (its still running but the homescreen has focus). The actual request to turn visibility on DOES work, but I'm not sure why my app is losing focus. Is there any way to ensure it doesn't lose focus, or to restore focus once the user has chosen to allow or disallow making their device visible?
My menu display code is:
Code:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
// Check if bluetooth is available
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(PS3mote.this, "Device does not support bluetooth. App is unusable.", Toast.LENGTH_LONG).show();
}
else
// Check if bluetooth is on. If not, show bluetooth option in menu
if (!mBluetoothAdapter.isEnabled()) {
menu
.findItem(R.id.visible)
.setVisible(false)
.setEnabled(false);
menu
.findItem(R.id.bton)
.setVisible(true)
.setEnabled(true);
}
// If bluetooth is on, show visibility option in menu
else {
menu
.findItem(R.id.visible)
.setVisible(true)
.setEnabled(true);
menu
.findItem(R.id.bton)
.setVisible(false)
.setEnabled(false);
}
return true;
}
My menu selection code is:
Code:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.bton:
bton();
return true;
case R.id.visible:
visible();
case R.id.quit:
quit();
return true;
case R.id.about:
about();
return true;
case R.id.help:
help();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and my BT and visibility request codes are:
Code:
private void bton() {
// Check if bluetooth is available
if (mBluetoothAdapter == null) {
// Device does not support Bluetooth
Toast.makeText(PS3mote.this, "Device does not support bluetooth. App is unusable.", Toast.LENGTH_LONG).show();
}
else {
// Request to turn on bluetooth
Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
}
private void visible() {
// Check if bluetooth is visible. If not visible, then request visibility for 180 seconds
if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 180);
startActivity(discoverableIntent);
}
}
Thanks in advance

[Q] programming back button

How do you add code to get input fro the back button (i.e. if back button is pressed... then do this)
You should press the "thanks" button if want continued help
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
...if ( keyCode == KeyEvent.KEYCODE_BACK) {
......//Do something
......return true;
...}
...return super.onKeyDown(keyCode, event);
}
do you mean for back button on a keyboard or the backbutton soft key on most android devices? If its the latter you would override the onBackPresed() function.
Code:
@Override
public void onBackPressed()
{
//bla
super.onBackPressed();
}

[Q] Back Key behavior can be modified?

So i have a galaxy s4. i had a idea, about pausing the process when leaving an app when hittin back button instead of destroying it.
when we open any app , bbm, facebook. games, twitter por example. and when we go out from it by pressing continously the back key, when we try to open the app again it will take more time than if we tap the home button.
basically home button tells android to pause the process and backbutton to close it which require more time when reloading the same app again.
i beleave this is not a remmaping thing because the back button will still going steps back in the apps, so, when there is no more steps to go back then it will pause it (not close it) ; home button will remain the same, pausing all apps to go home screen. and back button will pause only the one we are on (instead of closing it)
i repeat a lot the same thing because i want to be very clear. dont take it bad please.
Is this possible to make? may any of you give me some advices to do it?
You can change back button behavior by overriding this method
public void onBackPressed(){
Do here}
Sent from my GT-S5570 using XDA Premium 4 mobile app
But only for your own app, this doesn't work globally
---------------------------------
Phone : Nexus 4
OS:
Pure KitKat 4.4.2 stock, no root, no mods
---------------------------------
4d 61 73 72 65 70 75 73 20 66 74 77
Gesendet von Tapatalk
Also overrides for the back keys won't work with fragments without some very hacky code, this is how I managed to do it:
NewsActivity.java
Code:
public class NewsActivity extends SherlockFragmentActivity {
private final static String TAG_FRAGMENT = "TAG_FRAGMENT";
private static final int CONTENT_VIEW_ID = 666;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frame = new FrameLayout(this);
frame.setId(CONTENT_VIEW_ID);
setContentView(frame, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
if (savedInstanceState == null) {
setInitialFragment();
}
}
private void setInitialFragment() {
final NewsFragment fragment = new NewsFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(CONTENT_VIEW_ID, fragment, TAG_FRAGMENT).commit();
}
@Override
public void onBackPressed() {
final NewsFragment fragment = (NewsFragment) getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
if (fragment.onBackPressed()) {
super.onBackPressed();
}
}
}
NewsFragment.java - only the bit needed for getting the back press code, the layout I'm using is the Navigation drawer, the code captures the back key press and opens the drawer on back key press if its closed (which is the opposite way of doing it from the default way but its how I need it for the app
Code:
public boolean onBackPressed() {
if (mDrawerLayout.isDrawerOpen(newsListView)) {
return true;
} else {
mDrawerLayout.openDrawer(newsListView);
return false;
}
}

CountDownTimer problem

Hello! I'm new to this forum. I'm developing an Android app, I have some problems with the functionality of a timer and I don't figure out why. I would need some ideas.
In my application I have 2 activities: one is with the levels of a game, where you can chose between them and the second one is with the game itself. In the second activity I have a CountDownTimer which tells me when the game must finish. I have a progressBar assigned to that timer.
CountDownTimer mCountDownTimer; -global variable
In onCreate I have:
mProgressBar=(ProgressBar)findViewById(R.id.progressBar);
mProgressBar.setProgress(0);
mCountDownTimer=new CountDownTimer(90000,1000) {
int i=0;
@override
public void onTick(long millisUntilFinished) {
Log.v("Log_tag", "Tick of Progress" + i + millisUntilFinished);
i++;
mProgressBar.setProgress(i); }
@override
public void onFinish() {
i++;
mProgressBar.setProgress(i);
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
}
};
mCountDownTimer.start();
I have also overriden the native back button from Android to go to the first activity, the one with the levels and there I try to stop the counter, but it doesn't seem to work at all. The counter doesn't stop, and the other functions don't work as well.
Here is the code:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) { //Back key pressed
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
mCountDownTimer.cancel();
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed(){
mCountDownTimer.cancel();
Intent in = new Intent(getApplicationContext(), MyActivity2.class);
startActivity(in);
return;
How could I solve this? Thank you so much!
Here is your problem:
CountDownTimer mCountDownTimer; -global variable
There are no global variables in Java, only instance variables. Since you didn't post your full code I'm going to make a few assumptions here...
public class myAwesomeTimerClass {
CountDownTimer mCountDownTimer;
// bla bla
}
Then in your second activity/class, you should refer to the class:
instead of:
mCountDownTimer.cancel();
try:
myAwesomeTimerClass.mCountDownTimer.cancel();
Good luck.

Lock App

i am trying to make a lockscreen replacement app. I want my app to diplay instead of android lockscreen .
I 've done some search and made it work fine , but when home button is pressed my app exits. I've read that it is not possible to ignore home button on android >4.0.1
But apps on playstore don't have this problem. There must be a way not to exit on home button pressed
TakisBeskos said:
i am trying to make a lockscreen replacement app. I want my app to diplay instead of android lockscreen .
I 've done some search and made it work fine , but when home button is pressed my app exits. I've read that it is not possible to ignore home button on android >4.0.1
But apps on playstore don't have this problem. There must be a way not to exit on home button pressed
Click to expand...
Click to collapse
Try this I am forget after it
@override
public void onPause() {
super.onPause();
return;
}
@override
public void onBackPressed() {
super.onBackPressed();
return;
}
@override
public void onStop() {
super.onStop();
return;
}
@override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return keyCode == KeyEvent.KEYCODE_HOME
|| keyCode == KeyEvent.KEYCODE_BACK;
}
Sent from my SM-G530H using XDA Free mobile app
AndroidFire said:
Try this I am forget after it
@override
public void onPause() {
super.onPause();
return;
}
@override
public void onBackPressed() {
super.onBackPressed();
return;
}
@override
public void onStop() {
super.onStop();
return;
}
@override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return keyCode == KeyEvent.KEYCODE_HOME
|| keyCode == KeyEvent.KEYCODE_BACK;
}
Sent from my SM-G530H using XDA Free mobile app
Click to expand...
Click to collapse
it didn't work Any other ideas ?
AndroidFire said:
Try this I am forget after it
@override
public void onPause() {
super.onPause();
return;
}
@override
public void onBackPressed() {
super.onBackPressed();
return;
}
@override
public void onStop() {
super.onStop();
return;
}
@override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return keyCode == KeyEvent.KEYCODE_HOME
|| keyCode == KeyEvent.KEYCODE_BACK;
}
Sent from my SM-G530H using XDA Free mobile app
Click to expand...
Click to collapse
You don't need return statements on void methods. By definition void doesn't return anything - your IDE should have already notified you of that.
Edit: Do you even know what the code you wrote does? Why on earth are you overriding all those super methods then just calling the super method from within the overridden method - your code makes absolutely no sense whatsoever.
i am searching a few weeks and i didn't find a thing.
The only idea i have is to make my lockscreen app a launcher , so home button will not exit the locksceen, and when user unlocks it , to open an installed launcher.
but how can i get installed launchers ?
Jonny said:
You don't need return statements on void methods. By definition void doesn't return anything - your IDE should have already notified you of that.
Edit: Do you even know what the code you wrote does? Why on earth are you overriding all those super methods then just calling the super method from within the overridden method - your code makes absolutely no sense whatsoever.
Click to expand...
Click to collapse
can't reconsize it through phone
Sent from my SM-G530H using XDA Free mobile app
TakisBeskos said:
i am searching a few weeks and i didn't find a thing.
The only idea i have is to make my lockscreen app a launcher , so home button will not exit the locksceen, and when user unlocks it , to open an installed launcher.
but how can i get installed launchers ?
Click to expand...
Click to collapse
Try looking here for disabling home button - I'm not sure if it works but might be worth a try:
http://stackoverflow.com/questions/21983462/creating-custom-lockscreen-in-android/28603790#28603790
As for getting a list of installed launchers try the below link:
http://stackoverflow.com/questions/6175821/get-a-list-of-every-launcher-in-android
Jonny said:
Try looking here for disabling home button - I'm not sure if it works but might be worth a try:
http://stackoverflow.com/questions/21983462/creating-custom-lockscreen-in-android/28603790#28603790
As for getting a list of installed launchers try the below link:
http://stackoverflow.com/questions/6175821/get-a-list-of-every-launcher-in-android
Click to expand...
Click to collapse
nope. it didn't work

Categories

Resources