Avoiding run my android app in background - android

I want to force exit my android app when it's going to background.So i use this code in my base Activity class.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_HOME) {
finish();
System.exit(0);
}
return super.onKeyDown(keyCode, event);
}
But it's not working.Please help for resolving this.

You need to do like this,
#Override
public void onStop(){
super.onStop();
finish();
}
This will override your Onstop method

Related

How to pause media player when you click button back home or recent apps to exit app?

I am trying to pause media player when I click home button or recent apps to exit app but that's not happening.
Below is my code for the same:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK)
{
MainActivity.mediaPlayer.pause();
}
else if(keyCode==KeyEvent.KEYCODE_HOME )
{
MainActivity.mediaPlayer.pause();
}
else if(keyCode==KeyEvent.KEYCODE_ALL_APPS) {
MainActivity.mediaPlayer.pause();
}
else if(keyCode == KeyEvent.KEYCODE_APP_SWITCH)
{
MainActivity.mediaPlayer.pause();
}
return super.onKeyDown(keyCode, event);
}
This will help
onPause()
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.
Followed by either onResume() if the activity returns back to the front, or onStop() if it becomes invisible to the user.
onResume()
Called when the activity will start interacting with the user. At this point your activity is at the top of the activity stack, with user input going to it.
Always followed by onPause().
#Override
protected void onPause() {
super.onPause();
if(mediaPlayer!=null)
{
//pause
mediaPlayer.pause();
}
}
//resume activity
#Override
protected void onResume() {
super.onResume();
if(mediaPlayer!=null)
{
//resume
mediaPlayer.start();
}
}
Just override onPause() and onStop()
#Override
protected void onPause() {
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}
#Override
protected void onStop() {
if (mediaPlayer != null) {
mediaPlayer.pause();
}
}

Cancelling a CountDownTimer on Physical Back Button Press in a Fragment

I am using a CountDownTimer in a Fragment and trying to stop it if the user hit the physical back button in the phone. I have tried overriding onPause, onDestroy, onStop, onDestroyView but nothing seems to be working. Kind of lost here. Can some one give me a pointer here?
public class Foo extends Fragment {
CountDownTimer myTimer;
#Override
public void onStop() {
super.onStop();
myTimer.cancel();
}
#Override
public void onPause() {
super.onPause();
myTimer.cancel();
}
#Override
public void onDestroyView() {
super.onDestroyView();
myTimer.cancel();
}
#Override
public void onDestroy() {
super.onDestroy();
myTimer.cancel();
}
#OnClick(R.id.btn_greenleft_no)
public void goBack() {
myTimer.cancel();
Objects.requireNonNull(getActivity()).onBackPressed();
}
#OnClick(R.id.btn_greenright_yes)
public void showSuccess(View view) {
markAll();
myTimer.cancel();
(new MusicPlayer()).playSound(getContext(), "cheers.mp3");
final Snackbar snackbar = Snackbar.make(snackBarView, R.string.congratulations, Snackbar.LENGTH_SHORT);
snackbar.show();
myTimer.cancel();
}
private void startTimer(final View view) {
int Seconds = 5;
myTimer = new CountDownTimer(Seconds * 1000 + 1000, 1000) {
public void onTick(long millisUntilFinished) {
String rem = String.valueOf(millisUntilFinished / 1000);
Log.d("APP_DEBUG", "Timer: " + rem);
}
public void onFinish() {
goBack();
}
}.start();
}
}
Here is my 2 cents. Fragment doesn't have an onBackPressed() method which is present in the Activity class. It gets called when physical back button is pressed by the user. Here is what docs says:
Called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.
What you can do is override the onBackPressed() method in the parent activity of your Foo fragment, then using an interface communicate to the fragment that back button was pressed by the user. Inside the fragment you can have the desired code to cancel the timer. This answer in the question How to implement onBackPressed() in Fragments? can help with sample code.
Try to modify onBackPressed in Your fragment's parent activity.
#Override
public void onBackPressed() {
// I assume this is the way how You add fragment to fragment manager
//getSupportFragmentManager().beginTransaction().replace(android.R.id.content, Foo.getInstance(), Foo.TAG).commit()
// Find fragment by its string TAG and when You get it, call to stop countDownTimer
Foo foo = (Foo) getSupportFragmentManager().findFragmentByTag(Foo.TAG);
if (foo != null) {
foo.stopCountDownTimer();
}
super.onBackPressed();
}
Next step is to declare in Your Foo fragment two things:
public static final String TAG = "Foo";
and
public void stopCountDownTimer() {
myTimer.cancel();
}
For fragment you cant use onBackPressed method, Instead please use this code
#Override
public void onResume() {
super.onResume();
if (getView() == null) {
return;
}
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
myTimer.cancel();
return false;
}
return true;
}
});
}
You have to call countDownTime.finish() as well. I have used it and it works for me.
#Override
public void onDetach()
{
super.onDetach();
if (countDownTimer != null)
{
countDownTimer.cancel();
countDownTimer.onFinish();
}
}
have you tried onBackPressed()
#Override
public void onBackPressed() {
super.onBackPressed();
myTimer.cancel();
myTimer =null;
}
Have you tried adding an OnKeyListener in your fragment like this:
Here "view" is the parent layout of your fragment , the one that hosts the timer.
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
Log.i(tag, "keyCode: " + keyCode);
if( keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
myTimer.cancel();
return true;
}
return false;
}
});

Accessibility Service - Disable home button action android

I want to disable home button action using the accessibility service. I already read the docs from https://developer.android.com/training/accessibility/service
but didn't understand how to do it? Can anyone help me out in this
Create your own Accessibility Service using any tutorial and put this code inside it....
public class button_lock extends AccessibilityService {
#Override
public void onAccessibilityEvent(AccessibilityEvent accessibilityEvent) {
}
#Override
public void onInterrupt() {
}
#Override
protected boolean onKeyEvent(KeyEvent event) {
if(event.getKeyCode()==KeyEvent.KEYCODE_HOME)
return true;
else
return super.onKeyEvent(event);
}}
Then enjoy....
Override the below method in your Activity,
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
And now handle the key event like this,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
Log.i("Home Button","Clicked");
}
if(keyCode==KeyEvent.KEYCODE_BACK)
{
finish();
}
return false;
}
You can Disable Home Key by adding this code in your activity:
#Override
public void onAttachedToWindow() {
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
super.onAttachedToWindow();
}
However, this doesn't work while dialog is showing. To get rid of this problem simply add my code to your Dialog object:
Disable home Key while Dialog showing

how can I stop activity wdeviceclick back button on my device

Answered and trusted look comments !
Thank sania
public class Test extends Activity {
HTML5WebView mWebView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebView = new HTML5WebView(this);
Intent i = getIntent();
String url = i.getStringExtra("url"); // retrieve the value you passed
if (savedInstanceState != null) {
mWebView.restoreState(savedInstanceState);
} else {
mWebView.loadUrl(url); // use that value
}
setContentView(mWebView.getLayout());
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
#Override
public void onStop() {
super.onStop();
mWebView.stopLoading();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
}
return super.onKeyDown(keyCode, event);
}
public void onBackPressed() {
Intent myIntent = new Intent(Test.this, fragment_main.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// clear back stack
startActivity(myIntent);
finish();
return;
}
}
how can I stop last activity when clicked back button ? I was made a vimeo app working with listview how can I stop and close (vimeo videoplayer.class) that activity.when I click back button its going first activity or showing message Application was stopped :/
Add ondestroy () or video.stop() methods in backkey method

Sound is still playing in Background HTML 5 Video

I am playing HTML 5 Video in activity that is working fine, but when i press back button get exit from the activity and slide(Video contains by slides) but sound is still playing in background of that particular slide until it gets finish. How can i stop that sound while pressing the back button get exit completely form the activity.
here is my code when i press back button:
#Override
protected void onPause() {
super.onPause();
mWebView.stopLoading();
}
#Override
public void onStop() {
super.onStop();
mWebView.stopLoading();
}
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (mWebView.inCustomView()) {
mWebView.hideCustomView();
mWebView.stopLoading();
return true;
}
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed() {
if (mWebView.isFocused() && mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
activity.finish();
}
}
}
Put below line in your onStop() method:
mWebView.destroy();
Try mWebView.destroy() or maybe load some dummy URL.
Like:
#Override
public void onBackPressed() {
if (mWebView.isFocused() && mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
activity.finish();
mWebView.destroy();
}
}

Categories

Resources