Android开发官方文档---Interacting with Other Apps

邰棋
2023-12-01

例如:

调用自带浏览器打开指定网页

Uri webpage =Uri.parse("http://www.android.com"); 
Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);

 

打电话

Uri number =Uri.parse("tel:5551234"); 

Intent callIntent = newIntent(Intent.ACTION_DIAL, number);

 

打开地图

// Map point based onaddress 
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"); // zparam is zoom level 
Intent mapIntent = new Intent(Intent.ACTION_VIEW, location);

 

发送邮件

Intent emailIntent =new Intent(Intent.ACTION_SEND); 
// The intent does not have a URI, so declare the "text/plain" MIMEtype 
emailIntent.setType(HTTP.PLAIN_TEXT_TYPE); 
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"jon@example.com"}); //recipients 
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Emailsubject"); 
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email messagetext"); 
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");

 

使用下面代码获取可以使用的应用

PackageManagerpackageManager = getPackageManager(); 

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

boolean isIntentSafe =activities.size() > 0;

返回的 isIntentSafe 就为true,如果找不到应用打开就为false

 

 

创建选择器

Intent intent = newIntent(Intent.ACTION_SEND); 

... 

 

// Always use stringresources for UI text. 

// This says somethinglike "Share this photo with" 

String title =getResources().getString(R.string.chooser_title); 

// Create intent toshow chooser 

Intent chooser =Intent.createChooser(intent, title); 

 

// Verify the intentwill resolve to at least one activity 

if(intent.resolveActivity(getPackageManager()) != null) { 

   startActivity(chooser); 

}

 

获取Activity返回数据

static final intPICK_CONTACT_REQUEST = 1;  // The request code 

... 

private voidpickContact() { 

    Intent pickContactIntent = newIntent(Intent.ACTION_PICK, Uri.parse("content://contacts")); 

   pickContactIntent.setType(Phone.CONTENT_TYPE); // Show user only contacts w/phone numbers 

    startActivityForResult(pickContactIntent,PICK_CONTACT_REQUEST); 

}

 

/**

requestCode 请求码

resultCode 结果码

data 返回的带数据的Intent

*/

@Override 

protected void onActivityResult(int requestCode, int resultCode,Intent data) { 

    if (requestCode ==PICK_CONTACT_REQUEST) {  //判断请求码

        if (resultCode == RESULT_OK) {  //判断结果是否成功 

   Uri contactUri =data.getData(); 

            String[] projection = {Phone.NUMBER}; 

               //query() 操作不要在主线程中使用,避免操作超时停止响应,这里是为了简化才放在主线程

            Cursor cursor = getContentResolver() .query(contactUri,projection, null, null, null); 

            cursor.moveToFirst(); 

 

            int column = cursor.getColumnIndex(Phone.NUMBER); 

            String number = cursor.getString(column); 

        } 

    } 

}

 

 

让其它应用可以调用你的Activity

添加Intent Filter

<action> 接受的动作

<category>  类型

<data> 数据类型

 

<activityandroid:name="ShareActivity"> 

    <!-- filter for sending text;accepts SENDTO action with sms URI schemes --> 

    <intent-filter> 

        <actionandroid:name="android.intent.action.SENDTO"/> 

        <categoryandroid:name="android.intent.category.DEFAULT"/> 

        <dataandroid:scheme="sms" /> 

        <dataandroid:scheme="smsto" /> 

    </intent-filter> 

    <!-- filter for sending text orimages; accepts SEND action and text or image data --> 

    <intent-filter> 

        <actionandroid:name="android.intent.action.SEND"/> 

        <categoryandroid:name="android.intent.category.DEFAULT"/> 

        <dataandroid:mimeType="image/*"/> 

        <dataandroid:mimeType="text/plain"/> 

    </intent-filter> 

</activity>

注意:

category  默认可以设置成 DEFAULT

 

在Activity中处理Intent

@Override 

protected voidonCreate(Bundle savedInstanceState) { 

   super.onCreate(savedInstanceState); 

 

   setContentView(R.layout.main); 

 

    // Get the intent that started thisactivity 

    Intent intent = getIntent();   //获取请求的Intent对象

    Uri data = intent.getData(); //获取Intent携带的数据

 

    // Figure out what to do based onthe intent type 

    if (intent.getType().indexOf("image/") != -1){   //根据不同的类型执行不同操作

        //Handle intents with image data ... 

    } else if (intent.getType().equals("text/plain")){ 

        //Handle intents with text ... 

    } 

}

 

返回处理结果

RESULT_OK 或者 RESULT_CANCELED

注意:默认返回的是 RESULT_CANCELED,如果在设置处理结果之前点击返回按钮取消当前的Activity,返回的就是默认值。

Intent result = newIntent("com.example.RESULT_ACTION",Uri.parse("content://result_uri"); 

setResult(Activity.RESULT_OK,result); 

finish(); //如果请求的源Activity调用的是 startActivityForResult,设置完结果之后应该调用finish结束当前的Activity,返回到源Activity.

也可以使用setResult

setResult(RESULT_COLOR_RED); 

finish();

返回简单的整数值(大于0)

 类似资料: