发送多个文件,资源管理器会闪退

背景一 


今天调试apk 发现发送多个文件,资源管理器会闪退

查看 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?

     发现,Context中有一个startActivity方法,Activity继承自Context,重载了startActivity方法。如果使用Activity的startActivity方法,不会有任何限制,而如果使用Context的startActivity方法的話,就需要开启一个新的的task,遇到这个异常,是因为使用了Context的startActivity方法。解决办法是,加一个flag。

代码:

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );

这样就可以在新的task里面启动这个Activity了。

加了之后发现还是不行,

继续看代码

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);


三、搞定。


推荐阅读更多精彩内容

  • 本文重点介绍应用程序的启动过程,应用程序的启动过程实际上就是应用程序中的默认Activity的启动过程,本文将详细...
    天宇sonny阅读 169评论 1 0
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 1,551评论 0 3
  • 看到不说到,中国人哲理
    酒业大男孩阅读 39评论 0 0
  • 我何须向太阳走去,它的光茫自会到来,黑夜有点漫长,不过没关系。 ——题记 梦不在远方,...
    海云彼乐阅读 1,442评论 23 64
  • 且说陈菱与陈芰来到正房的时候,就见母亲旁边有一位少妇在伺候着。 陈夫人见他们二人过来,还没有等二人说话,就高兴地招...
    夕荷阅读 181评论 1 0