我试图通过发送短信在两个应用程序之间进行通信。这是我的第一个应用程序,它包含一个按钮,将调用一个动作发送,以便所有其他应用程序的动作发送出现在这个对话框。
((Button) findViewById(R.id.button1))
.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra("ComingFrom", "Activity 1");
final int result = 1;
startActivityForResult(Intent.createChooser(intent, "Sending File..."),result);
}
});
现在,这是我的第二个应用程序,将获得意图。
// Get the intent that started this activity
Intent intent = getIntent();
Uri data = intent.getData();
if (intent != null) {
// Figure out what to do based on the intent type
if (intent.getType().indexOf("image/") != -1) {
// Handle intents with image data ...
} else if (intent.getType().equals("text/plain")) {
// Handle intents with text ...
}
}
这是我的第二个应用程序清单,其中包含动作SEND。
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
<data android:mimeType="image/*" />
</intent-filter>
但问题是我没有得到一个对话框,显示我的其他应用程序,而不是显示没有应用程序执行此操作。我做错了什么?
你还应该在你的bundle中添加一个mimetype属性。
intent.setType("text/plain");
例如。
你需要设置这个类型发送文本,图像等的类型为mime类型。
对于简单文本或纯文本,您需要使用text/plain;对于图像,您需要设置image/*无论你是否附加任何图像文件它都会打开android的默认应用程序
intent.setType("text/plain");
或for(用于图像文件发送)
intent.setType("image/*");