2013-10-06 47 views
1

我想从我的应用程序共享多个图像到其他应用程序。在Android的开发者页面上,我发现:来自intentservice的Android共享意图

Intent shareIntent = new Intent(); 
     shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); 
     shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, SavedImages); 
     shareIntent.setType("image/*"); 
     startActivity(Intent.createChooser(shareIntent, "Share images to..")); 

如何从intentservice中使用此代码? 当使用从intentservice示例代码,我的应用程序崩溃与logcat的错误:

Calling startActivity from outside of an Activity context requires the flag FLAG_ACTIVITY_NEW_TASK

所以我添加

shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

,但我仍然得到同样的错误,我的应用程序崩溃。

如何从intentservice中使用共享意图?

+0

- 你不知道现在是否适合弹出一个活动,因为当你的IntentService在后台工作时,用户可能不会做其他事情。 – CommonsWare

+0

我知道用户可以做其他事情,但我仍想使用代码... – wildcat

回答

3

这行代码

startActivity(Intent.createChooser(shareIntent, "Share images to..")); 

这意味着你创造它是用来启动用户的对话活动,选择处理您的shareIntent该活动的意图对象。所以,在这种情况下,意图显示选择对话框活动所需要的标志FLAG_ACTIVITY_NEW_TASK
你可以试试:“我怎么可以使用此代码从intentservice”

Intent chooserIntent = Intent.createChooser(shareIntent, "Share images to.."); 
chooserIntent.addFlag(Intent.FLAG_ACTIVITY_NEW_TASK); 
startActivity(chooserIntent); 
+0

谢谢,现在正在工作! – wildcat