Android finish() method is not closing the app but minimizing it

860 views Asked by At

I'm starting an activity from the background service by using following piece of code:

Intent intent = new Intent(getApplicationContext(),AlarmActivity.class);   
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
startActivity(intent);

But when I finish the activity by clicking the OK or Snooze button the app doesn't close but minimises instead.
When that minimised app is opened, the alarm starts ringing again until the app is closed manually.
I've tried by following commands as well but no gain.

android.os.Process.killProcess(android.os.Process.myPid()); 

 

System.exit(1);

What could be the issue?

ActivityCode:

import android.content.Context;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.PowerManager;
import android.os.Vibrator;
import android.support.annotation.RequiresApi;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;

import com.solutionz.battery.saver.master.alarm.utils.AdInterstitial;
import com.solutionz.battery.saver.master.alarm.utils.AppGlobal;
import com.solutionz.battery.saver.master.alarm.utils.MySharedPreferences;

public class AlarmActivity extends AppCompatActivity {
    Context context;
    TextView title_tv, message_tv;
    ImageView batteryIcon_iv;
    TextView ok_tv, snooze_tv;
    boolean isCharging;
    Ringtone ringtone;
    Vibrator vibrator;
    Handler handler;
    Runnable runnable;
    PowerManager.WakeLock screenLock;
    MySharedPreferences mySharedPreferences;
    AdInterstitial adInterstitial;

    long[] pattern = {1000, 500, 1000, 1000, 500};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        setContentView(R.layout.activity_alarm);
        mySharedPreferences = new MySharedPreferences(context);
        adInterstitial = new AdInterstitial(context);
        screenLock = ((PowerManager) getSystemService(POWER_SERVICE)).newWakeLock(
                PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
        screenLock.acquire();

        setScreenSize();
        setViews();
        setContent();
        setTimer();
        adInterstitial.requestLoadInterstitial(false);
    }

    private void setScreenSize() {
        // getting and setting the window size
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        // int width = (int) (size.x * 0.7);
        // int height = (int) (size.y * 0.4);

        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.gravity = Gravity.CENTER;
        //params.height = height;
        //params.width = width;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;

        this.getWindow().setAttributes(params);
    }

    private void setViews() {
        title_tv = (TextView) findViewById(R.id.title_tv);
        message_tv = (TextView) findViewById(R.id.message_tv);
        batteryIcon_iv = (ImageView) findViewById(R.id.batteryIcon_iv);
        ok_tv = (TextView) findViewById(R.id.ok_tv);
        snooze_tv = (TextView) findViewById(R.id.snooze_tv);

        scaleView(batteryIcon_iv, 0.7f, 1.0f);

        ok_tv.getCompoundDrawables()[1].mutate().setColorFilter(ContextCompat.getColor(context, R.color.white), PorterDuff.Mode.SRC_ATOP);
        snooze_tv.getCompoundDrawables()[1].mutate().setColorFilter(ContextCompat.getColor(context, R.color.white), PorterDuff.Mode.SRC_ATOP);

        ok_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mySharedPreferences.SetLastNotifiedChargingState(isCharging);
                mySharedPreferences.SetIsNotified(true);
                finish();
                //android.os.Process.killProcess(android.os.Process.myPid());
//                  System.exit(1);
            }
        });
        snooze_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mySharedPreferences.SetIsSnoozed(true);
                 finish();
//android.os.Process.killProcess(android.os.Process.myPid());
//                  System.exit(1);
                }
            }
        });
    }

    private void setContent() {
        Bundle bundle = getIntent().getBundleExtra("data");
        title_tv.setText(bundle.getString("title"));
        message_tv.setText(bundle.getString("message"));
        isCharging = bundle.getBoolean("isCharging");

        if (!isCharging) {
            batteryIcon_iv.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_battery_30));
            batteryIcon_iv.getDrawable().mutate().setColorFilter(ContextCompat.getColor(context, R.color.white), PorterDuff.Mode.SRC_ATOP);
        } else {
            batteryIcon_iv.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_battery_charging_80));
            batteryIcon_iv.getDrawable().mutate().setColorFilter(ContextCompat.getColor(context, R.color.white), PorterDuff.Mode.SRC_ATOP);
        }
        // playAlarmTone();
        mediaPlayerSetting();
    }

    public void scaleView(View v, float startScale, float endScale) {
        Animation anim = new ScaleAnimation(
                startScale, endScale, // Start and end values for the X axis scaling
                startScale, endScale, // Start and end values for the Y axis scaling
                Animation.RELATIVE_TO_SELF, 0.5f, // Pivot point of X scaling
                Animation.RELATIVE_TO_SELF, 0.5f); // Pivot point of Y scaling
        anim.setFillAfter(true); // Needed to keep the result of the animation
        anim.setDuration(1000);
        anim.setRepeatMode(Animation.REVERSE);
        anim.setRepeatCount(Animation.INFINITE);
        v.startAnimation(anim);
    }

    public void mediaPlayerSetting() {

        AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        ringtone = RingtoneManager.getRingtone(getApplicationContext(), notification);

        if (mySharedPreferences.GetIsSoundEnabled())   // Play sound
        {
            int volume = audioManager.getStreamVolume(AudioManager.STREAM_ALARM);
            if (volume == 0)
                volume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
            audioManager.setStreamVolume(AudioManager.STREAM_ALARM, volume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

            ringtone = RingtoneManager.getRingtone(getApplicationContext(), notification);
            if (ringtone != null) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ringtone.setAudioAttributes(new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_ALARM).build());
                } else {
                    ringtone.setStreamType(AudioManager.STREAM_ALARM);
                }
                ringtone.play();
            }

        }
        if (mySharedPreferences.GetIsVibrationEnabled()) // Start vibration
        {
            vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
            vibrator.vibrate(pattern, 0);
        }
    }

    private void setTimer() {
        handler = new Handler();
        runnable = new Runnable() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void run() {

                AppGlobal.showNotification(getApplicationContext(), title_tv.getText().toString(), message_tv.getText().toString());
                AlarmActivity.this.finish();
            }
        };
        handler.postDelayed(runnable, mySharedPreferences.GetAlarmDuration() * 1000);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (ringtone != null && ringtone.isPlaying()) ringtone.stop();
        if (vibrator != null) vibrator.cancel();
        handler.removeCallbacks(runnable);
        screenLock.release();
    }
}
1

There are 1 answers

1
Marcin Orlowski On

In Android finish() method is not closing the app but minizing it

No. First of all there's no concept of "minimizing" apps on Android, and finish() is NOT closing the main app process, but only finished the activity.