1.显示Intent 指定接收者
2.隐式Intent 不指定接收者:下面全部都是隐式Intent
package cn.uflycn.com.aidltest;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.IBinder;
import android.os.RemoteException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import cn.uflycn.com.aidltest.aidl_test.Book;
import cn.uflycn.com.aidltest.aidl_test.aidl.BookService;
import cn.uflycn.com.aidltest.aidl_test.aidl.IBookManager;
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private void initViews() {
findViewById(R.id.btn_share_text).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// sendTextIntent();
// dialNum();
// checkLocate();
// openWebPage();
// sendIntentChooser();
sendEmailWidthAttchment();
}
});
findViewById(R.id.btn_share_binary).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendBinaryIntent();
}
});
findViewById(R.id.btn_share_Multiple).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendMuiltipleIntent();
}
});
}
void dialNum() {
Uri number = Uri.parse("tel:5553567");
Intent dialIntent = new Intent(Intent.ACTION_DIAL, number);
startActivity(dialIntent);
}
void checkLocate() {
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
if (hasAnyAppToStartIntent(mapIntent)) {
startActivity(mapIntent);
} else {
Toast.makeText(this, "未找到对应的App", Toast.LENGTH_SHORT).show();
}
}
void openWebPage() {
Uri uri = Uri.parse("http://www.baidu.com");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
if (hasAnyAppToStartIntent(intent)) {
startActivity(intent);
}
}
/**
* 如果有多个app 可以打开会有一个选择默认打开的选择,
* 选择之后不再弹出选择窗口而是直接打开
*/
void sendTextIntent() {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "this is my share intent");
shareIntent.setType("text/plain");
if (hasAnyAppToStartIntent(shareIntent)) {
startActivity(shareIntent);
}
}
/**
* 每次不确定使用哪个app 进行分享
*/
void sendIntentChooser() {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "this is my share text");
Intent chooser = Intent.createChooser(intent, "share message via");
if (hasAnyAppToStartIntent(intent)) {
startActivity(chooser);
}
}
/**
* 分享二进制的内容
*/
void sendBinaryIntent() {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.setType("image/jpeg");
//
Uri imageUri = Uri.parse("file:/// android_asset/girl.jpg");
sendIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
if (hasAnyAppToStartIntent(sendIntent)) {
startActivity(Intent.createChooser(sendIntent, "分享图片"));
}
}
/**
* 发送多块内容
*/
void sendMuiltipleIntent() {
ArrayList<Uri> imageUris = new ArrayList<Uri>();//asset 文件夹路径
Uri imageUri = Uri.parse("file:/// android_asset/girl.jpg");
imageUris.add(imageUri);
imageUris.add(imageUri);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share images to.."));
}
boolean hasAnyAppToStartIntent(Intent intent) {
PackageManager packageManager = getPackageManager();
List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(intent, 0);
return resolveInfos.size() > 0;
}
void sendEmailWidthAttchment() {
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
//地址
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"jon@126.com"});
//主题
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "this is email subject");
//内容
emailIntent.putExtra(Intent.EXTRA_TEXT, "this is email content");
//附件
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content:file:/// android_asset/girl.jpg"));
if (hasAnyAppToStartIntent(emailIntent)) {
startActivity(emailIntent);
}
}
}
3.接收隐式Intent
<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>
4.处理接收到的数据
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
}
}