-1

我開始從背景服務的活動,通過使用下面的代碼段:的Android光潔度()方法並不關閉所述應用程序,但最小化它

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

但是,當我通過點擊完成活動好的貪睡按鈕應用程序不關閉,但最小化。
當最小化的應用程序打開時,鬧鐘再次響起,直到手動關閉應用程序。
我已經嘗試了以下命令,但沒有收穫。

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

 

System.exit(1); 

可能是什麼問題?

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(); 
    } 
} 
+0

如果API> = 16,那麼你應該嘗試'finishAffinity();' – bshears

+0

請閱讀[this(how to ask)](http://stackoverflow.com/help/how-to-ask)和[this(mcve)](http://stackoverflow.com/help/mcve),因爲這些將幫助您從社區獲得更多更好的答案。這種情況發生的可能性太多了。活動不應該被手動銷燬(也不會調用'System.exti()')。顯示'onPause' /'onDestroy'方法 – Bonatti

+0

@bshears,試過了,沒有收穫。 –

回答

0

在Android中完成()方法不關閉應用程序,但minizing這一切的

號第一有沒有對Android的 「最小化」 的應用理念,並且finish()不是關閉了主應用進程,但只完成了活動。

+0

那我怎樣才能達到那種報警類型的行爲呢? ,當用戶打盹或取消時,它應該關閉。 –

相關問題