- 浏览: 2551329 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Android UI(4)Getting Started - Interacting with Other Apps
Interacting with Other Apps
Every time we pass an intent to the system, startActivity, even the system will start an activity in other apps.
1. Sending the User to Another App
I do not need to build a map on my own apps if I meet an address and I want to display it on a map. I can send an intent to other apps and pass the address as parameter. And we need to use implicit Intent
Build an Implicit Intent
For example:
Phone Call
Uri number = Uri.parse("tel:5127922045");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number):
Once I call the startActivity(), the Phone app initiates a call to the given phone number then.
View a Map
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
//We can directly put the address in the query with geo 0
// Uri location = Uri.parse("geo:37.422219,-122.08364?z=14");
// Or we can also the latitude and longitude with z , zoom level
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
View a Web Page
Uri webpage = Uri.parse("http://www.android.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
Email with Extra Data
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"luohuazju@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT,"Subject Title");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Content with Good Luck!");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment");
Calendar Event
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, "Meeting Plan");
calendarIntent.putExtra(Events.EVENT_LOCATION, "everywhere");
Verify There is an App to Receive the Intent
Try to query and find the activities which will response to your intent
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent,0);
boolean isIntentSave = activities.size() > 0;
Here is what I wrote in my activity.
intent.setClass(this, PersonListActivity.class);if(isIntentSafe(intent)){ startActivity(intent);
}
private boolean isIntentSafe(Intent intent) {
boolean flag = false; PackageManager manager = this.getPackageManager(); List<ResolveInfo> activities = manager.queryIntentActivities(intent, 0); if(activities != null && activities.size() > 0){
Log.d(TAG,"The intent is save to invoke."); flag = true; } return flag;
}
Start an Activity with the Intent
Just call startActivity(intent);
If multiple activities are responding this intent, the system will provide an option page.
Show an App Chooser
Only we can customized the title of the chooser
Intent intent = new Intent(...);
String title = getResources().getText(R.string.chooser_title);
Intent chooser = Intent.createChooser(intent,title);
startActivity(chooser);
2. Getting a Result from an Activity
For example, your app can start a camera app and receive the captured photo as as a result. Or start the People app in order to pick up an contract detail.
startActivityForResult() -----> onActivityResult() callback
Start the Activity
private static final int REQUEST_CODE = 10001;
final Button buttonJson = (Button) findViewById(R.id.pickup_contact);
buttonJson.setOnClickListener(new View.OnClickListener() { publicvoid onClick(View v) { Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers startActivityForResult(pickContactIntent, REQUEST_CODE); }
});
Receive the Result
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == REQUEST_CODE) {
// 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.
Log.d(TAG, "I get here! I get the callback response."); Uri contactUri = data.getData(); String[] projection = {Phone.NUMBER}; Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null); cursor.moveToFirst(); int column = cursor.getColumnIndex(Phone.NUMBER); String number = cursor.getString(column); Log.d(TAG, "The number you choose it is = " + number); final TextView infoView = (TextView) findViewById(R.id.contact_info); infoView.setText("Number You Pickup is " + number); } }
}
Error Message:
04-04 10:56:42.623: E/DatabaseUtils(145): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/5 from pid=711, uid=10041 requires android.permission.READ_CONTACTS
Solution:
<uses-permission android:name="android.permission.READ_CONTACTS" />
3. Allowing Other Apps to Start Your Activity
…snip...
References:
http://developer.android.com/training/basics/intents/index.html
Interacting with Other Apps
Every time we pass an intent to the system, startActivity, even the system will start an activity in other apps.
1. Sending the User to Another App
I do not need to build a map on my own apps if I meet an address and I want to display it on a map. I can send an intent to other apps and pass the address as parameter. And we need to use implicit Intent
Build an Implicit Intent
For example:
Phone Call
Uri number = Uri.parse("tel:5127922045");
Intent callIntent = new Intent(Intent.ACTION_DIAL, number):
Once I call the startActivity(), the Phone app initiates a call to the given phone number then.
View a Map
Uri location = Uri.parse("geo:0,0?q=1600+Amphitheatre+Parkway,+Mountain+View,+California");
//We can directly put the address in the query with geo 0
// Uri location = Uri.parse("geo:37.422219,-122.08364?z=14");
// Or we can also the latitude and longitude with z , zoom level
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);
View a Web Page
Uri webpage = Uri.parse("http://www.android.com");
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
Email with Extra Data
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType(HTTP.PLAIN_TEXT_TYPE);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"luohuazju@gmail.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT,"Subject Title");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Content with Good Luck!");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("content://path/to/email/attachment");
Calendar Event
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, "Meeting Plan");
calendarIntent.putExtra(Events.EVENT_LOCATION, "everywhere");
Verify There is an App to Receive the Intent
Try to query and find the activities which will response to your intent
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent,0);
boolean isIntentSave = activities.size() > 0;
Here is what I wrote in my activity.
intent.setClass(this, PersonListActivity.class);if(isIntentSafe(intent)){ startActivity(intent);
}
private boolean isIntentSafe(Intent intent) {
boolean flag = false; PackageManager manager = this.getPackageManager(); List<ResolveInfo> activities = manager.queryIntentActivities(intent, 0); if(activities != null && activities.size() > 0){
Log.d(TAG,"The intent is save to invoke."); flag = true; } return flag;
}
Start an Activity with the Intent
Just call startActivity(intent);
If multiple activities are responding this intent, the system will provide an option page.
Show an App Chooser
Only we can customized the title of the chooser
Intent intent = new Intent(...);
String title = getResources().getText(R.string.chooser_title);
Intent chooser = Intent.createChooser(intent,title);
startActivity(chooser);
2. Getting a Result from an Activity
For example, your app can start a camera app and receive the captured photo as as a result. Or start the People app in order to pick up an contract detail.
startActivityForResult() -----> onActivityResult() callback
Start the Activity
private static final int REQUEST_CODE = 10001;
final Button buttonJson = (Button) findViewById(R.id.pickup_contact);
buttonJson.setOnClickListener(new View.OnClickListener() { publicvoid onClick(View v) { Intent pickContactIntent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")); pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/ phone numbers startActivityForResult(pickContactIntent, REQUEST_CODE); }
});
Receive the Result
protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == REQUEST_CODE) {
// 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.
Log.d(TAG, "I get here! I get the callback response."); Uri contactUri = data.getData(); String[] projection = {Phone.NUMBER}; Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null); cursor.moveToFirst(); int column = cursor.getColumnIndex(Phone.NUMBER); String number = cursor.getString(column); Log.d(TAG, "The number you choose it is = " + number); final TextView infoView = (TextView) findViewById(R.id.contact_info); infoView.setText("Number You Pickup is " + number); } }
}
Error Message:
04-04 10:56:42.623: E/DatabaseUtils(145): java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/5 from pid=711, uid=10041 requires android.permission.READ_CONTACTS
Solution:
<uses-permission android:name="android.permission.READ_CONTACTS" />
3. Allowing Other Apps to Start Your Activity
…snip...
References:
http://developer.android.com/training/basics/intents/index.html
发表评论
-
ionic UI(4)ionic2 framework - basic and components and native
2016-03-24 02:33 1261ionic UI(4)ionic2 framework - b ... -
ionic UI(3)TypeScript - handbook
2016-03-22 23:21 634ionic UI(3)TypeScript - handboo ... -
ionic UI(2)ionic2 framework - TypeScript - tutorial
2016-03-22 06:52 1656ionic UI(2)ionic2 framework - T ... -
Parse and Heroku Service(3)Parse Server and Parse Dashboard
2016-03-22 06:30 967Parse and Heroku Service(3)Pars ... -
Parse and Heroku Service(2)Mail Templates and Push Notification
2016-03-22 02:45 583Parse and Heroku Service(2)Mail ... -
ionic UI(1)Introduction
2016-03-19 03:18 723ionic UI(1)Introduction 1 Inst ... -
Parse and Heroku Service(1)Heroku Installation and Play
2016-03-19 00:13 824Parse and Heroic Service(1)Hero ... -
Hybrid(5)Customize Meteor Directly Google Login
2015-09-01 02:33 910Hybrid(5)Customize Meteor Direc ... -
Hybrid(4)Favorite Places - Google Login
2015-09-01 02:02 1341Hybrid(4)Favorite Places - Goog ... -
Hybrid(3)More Meteor Example - Social
2015-08-11 05:04 756Hybrid(3)More Meteor Example - ... -
Hybrid(2)meteor Running Android and iOS
2015-07-28 23:59 1050Hybrid(2)meteor Running Android ... -
Create the Google Play Account
2015-07-18 06:42 1101Create the Google Play Account ... -
Secure REST API and Mobile(1)Document Read and Understand OAUTH2
2015-07-14 00:36 764Secure REST API and Mobile(1)Do ... -
Screen Size and Web Design
2015-07-11 01:11 725Screen Size and Web Design iPh ... -
Hybrid(1)ionic Cordova meteor
2015-06-25 05:49 471Hybrid(1)ionic Cordova meteor ... -
Android Fire Project(1)Recall Env and Knowledge
2015-02-11 12:28 687Android Fire Project(1)Recall ... -
Android Content Framework(1)Concept
2014-06-14 13:54 1081Android Content Framework(1)Con ... -
Feel Android Studio(1)Install and Update Android Studio
2014-04-11 03:12 2029Feel Android Studio(1)Install a ... -
IOS7 App Development Essentials(2)iBeacon
2014-03-05 05:55 890IOS7 App Development Essentials ... -
IOS7 App Development Essentials(1) Persistent Store
2014-03-05 05:54 1327IOS7 App Development Essentials ...
相关推荐
在Android开发中,应用程序通常由多个Activity组成,每个Activity对应一个用户界面,并负责完成特定的任务。例如,一个Activity可以专门用于显示地图,而另一个可能用于拍照功能。当需要在不同的Activity之间转换,...
The module has two 32-bit MCUs - an x86 Intel Quark processor and an ARC EM4 processor along with 384kB flash memory and 80kB SRAM. These onboard MCUs combine a variety of new technologies including ...
《ISO IEC TR 23187:2020 Information technology - Cloud computing - Interacting with cloud service partners (CSNs)》是国际标准化组织(ISO)和国际电工委员会(IEC)联合发布的一份技术报告,旨在为云计算...
《2016-Interacting Multiview Tracker》是一个针对多视图交互追踪技术的研究项目,由韩国国立釜山大学计算机视觉实验室(CVL@GIST)的团队开发。这个项目的主要目标是解决在多个视角下同时跟踪多个目标物体的问题,...
在Android操作系统中,应用间的交互(Interacting with Other Apps)是一项关键功能,它允许不同的应用程序之间共享数据、服务和功能,提升用户体验。Android系统通过多种机制实现这一目标,包括Intent、...
Chapter 1: Getting Started with Android Chapter 2: Views, Graphics, and Drawing Chapter 3: User Interaction Recipes Chapter 4: Communications and Networking Chapter 5: Interacting with Device Hardware...
6.Getting familiar with the android process model and low-level concurrent constructs delivered by the Android SDK. 7.Interacting with nearby devices over Bluetooth and WiFi communications channels. 8...
6.Getting familiar with the android process model and low-level concurrent constructs delivered by the Android SDK. 7.Interacting with nearby devices over Bluetooth and WiFi communications channels. 8...
Now that more people spend more time interacting with mobile apps than with their desktop counterparts, you need to think about your iOS app’s performance the moment you write your first line of code...
Interacting with other devices via SMS, web browsing, and social networkingStoring data efficiently with SQLite and its alternatives Accessing location data via GPS Using location-related services ...
**jQuery 与 DOM 交互详解** 在Web开发中,JavaScript是一种不可或缺的语言,它负责与用户进行交互并...在探索`jQuery-Interacting-with-DOM-master`这个压缩包中的文件时,你将有机会亲身体验到jQuery的强大之处。
UNIT 2 - STRINGS, TUPLES, AND INTERACTING WITH THE USER Lesson 7 - Introducing string objects: sequences of characters Lesson 8 - Advanced string operations Lesson 9 - Simple error messages Lesson 10 ...
【应用】★★★★★-manzana-.NET API for interacting with the Apple iPhone【应用】★★★★★-manzana-.NET API for interacting with the Apple iPhone 1.适合学生学习研究参考 2.适合个人学习研究参考 3.适合...
Do you want to get started building apps for Android, today's number one mobile platform? Are you already building Android apps but want to get better at it? The Android(TM) Developer's Cookbook, ...
染色质重塑酶INO80复合物的潜在靶基因-BCCIP,苏家明,隋毅,目前认为BRCA2/CDKN1A-相关蛋白BCCIP在肿瘤抑制过程中作为BRCA2的辅助因子发挥重要的作用。已知BCCIP在多种肿瘤中包括卵巢癌、肾癌和大肠�
Chapter 4: Interacting with Device Hardware and Media Chapter 5: Persisting Data Chapter 6: Interacting with the System Chapter 7: Graphics and Drawing Chapter 8: Working with Android NDK and ...
Interacting with other devices via SMS, web browsing, and social networkingStoring data efficiently with SQLite and its alternatives Accessing location data via GPS Using location-related services ...
《manzana-.NET API for interacting with the Apple iPhone》是一个专为与苹果iPhone进行交互而设计的.NET API库。这个源码包对于那些希望在iOS平台上构建应用的开发者来说,是一份宝贵的资源,特别是对.NET框架有...
【标题】"IOS应用源码之manzana-.NET API for interacting with the Apple iPhone" 提供了一种使用.NET框架与苹果iPhone进行交互的途径。manzana是一个.NET库,它为开发者提供了方便的API,使得在iOS应用开发过程中...
本文将深入探讨标题为“【应用】-manzana-.NET API for interacting with the Apple iPhone.7z”的压缩包,其中包含了一个名为“Manzana”的.NET库,它为开发者提供了一种与Apple iPhone进行交互的方式。 首先,...