zoukankan      html  css  js  c++  java
  • Android 监听home键(android:launchMode="singleTask" 与 onNewIntent(Intent intent) 的用法

    android:launchMode="singleTask" 和 onNewIntent(Intent intent)两个特性,现总结一下经验:

    android:launchMode="singleTask" 配置在 Mainifest 中,它保证了栈中此Activity总是只有一个,无论你启动它多少次;

    onNewIntent(Intent intent) 是Override Activity的父类方法,只有仅在点Home键退出Activity而再次启动新的Intent进来才被调用到;

    它们两结合使用,可以做到监听home键(仅当发起新的Intent)。

    代码如下:

    Manifest.xml

    < activity   android:name = ".OnNewIntentDemo"    
               android:launchMode = "singleTask"    
                     android:label = "@string/app_name" >    
               < intent-filter >    
                   < action   android:name = "android.intent.action.MAIN"   />    
                   < category   android:name = "android.intent.category.LAUNCHER"   />    
               </ intent-filter >    
               < intent-filter >    
                   < action   android:name = "android.intent.action.VIEW"   />    
                    < category   android:name = "android.intent.category.DEFAULT"   />    
                    < data   android:mimeType = "video/*"   />    
                </ intent-filter >    
    </ activity >    
    

    Activity中代码

    @Override    
    protected   void  onNewIntent(Intent intent) {   
     if (DEBUG) Log.i(TAG,  "onNewIntent ~~~~~~~ intent = " +intent);   
     super .onNewIntent(intent);   
    }  
    
  • 相关阅读:
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/error404/p/2126576.html
Copyright © 2011-2022 走看看