背景一?
今天調(diào)試apk 發(fā)現(xiàn)發(fā)送多個(gè)文件,資源管理器會(huì)閃退
查看 log
01-19 02:11:25.328 1777 1777 E AndroidRuntime: FATAL EXCEPTION: main
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: Process: com.android.rk, PID: 1777
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity? context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at android.app.ContextImpl.startActivity(ContextImpl.java:912)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at android.app.ContextImpl.startActivity(ContextImpl.java:888)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at android.content.ContextWrapper.startActivity(ContextWrapper.java:379)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at com.android.rk.ShareIntent.shareMultFile(ShareIntent.java:47)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at com.android.rk.RockExplorer.shareFiles(RockExplorer.java:1589)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at com.android.rk.RockExplorer.access$1700(RockExplorer.java:89)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at com.android.rk.RockExplorer$15.onClick(RockExplorer.java:1545)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at android.view.View.performClick(View.java:6597)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at android.view.View.performClickInternal(View.java:6574)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at android.view.View.access$3100(View.java:778)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at android.view.View$PerformClick.run(View.java:25885)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:873)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at android.os.Looper.loop(Looper.java:193)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6718)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
01-19 02:11:25.324? 479? 1197 W ActivityManager:? Force finishing activity com.android.rk/.RockExplorer
01-19 02:11:25.321? 479? 1197 D PowerManagerService: acquireWakeLockInternal: lock=217838472, flags=0x1, tag="*launch*", ws=WorkSource{1000}, uid=1000, pid=479
01-19 02:11:25.321? 479? 1197 D PowerManagerService: updateWakeLockSummaryLocked: mWakefulness=Awake, mWakeLockSummary=0x1
01-19 02:11:25.321? 479? 1197 D PowerManagerService: updateUserActivitySummaryLocked: mWakefulness=Awake, mUserActivitySummary=0x1, nextTimeout=2147593254 (in 2147476552 ms)
01-19 02:11:25.320? 479? 1197 D PowerManagerService: updateDisplayPowerStateLocked: mDisplayReady=true, policy=3, mWakefulness=1, mWakeLockSummary=0x1, mUserActivitySummary=0x1, mBootCompleted=true, screenBrightnessOverride=-1, useAutoBrightness=false, mScreenBrightnessBoostInProgress=false, mIsVrModeEnabled= false, sQuiescent=false
01-19 02:11:25.320? 479? 1197 D PowerManagerService: Acquiring suspend blocker "PowerManagerService.WakeLocks".
二 拆座、問題定位
????從log中看主巍,01-19 02:11:25.328? 1777? 1777 E AndroidRuntime: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity? context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
? ? ?發(fā)現(xiàn),Context中有一個(gè)startActivity方法挪凑,Activity繼承自Context孕索,重載了startActivity方法。如果使用Activity的startActivity方法岖赋,不會(huì)有任何限制,而如果使用Context的startActivity方法的話瓮孙,就需要開啟一個(gè)新的的task唐断,遇到這個(gè)異常,是因?yàn)槭褂昧薈ontext的startActivity方法杭抠。解決辦法是脸甘,加一個(gè)flag。
代碼:
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
這樣就可以在新的task里面啟動(dòng)這個(gè)Activity了偏灿。
加了之后發(fā)現(xiàn)還是不行丹诀,
繼續(xù)看代碼
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
? ? ? ? intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ? intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
? ? ? ? intent.setType(mFileType);
? ? ? ? mcontext.startActivity(Intent.createChooser(intent, mcontext.getString(R.string.edit_bluetooth)));
這塊邏輯需要完善一下。
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
? ? ? ? intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ? intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
? ? ? ? intent.setType(mFileType);
? ? ? - ?//mcontext.startActivity(Intent.createChooser(intent, mcontext.getString(R.string.edit_bluetooth)));
? ? ? + ?Intent sendIntent = Intent.createChooser(intent, mcontext.getString(R.string.edit_bluetooth));
? ? ? ?+ if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O_MR1) {
? ? ? ? + ? ?sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
? ? ? ?+ }
? ? ? ?+ mcontext.startActivity(sendIntent);
三翁垂、搞定铆遭。