在深入分析:Android在app之间的相互作用(一个,使用Action)

        我们开发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 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);
发送邮件:
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
创建一个事件通知:
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,我们创建一个button。而且在点击事件中创建Intent。并设置Action和type,并加入onActivityResult()来接收我们从第二个应用中返回的数据。

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

@Override
	protected 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中监听返回button,并传回数据。

	@Override
	public 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点击打开链接



版权声明:本文博客原创文章,博客,未经同意,不得转载。

posted @ 2015-08-13 10:26  lcchuguo  阅读(387)  评论(0编辑  收藏  举报