深入分析:Android中app之间的交互(一,使用Action)

来源:互联网 发布:宜宾零距离网络 编辑:程序博客网 时间:2024/05/16 01:47

        在我们开发Android App应用的时候,有些需求需要我们启动其他的App来处理一些逻辑,例如我们需要根据一个地址来调用系统或者相关的地图Map App,这样我们不用在自己的App中编写相应的功能,而是通过Intent来发送一些请求,调用相关的应用来处理这些请求。并且我们称这种Intent为隐式的Intent;这种隐式的Intent是相对于显式的Intent来讲的。显式的Intent我们都比较熟悉,显式的Intent常常需要声明类的名称,而隐式的Intent我们需要声明一个Action,我们Action中定义了我们想要处理的请求。与Action相关联的还有data,例如我们需要查看的地址,或者我们需要拨打的电话号码,或者我们需要发送邮件的邮件地址等等。例如:

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

        上述代码中,我们通过startActivity()来调用Phone App,并进行拨打(5551234)的通话操作。当然这里还有很多案例,这里主要是从API 文档中摘录的了。贴在这里供大家参考。

查看地图:

// Map point based on addressUri 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 levelIntent mapIntent = new Intent(Intent.ACTION_VIEW, location);

浏览网页:

Uri webpage = Uri.parse("http://www.android.com");Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
发送邮件:
Intent emailIntent = new Intent(Intent.ACTION_SEND);// The intent does not have a URI, so declare the "text/plain" MIME typeemailIntent.setType(HTTP.PLAIN_TEXT_TYPE);emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"jon@example.com"}); // recipientsemailIntent.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
创建一个事件通知:
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的时候,要尽可能的详细,例如我们希望调用系统的图片查看器浏览图片,我们应该定义MIME type" image/*.",以防止会启动map app进行查看。而且如果没有app来响应我们的请求,我们的app就会崩溃。

        因此为了防止我们的app发送intent没有其他app来响应而导致应用异常退出,我们在发送intent前进行验证。为了验证是否有app的Activity来响应我们的Intent请求,我们需要调用queryIntentActivities()来进行验证。这个方法会返回一个list,我们通过判断list是否为空来验证,这样我们可以安全的使用Intent来实现不同App之间Activity的交互。如果没有响应的Acitivty来响应,我们可以提供一些链接给用户进行下载安装。验证方法如下:

       

PackageManager packageManager = getPackageManager();

List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);

boolean isIntentSafe = activities.size() > 0;

下面我们通过具体的案例来使用隐式的Intent,使不同的App中的Activity进行交互。

首先我们创建第一个项目appsend,我们创建一个按钮,并且在点击事件中创建Intent,并设置Action和type,并添加onActivityResult()来接收我们从第二个应用中返回的数据。

public void button(View view) {Intent intent = new Intent();intent.setAction(Intent.ACTION_VIEW);intent.setType("test/");startActivityForResult(intent, 2);}

@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);switch (requestCode) {case 2:switch (resultCode) {case Activity.RESULT_OK:text.setText("URI:" + data.getDataString());break;}break;}}

第二步:我们创建第二个应用,并且在清单文件中进行配置Intent-filter;

<activity            android:name="com.example.appreceiver.MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>            <intent-filter>                <action android:name="android.intent.action.VIEW" />                <data android:mimeType="test/*" />                <category android:name="android.intent.category.DEFAULT" />            </intent-filter>        </activity>

第三步:在第二个Activity中监听返回按钮,并传回数据。

@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {switch (keyCode) {case KeyEvent.KEYCODE_BACK:Intent result = new Intent("com.example.appsend",Uri.parse("content://result_uri"));setResult(Activity.RESULT_OK, result);finish();break;}return super.onKeyDown(keyCode, event);}
具体案例下载地址为:http://download.csdn.net/detail/huangyabin001/7561309点击打开链接



4 0
原创粉丝点击