惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
博客园 - 司徒正美
C
Check Point Blog
G
Google Developers Blog
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
有赞技术团队
有赞技术团队
P
Proofpoint News Feed
IT之家
IT之家
B
Blog
博客园_首页
量子位
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
J
Java Code Geeks
H
Help Net Security
A
About on SuperTechFans
Apple Machine Learning Research
Apple Machine Learning Research
Jina AI
Jina AI
D
DataBreaches.Net
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
云风的 BLOG
云风的 BLOG
Google DeepMind News
Google DeepMind News

博客园 - gcg0036

亚信仨月印象(国庆时再写) 博客搬家了! log4j:ERROR A "org.apache.log4j.ConsoleAppender" object is not assignable to a "org.apache.log4j.Appender" variable. 模板引擎Crox用法大全(更新版) Apache Tomcat Maven Plugin 参数大全(Tomcat 6) Apache Tomcat Maven Plugin MyEclipse 2015 CI 12 无限试用 sshe源码分析——权限管理的后台部分 sshe源码分析——全局架构 无聊的三月终于过去了 failed to initialize monitor thread 在Android项目中的测试类点击"run as JUnit test"出错 Saving Data Managing the Activity Lifecycle Adding the Action Bar Building Your First App The AndroidManifest.xml File handlebars简单用法 高性能跨语言模板引擎Crox
Interacting with Other Apps
gcg0036 · 2015-03-08 · via 博客园 - gcg0036

http://www.cnblogs.com/gcg0036/p/4321279.html

Build an Implicit Intent:

implicit Intent不声明要调用的类的组件的具体名称,只声明要执行的动作

通常也包含和action有关的数据

1.如果你的数据是URI,可以使用一个很简单的Intent()构造器,来定义action和data

Uri number = Uri.parse("tel:5551234");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number);

当你的应用通过调用startActivity()引用上面这个intent的时候,拨号应用就会拨打上面这个电话了。

2.其他implicit intent可能需要“extra”数据来提供不同的数据类型,比如一个string类型,我们可以使用很多种重载的putExtra ()添加一种或几种额外的数据

默认情况下:系统根据包含的Uri数据来为intent选择合适的MIME类型。如果intent不包含Uri,就需要使用setType()来指定intent所关联的数据类型。确定MIME类型进一步确定了哪一种activity可以接收这个intent。

Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI);
Calendar beginTime = Calendar.getInstance().set(2012, 0, 19, 7, 30);
Calendar endTime = Calendar.getInstance().set(2012, 0, 19, 10, 30);
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME, beginTime.getTimeInMillis());
calendarIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME, endTime.getTimeInMillis());
calendarIntent.putExtra(Events.TITLE, "Ninja class");
calendarIntent.putExtra(Events.EVENT_LOCATION, "Secret dojo");

注意:定义的intent越具体越好,如果你想浏览图片,通过ACTION_VIEW intent,你就需要指定MIME类型为image/*,这样可以防止用于访问其他类型数据的app(比如地图app)被这个intent所激发。因为你指定了MIME类型以后,其他app因为数据类型不一致,所以不会被这个intent所trigger了。

尽管系统平台会将特定的intent对应到内置的app,但是在引用一个intent之前,最好还是验证一下是否可用。

因为如果你引用了一个没有app能对其响应的intent的时候,后果很严重,app会崩溃

PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isIntentSafe = activities.size() > 0;

注意: 当你的activity第一次启动的时候,你需要做这个检查,从而可以屏蔽一些功能,因为这些功能没有activity响应这些功能发出的intent。这样就可以避免app的crash。如果你知道是什么app可以handle这个你app里无法handle的intent的话,你还可以提供Google Play里的下载链接。

Start an Activity with the Intent:

build好一个intent,并且录入了相关信息以后,可以调用startActivity()方法,就可以向系统发送这个intent啦。如果系统发现不止一个app可以handle这个 intent的时候,系统就会提供一个列表来让你选择,如果只有一个的话,就不用选择啦,那就直接启动了。

下面代码是一个查看地图的完整代码:包括新建intent,验证是否有handle它的activity,启动等

// Build the intent
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);

// Verify it resolves
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(mapIntent, 0);
boolean isIntentSafe = activities.size() > 0;
 
// Start an activity if it's safe
if (isIntentSafe) {
    startActivity(mapIntent);
}

Getting a Result from an Activity:

注意:当你调用startActivityForResult()方法时,明确的和隐藏的intent都可以使用,但是用来接受其他activity返回结果的activity,必须使用明确的intent,以确保它接收到。

以下代码是选取一个联系人的activity,返回一个contact:

static final int PICK_CONTACT_REQUEST = 1;  // The request code
...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK, new Uri("content://contacts"));
    pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

以下代码接收返回结果:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == RESULT_OK) {
            // The user picked a contact.
            // The Intent's data Uri identifies which contact was selected.

            // Do something with the contact here (bigger example below)
        }

比如,联系人app返回选定的联系人信息contact URI作为结果,camera返回一个Bitmap作为结果。

Handle the Intent in Your Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    // Get the intent that started this activity
    Intent intent = getIntent();
    Uri data = intent.getData();

    // 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 ...
    }
}

如果想要使自己的activity返回一个值,只需要调用setResult()方法来指定result code和result intent 就可以。当用户在这个activity里操作完毕以后,就需要返回原来的activity了,调用finish()方法就可以关闭这个activity,回到原来的。

// Create intent to deliver some kind of result data

//两个参数:1.result code即Activity.RESULT_OK  2. result intent  默认值是RESULT_CANCELED

Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse("content://result_uri");
setResult(Activity.RESULT_OK, result);
finish();

没有必要检验你的activity是用哪个方法启动的,有没有返回值都一样,都用 setResult()设置好返回值,如果本来的activity使用需要返回值的方法startActivityForResult()来调用你的 activity,则就按照设置好的返回值返回给它,如果用 startActivity()调用你的activity,则就当没设置返回值一样,直接忽略原来设置的返回值就行。

http://blog.sina.com.cn/s/blog_8191005601019yci.html