#Androdi training# interactions with activities or apps
来源:互联网 发布:优化设计措施 编辑:程序博客网 时间:2024/05/22 00:16
一 to Other's Apps
explicit intent
Intent mIntent = new Intent();mIntent.setClass(this,MainConversationActivity.class);startActivity(mIntent);
implicit intent
call:
Uri number = Uri.parse("tel:5551234");Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
map:
// 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);
view a page
Uri webpage = Uri.parse("http://www.android.com");Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
tips:
1.you can add more data through putExtra()
2.if you don't include a Uri in the intent, you should usually use setType() to specify the type of data
associated with the intent.
send an email with an attachment
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
create a 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, "Ninja class");calendarIntent.putExtra(Events.EVENT_LOCATION, "Secret dojo");
3.you may need to verify if there is an app to receive the intent
PackageManager packageManager = getPackageManager();List activities = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);boolean isIntentSafe = activities.size() > 0;
if(isIntentSafe){ startActivity(mapIntent);}
4. show the chooser
Intent intent = new Intent(Intent.ACTION_SEND);...// Always use string resources for UI text.// This says something like "Share this photo with"String title = getResources().getString(R.string.chooser_title);// Create intent to show chooserIntent chooser = Intent.createChooser(intent, title);// Verify the intent will resolve to at least one activityif (intent.resolveActivity(getPackageManager()) != null) { startActivity(chooser);}
二 getting a Result from an Activity
startActivityForResult()
onActivityResult()
static final int PICK_CONTACT_REQUEST = 1; // The request code. Later, you app can properly identify the result and determine how to handle it....private void pickContact() { 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, PICK_CONTACT_REQUEST);}
@Overrideprotected 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) } }}
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) { // Check which request it is that we're responding to if (requestCode == PICK_CONTACT_REQUEST) { // Make sure the request was successful if (resultCode == RESULT_OK) { // Get the URI that points to the selected contact Uri contactUri = data.getData(); // We only need the NUMBER column, because there will be only one row in the result String[] projection = {Phone.NUMBER}; // Perform the query on the contact to get the NUMBER column // We don't need a selection or sort order (there's only one result for the given URI) // CAUTION: The query() method should be called from a separate thread to avoid blocking // your app's UI thread. (For simplicity of the sample, this code doesn't do that.) // Consider using CursorLoader to perform the query. Cursor cursor = getContentResolver() .query(contactUri, projection, null, null, null); cursor.moveToFirst(); // Retrieve the phone number from the NUMBER column int column = cursor.getColumnIndex(Phone.NUMBER); String number = cursor.getString(column); // Do something with the phone number... } }}
三 allwoing other apps to start your activity
<intent-filter>
Action:<action>
ACTION_SEND
ACTION_VIEW
Data:<data>
A description of the data associated with the intent.
you can specify the MIME type
Note: if you don't need to declare specifics about data Uri(such as when your activity handles to other kind of "extra")
data, instead of a Uri), you should specify only the android:mimeType attribute to declare the type of data, such as text/plain or image/jpeg
Category:<category>
rarely used.
<activity android:name="ShareActivity"> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="text/plain"/> <data android:mimeType="image/*"/> </intent-filter></activity>
<activity android:name="ShareActivity"> <!-- filter for sending text; accepts SENDTO action with sms URI schemes --> <intent-filter> <action android:name="android.intent.action.SENDTO"/> <category android:name="android.intent.category.DEFAULT"/> <data android:scheme="sms" /> <data android:scheme="smsto" /> </intent-filter> <!-- filter for sending text or images; accepts SEND action and text or image data --> <intent-filter> <action android:name="android.intent.action.SEND"/> <category android:name="android.intent.category.DEFAULT"/> <data android:mimeType="image/*"/> <data android:mimeType="text/plain"/> </intent-filter></activity>
handle the Intent
@Overrideprotected 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 ... }}
return a result
when your operation is done and the user should returen original activity, call finish() to close (and destroy) your activity.
Intent result = new Intent("com.example.RESULT_ACTION", Uri.parse("content://result_uri"));setResult(Activity.RESULT_OK, result);finish();
the default is RESULT_CANCELED.
setResult(RESULT_COLOR_RED);finish();
the intent is not necessary.
- #Androdi training# interactions with activities or apps
- Develop--Training(三)Getting Started--Interacting with Other Apps
- Android Develop Training——和其它Android应用交互(Interacting with Other Apps)
- Scaling Apps with Varnish
- win32-apps-with-mingw
- Interacting with Other Apps
- Building Apps with Multimedia
- Building Apps with Multimedia
- Integration with Existing Apps #
- Activities
- Activities
- Activities
- Activities
- Activities
- Activities
- Activities
- Activities
- Activities
- 自己的电脑上安装Oracle,sys用户可以用任何密码登陆的问题
- linux c语言字符串函数replace,indexOf,substring等的实现(转)
- android studio 断开与SVN的连接
- java原生实现屏幕设备遍历和屏幕采集(捕获)等功能
- TestComplete 调用powershell 和cmd并获取屏幕输出
- #Androdi training# interactions with activities or apps
- C++第三次实验-数组选择
- 虚函数表深度剖析
- SCR888 Casino Strike and WIN Samsung Galaxy S7 EDGE
- C语言学习笔记7
- NP
- sql中插入多列多行数据
- 过渡模块与其编写套路
- 视频会议十大开源编解码项目排行