在改项目关于邮件附件的bug时看到了程序中关于onNewIntent与singleTask的特性,我们的程序关联了Office的打开,当我们在email中添加附件的时候,可以查看附件,由于在AndroidManifest.xml中的intent-filter做了关联,所以可以选择我们的程序打开
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".OnNewIntentActivity" 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" /> <action android:name="android.intent.action.EDIT" /> <category android:name="android.intent.category.DEFAULT" /> <data android:mimeType="application/ms-word" /> <data android:mimeType="application/msword" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.document" /> <data android:mimeType="application/vnd.openxmlformats-officedocument.wordprocessingml.template" /> <data android:mimeType="application/vnd.ms-word.document.macroEnabled.12" /> <data android:mimeType="application/vnd.ms-word.template.macroEnabled.12" /> </intent-filter> </activity> </application>
OnNewIntentActivity.java:
public class OnNewIntentActivity extends Activity{ private static final String TAG="OnNewIntentActivity"; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.i(TAG, "onCreate"); } @Override protected void onStart() { // TODO Auto-generated method stub super.onStart(); Log.i(TAG, "onStart"); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); Log.i(TAG, "onResume"); } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); Log.i(TAG, "onPause"); } @Override protected void onStop() { // TODO Auto-generated method stub super.onStop(); Log.i(TAG, "onStop"); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); Log.i(TAG, "onDestroy"); } @Override protected void onRestart() { // TODO Auto-generated method stub super.onRestart(); Log.i(TAG, "onRestart"); } @Override protected void onNewIntent(Intent intent) { // TODO Auto-generated method stub super.onNewIntent(intent); Log.i(TAG, "onNewIntent"); Intent intent2=getIntent(); } }
当我在程序中已经打开了,然后通过添加的附件再次查看的时候:
当在加上singleTask时:
<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 the FLAG_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总是只有一个,无论你启动它多少次
应当注意一下的情况:
@Override protected void onNewIntent(Intent intent) { // TODO Auto-generated method stub super.onNewIntent(intent); Log.i(TAG, "onNewIntent"); Intent intent2=getIntent(); System.out.println(intent2); System.out.println(intent2.getData()); }
intent2打印出来是: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.gao.test/.OnNewIntentActivity }
Uri打印出来是:null