python中要怎么实现当程序要被关闭时触发一个事件例如写一个配置文件?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python中要怎么实现当程序要被关闭时触发一个事件例如写一个配置文件?相关的知识,希望对你有一定的参考价值。

开一个线程来处理写配置文件,当截获到关闭的消息时启动即可。追问

问题是怎么截获

参考技术A 析构方法__del__,也许可行。打个比方吧:
class A:
def __del__(self):
print('当实例化对象从内存中消失时实现的功能')

a1 = A()
del a1本回答被提问者采纳
参考技术B 当下平安财神节在平安公司旗下多个平台推出,大家都热情高涨踊跃参加,参与人数再创新高。作为平安金管家理财圈也适时推出晒财神节美好时刻天天赢108大包评论活动,时至今日参与评论已过7万条,大家晒出了中奖的喜悦,晒出了对平安未来发展的建议,也晒出了对平安的不离不弃,这说明大家对平安的共识是达成一致的,平安公司舍得拿出收益回馈大家,我们也懂得感恩,真心喜欢平安这个大舞台,祝愿平安公司旗下产品大卖,明天的今天把酒言欢! 参考技术C 左转 直直冲在风速140节V1 V2 185节 倾角40度向天空
离地100呎 马上急转向360 航线东海

当应用程序被杀死/关闭时,世博会通知不会触发方法

【中文标题】当应用程序被杀死/关闭时,世博会通知不会触发方法【英文标题】:Expo notification doesnt fire method when app is killed/closed 【发布时间】:2021-03-09 13:54:36 【问题描述】:

当应用程序正在运行或在后台但应用程序关闭时,我使用 react native expo 和推送通知工作正常。它不调用处理通知的方法。我需要重定向到详细信息页面。我尝试使用函数组件和类组件,尝试迁移到旧通知和新通知。

import React, useState, useEffect, useRef from 'react';
import 
    Image, ScrollView,
    StyleSheet, Text, TouchableOpacity, Platform,
    View, Linking,

 from 'react-native';
import * as Notifications from "expo-notifications";


const HomeScreen = (props) => 
  useEffect(() => 
        notificationListener.current = Notifications.addNotificationReceivedListener(notification => 
        
            const request, date = notification ||
            const content = request ||
            const data = content ||
            const annKey,type = data ||
           
            if(annKey) 
              //  navigation.navigate('Detail', annKey,  updateFeed: true, onSelect,)
            
         

        );


        responseListener.current = Notifications.addNotificationResponseReceivedListener(response => 
           

            const notification = response ||
            console.log(notification);
            const request, date = notification ||
            const content = request ||
            const data = content ||
            const annKey, type = data ||


            if(annKey)
               navigation.navigate('Detail', annKey,  updateFeed: true, onSelect,)
            

          
        );

        return () => 
            Notifications.removeNotificationSubscription(notificationListener);
            Notifications.removeNotificationSubscription(responseListener);
        ;
    , []);



export default HomeScreen;

【问题讨论】:

【参考方案1】:

问题是在应用程序完成初始化后调用useEffect() 太晚了。因此,在系统放弃通知之前不会添加侦听器,并且不会调用处理程序。

幸运的是,由于您使用的是新的 expo-notifications 库,它引入了 useLastNotificationResponse() React 钩子。它可以替换addNotificationResponseReceivedListener() 监听器并返回用户交互的最后一个通知(即点击)。它可以安全地用于useEffect() 钩子中。

您可以在此处找到文档:https://docs.expo.io/versions/latest/sdk/notifications/#uselastnotificationresponse-undefined--notificationresponse--null

这里是如何使用它(最好在你的根组件上实现它):

import * as Notifications from 'expo-notifications';

export default function App() 
  const lastNotificationResponse = Notifications.useLastNotificationResponse();
  
  React.useEffect(() => 
    if (
      lastNotificationResponse &&
      lastNotificationResponse.notification.request.content.data['someDataToCheck'] &&
      lastNotificationResponse.actionIdentifier === Notifications.DEFAULT_ACTION_IDENTIFIER
    ) 
      // navigate to your desired screen
    
  , [lastNotificationResponse]);

  return (
    /*
     * your app
     */
  );

【讨论】:

您能给我一个建议吗?我在 useEffect() 中使用 setWebViewUri() 钩子在我的 组件中打开新 URL。不幸的是,在点击通知后(当应用程序处于终止状态时),这会导致无限循环。我的包含 Webview 的主要应用程序组件尝试重新渲染很多次,并且用户得到一个白屏。如果应用程序处于前台/后台,一切正常。【参考方案2】:

您必须将其添加到您的 app.json 文件中:

    "android": 
      "useNextNotificationsApi": true,
    ,

【讨论】:

以上是关于python中要怎么实现当程序要被关闭时触发一个事件例如写一个配置文件?的主要内容,如果未能解决你的问题,请参考以下文章

Azure 逻辑应用程序:Azure DevOps“当工作项关闭时”触发器有时不会触发

当应用程序被杀死/关闭时,世博会通知不会触发方法

bootstrap的modal提示框怎么实现自动关闭

delphi中要把时间比如说2011年1月1日12时25秒转换成整型数怎么转换

页面关闭或刷新时触发javascript的事件

当 UIAlertController 处于活动状态时,VoiceOver Z 手势不会触发