onNewIntent与singleTask

在改项目关于邮件附件的bug时看到了程序中关于onNewIntent与singleTask的特性,我们的程序关联了Office的打开,当我们在email中添加附件的时候,可以查看附件,由于在AndroidManifest.xml中的intent-filter做了关联,所以可以选择我们的程序打开

  1. <application android:icon="@drawable/icon" android:label="@string/app_name">  
  2.         <activity android:name=".OnNewIntentActivity" android:label="@string/app_name">  
  3.             <intent-filter>  
  4.                 <action android:name="android.intent.action.MAIN" />  
  5.                 <category android:name="android.intent.category.LAUNCHER" />  
  6.             </intent-filter>  
  7.             <intent-filter>  
  8.                 <action android:name="android.intent.action.VIEW" />  
  9.                 <action android:name="android.intent.action.EDIT" />  
  10.                 <category android:name="android.intent.category.DEFAULT" />  
  11.                 <data android:mimeType="application/ms-word" />  
  12.                 <data android:mimeType="application/msword" />  
  13.                 <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" />  
  14.                 <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.template" />  
  15.                 <data android:mimeType="application/vnd.ms-word.document.macroEnabled.12" />  
  16.                 <data android:mimeType="application/vnd.ms-word.template.macroEnabled.12" />  
  17.             </intent-filter>  
  18.         </activity>  
  19.     </application>  
 

OnNewIntentActivity.java:

  1. public class OnNewIntentActivity extends Activity{  
  2.     private static final String TAG="OnNewIntentActivity";  
  3.       
  4.     @Override  
  5.     protected void onCreate(Bundle savedInstanceState) {  
  6.         // TODO Auto-generated method stub  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.         Log.i(TAG, "onCreate");  
  10.     }  
  11.       
  12.     @Override  
  13.     protected void onStart() {  
  14.         // TODO Auto-generated method stub  
  15.         super.onStart();  
  16.         Log.i(TAG, "onStart");  
  17.     }  
  18.       
  19.     @Override  
  20.     protected void onResume() {  
  21.         // TODO Auto-generated method stub  
  22.         super.onResume();  
  23.         Log.i(TAG, "onResume");  
  24.     }  
  25.       
  26.     @Override  
  27.     protected void onPause() {  
  28.         // TODO Auto-generated method stub  
  29.         super.onPause();  
  30.         Log.i(TAG, "onPause");  
  31.     }  
  32.       
  33.     @Override  
  34.     protected void onStop() {  
  35.         // TODO Auto-generated method stub  
  36.         super.onStop();  
  37.         Log.i(TAG, "onStop");  
  38.     }  
  39.       
  40.     @Override  
  41.     protected void onDestroy() {  
  42.         // TODO Auto-generated method stub  
  43.         super.onDestroy();  
  44.         Log.i(TAG, "onDestroy");  
  45.     }  
  46.       
  47.     @Override  
  48.     protected void onRestart() {  
  49.         // TODO Auto-generated method stub  
  50.         super.onRestart();  
  51.         Log.i(TAG, "onRestart");  
  52.     }  
  53.       
  54.     @Override  
  55.     protected void onNewIntent(Intent intent) {  
  56.         // TODO Auto-generated method stub  
  57.         super.onNewIntent(intent);  
  58.         Log.i(TAG, "onNewIntent");  
  59.         Intent intent2=getIntent();  
  60.     }  
  61. }  
 

当我在程序中已经打开了,然后通过添加的附件再次查看的时候:

当在加上singleTask时:

  1. <activity android:name=".OnNewIntentActivity"  android:launchMode="singleTask" android:label="@string/app_name">  
 

程序的执行结果是:

protected void onNewIntent (Intent intent)

 

This is called for activities that set launchMode to "singleTop" in their package, or if a client used theFLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

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

应当注意一下的情况:

  1. @Override  
  2. protected void onNewIntent(Intent intent) {  
  3.     // TODO Auto-generated method stub  
  4.     super.onNewIntent(intent);  
  5.     Log.i(TAG, "onNewIntent");  
  6.     Intent intent2=getIntent();  
  7.     System.out.println(intent2);  
  8.     System.out.println(intent2.getData());  
  9. }  
 

intent2打印出来是: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.gao.test/.OnNewIntentActivity }

Uri打印出来是:null

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值