简体   繁体   English

完成后,应用将无法关闭

[英]App won't close after finish()

So, I'm trying to create an app which have a function like an alarm clock for Android phones, it will alert the user at their chosen time. 因此,我正在尝试创建一个具有类似于Android手机闹钟的功能的应用程序,它将在用户选择的时间提醒用户。 When the alert screen show up, the user has an option to push a notification or snooze, and the app will push a notification and then close. 当出现警报屏幕时,用户可以选择推送通知或暂停,然后该应用程序将推送通知,然后关闭。

However, when I click the snooze or the notification button, the app did not close. 但是,当我单击“暂停”或“通知”按钮时,该应用程序没有关闭。 Instead, the app stop the alarm sound and minimized (just as when the user touch the home button). 而是,该应用停止警报声音并最小化(就像用户触摸“主页”按钮时一样)。 If I touch the recent app, I then can open the alert screen again. 如果我触摸了最近使用的应用程序,则可以再次打开警报屏幕。

Edit: I know that there're some codes such as System.exit() or killProcess, since those code are not recommended, I prefer avoid using them. 编辑:我知道有一些代码,例如System.exit()或killProcess,因为不建议使用这些代码,所以我宁愿避免使用它们。 The reason I ask is because I tested the real clock app that come with my phone (4.3), and the it's alert screen will close after the I press the snooze or dismiss button. 我问的原因是因为我测试了手机随附的真实时钟应用程序(4.3),并且在我按下贪睡或关闭按钮后,它的警报屏幕将关闭。 So there must be a way for me to do the same, right ? 所以我必须有一种方法可以做到这一点,对吗?

Answer Okay, so as Sagar Pilkhwal explained below, and after reading others related problems, I found out that there's no "good" method to close you app by codes, you have to leave that option to the users or OS. 回答好的,正如下面的Sagar Pilkhwal所述,在阅读了其他相关问题之后,我发现没有一种“好的”方法可以通过代码关闭应用程序,您必须将该选项留给用户或OS。 Unless you want to use System.exit or killProcess, but they're bad ways to force your app to close. 除非您要使用System.exit或killProcess,否则它们是强制关闭应用程序的不好方法。

Sagar Pilkhwal also have a alternatively method to this problem, if you don't want your alert screen show up in recent app, you bring up another activity. Sagar Pilkhwal也有解决此问题的另一种方法,如果您不希望您的警报屏幕出现在最近的应用程序中,则会启动另一个活动。

However, as for this case and Alarm Manager in general, open MainActivity will lead to nasty stuffs, since MainActivity is when we handle Alarm events. 但是,对于这种情况和通常的Alarm Manager,打开MainActivity将导致令人讨厌的东西,因为MainActivity是我们处理Alarm事件的时间。 Then I found out another solution, if you don't want your alert screen show up in recent app, simply add 然后我找到了另一种解决方案,如果您不希望您的警报屏幕出现在最近的应用中,只需添加

android:excludeFromRecents="true"

to your Alert.class Activity in the Manifest, or add Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS to the intent used to start Alert.class. 到清单中的Alert.class活动中,或将Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS添加到用于启动Alert.class的意图中。

This will lead to other problem, as when the user press the Home button, the alarm won't turn off, and the user can't open recent app to access the alert screen (he'll has to open the app to do so). 这将导致其他问题,因为当用户按下“主页”按钮时,警报不会关闭,并且用户无法打开最近使用的应用程序以访问警报屏幕(他必须打开该应用程序才能这样做。 )。 I fix this problem by try to detect the home button pressed event. 我通过尝试检测“主页”按钮按下事件来解决此问题。 Detail answer are below. 详细答案如下。

Android's design does not favour exiting an application by choice, but rather manages it by the OS. Android的设计不支持选择退出应用程序,而是通过OS对其进行管理。 You can bring up the Home application by its corresponding Intent: 您可以通过相应的Intent调出Home应用程序:

public void quit() {
    Intent startMain = new Intent(Intent.ACTION_MAIN);
    startMain.addCategory(Intent.CATEGORY_HOME);
    startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(startMain);
}

not so recommended method to close application just call: 不建议使用这种方法关闭应用程序,只需调用:

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

or 要么

System.exit(0);

Otherwise due-to specific life-cycling of Android activities you can't be sure that application is closed/killed. 否则,由于Android活动的特定生命周期,您不能确保该应用程序已关闭/被杀死。

well im thinknig. 即时通讯thinknig。

System.exit(0);

maybe? 也许? im new to android thou..but i sometimes use it.. 我是新来的Android Thou ..但我有时会用..

Put the following to Manifest, in your Alert activity : 在您的Alert活动中,将以下内容放入清单:

android:excludeFromRecents="true" //this will make the Activity be exclude from recents list.

You can also add android:launchMode="singleInstance" and android:taskAffinity="" (if you know what they do) 您还可以添加android:launchMode="singleInstance"android:taskAffinity="" (如果您知道它们的作用)

In the Alert class, use this to detect home button pressed event: 在Alert类中,使用它来检测主页按钮按下事件:

@Override
public void onPause() {
    if (!isFinishing()) {
        createNotf(); //Handle home button press here.
    }
    super.onPause();
}

Use this to handle back button pressed: (You have to have this code if you want to detect home button press using isFinishing(); 使用它来处理后退按钮的按下:(如果要使用isFinishing()检测主按钮按下,则必须具有此代码;

@Override
public void onBackPressed() {
    //Handle BackButton event here.
}

For my case only, when the user press home button, I will create a notification, so he can either click on the notification or open the app again to access to Alert Screen.: 仅以我为例,当用户按下主页按钮时,我将创建一个通知,因此他可以单击该通知或再次打开该应用程序以访问警报屏幕。

private void createNotf() {
    Intent screenIntent = new Intent(MyAlert.this, MyAlert.class);
    screenIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    screenIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    PendingIntent pIntent = PendingIntent.getActivity(MyAlert.this, MainActivity.SEND_ALARM_CODE, screenIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    Notification.Builder ntfBuilder = new Notification.Builder(this)
    .setAutoCancel(true)
    .setContentTitle("ALARM_RUNNING")
    .setLargeIcon(bitmap)
    .setSmallIcon(R.drawable.ic_launcher)
    .setContentText("CLICK_TO_OPEN_ALERTSCREEN.")
    .setContentIntent(pIntent);

    NotificationManager myNotfM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);     
    myNotfM.notify(999, ntfBuilder.build());        
}

When the Alert stop, that notification will also be clear: 当警报停止时,该通知也将被清除:

@Override
public void onDestroy() {
    super.onDestroy();
    NotificationManager mntfM = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mntfM.cancel(999);}
System.exit(0);

That right there will close your application out leaving nothing running in the background.However,use this wisely and don't leave files open, database handles open, etc.These things would normally be cleaned up through the finish() command. 正确的位置将关闭您的应用程序,而不会在后台运行任何内容。但是,请明智地使用此操作,不要打开文件,打开数据库句柄等。通常可以通过finish()命令清除这些内容。

I personally HATE when I choose Exit in an application and it doesn't really exit. 当我在应用程序中选择“退出”时,我个人讨厌它并没有真正退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM