android与其他应用的交互

来源:互联网 发布:vr照片拍摄软件 编辑:程序博客网 时间:2024/06/02 16:55

使用隐式的Intent Implicit Intents一般用于从一个app到另一个app
而显示的Intent是用于一个app内页面的跳转
需要声明指定的动作
如Uri number = Uri.parse(“tel:5551234”);
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
指定电话号码的intent
// Map point based on address
Uri location = Uri.parse(“geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California”);
// Or map point based on latitude/longitude
// Uri location = Uri.parse(“geo:37.422219,-122.08364?z=14”); // z param is zoom level
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
查看地图
Uri webpage = Uri.parse(“http://www.android.com“);
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
查看网页
没有Uri数据类型则用setType()方法来指定intent附带的数据类型,用putextra()来装在mime型数据
Intent emailIntent = new Intent(Intent.ACTION_SEND);
// The intent does not have a URI, so declare the “text/plain” MIME type
emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {“jon@example.com”}); // recipients
emailIntent.putExtra(Intent.EXTRA_SUBJECT, “Email subject”);
emailIntent.putExtra(Intent.EXTRA_TEXT, “Email message text”);
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(“content://path/to/email/attachment”));
// You can also attach multiple items by passing an ArrayList of Uris

验证是否有App去接收这个Intent
PackageManager packageManager = getPackageManager();
List activities = packageManager.queryIntentActivities(mapIntent, 0);
boolean isIntentSafe = activities.size() > 0;

            // Start an activity if it's safe            if (isIntentSafe) {                startActivity(mapIntent);            }
0 0
原创粉丝点击