您的位置:首页 > 移动开发

共享数据 之 从其他APP接收数据

2013-03-27 14:12 561 查看
你的APP可以发送数据给其他的APP,那么也可以很容易的从其他的APP接收数据。要考虑用户如何和你的APP交互,你的APP需要接收哪类数据。比方说一个社交网络APP,可能会对text类型的数据感兴趣,比方说一个网站的URL地址等。例如Google+ Android application就接收各种txt文本和单张或者多张图片。利用这个APP,用户可以很方便的将图库里面的图片传到google+.

修改你的Manifest文件

intent filter告诉系统,这个APP可以接收处理的intent类型。比方说创建一个
ACTION_SEND动作的intent,那么如果你希望接收这个intent,那么就需要在想要接收的APP里面的对应的activity里面创建一个intent filter,来声明接收这个动作。你需要在你的manifest文件里面定义这个intent
filter,,用 
<intent-filter>标签来定义。如果你的APP希望可以处理text文本信息,可以接收任何类型格式的一张或者多张图片,那么你的manifest里面的intent filter应该这么写:


[code]<activity android:name=".ui.MyActivity" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="image/*" />
</intent-filter>
</activity>


注:想要了解更多,阅读:Intents and Intent Filters

当其他的程序通过一个intent来传递要分享的数据,把这个intent作为
startActivity()参数,启动的时候,如果你的程序可以接收这个intent的动作和要处理的数据,你的程序就会显示在一个可供用户选择的一个对话框里面,因为可能有多个应用都可以处理这个intent.如果选择了你的APP,那么对应的你的APP里面的activity就会启动(如上面.ui.MyActivity会被启动),然后就可以在你的这个activity里面来合适的处理这个数据了。


处理传进来的内容


要处理intent传递进来的内容,首先要调用
getIntent()来获取之前传递内容的那个intent.当获取到这个对象的时候,下来就可以考虑根据里面的内容做对应的处理。要记住,如果你的activity可以被系统的其他部分启动,比方说launcher,那么你就需要在检查intent的时候进行这种考虑。例如:


void onCreate (Bundle savedInstanceState) {
...
// Get intent, action and MIME type
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();

if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
handleSendText(intent); // Handle text being sent
} else if (type.startsWith("image/")) {
handleSendImage(intent); // Handle single image being sent
}
} else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
if (type.startsWith("image/")) {
handleSendMultipleImages(intent); // Handle multiple images being sent
}
} else {
// Handle other intents, such as being started from the home screen
}
...
}

void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
// Update UI to reflect text being shared
}
}

void handleSendImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
// Update UI to reflect image being shared
}
}

void handleSendMultipleImages(Intent intent) {
ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (imageUris != null) {
// Update UI to reflect multiple images being shared
}
}

警告:当检查用extra获取的数据的时候,你永远不知道别的APP会发送什么数据给你。比方说,一个错误的MIME可能被设置,或者是发送的图片非常大。要记得处理二进制数据的时候,要记得和主UI线程分开,不要在主UI线程里面进行。

更新UI可以简单的更新
EditText内容,也可以很复杂,如处理一张图。这个动作直接明确了你的APP将会产生的变化。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: