赞
踩
在我们开发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);
当然这里还有很多案例,这里主要是从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");
- PackageManager packageManager = getPackageManager();
- List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
- boolean isIntentSafe = activities.size() > 0;
我们创建一个按钮,并且在点击事件中创建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;
- }
- }
- <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>
- @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点击打开链接
原文地址:http://blog.csdn.net/huangyabin001/article/details/35265117
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。