Implicit Intent--含蓄的intent

通过intent调用打电话,打开地图、打开浏览器,创建邮件,创建事件的操作

截图:


代码

package com.example.hellointent;

import java.util.Calendar;
import java.util.List;

import org.apache.http.protocol.HTTP;

import android.net.Uri;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.provider.CalendarContract.Events;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {

	private static int sIndex = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	//打电话
	public void onTel(View view) {
		Uri number = Uri.parse("tel:5551234");
		Intent callIntent = new Intent(Intent.ACTION_DIAL, number);
		String title = sIndex % 2 == 0 ? null : "打电话";
		startActivity(callIntent, title);

	}
	//打电话
	public void onMap(View view) {
		// Map point based on address
		Uri location = Uri.parse("geo:40.004754,116.430867?z=14");
		// 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);
		String title = sIndex % 2 == 0 ? null : "获取地图";
		startActivity(mapIntent, title);

	}
   //打开浏览器
	public void onWeb(View view) {
		Uri webpage = Uri.parse("http://www.android.com");
		Intent webIntent = new Intent(Intent.ACTION_VIEW, webpage);
		String title = sIndex % 2 == 0 ? null : "打开网页";
		startActivity(webIntent, title);

	}
	//创建邮件
	public void onEmail(View view) {
		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[] { "271414845@qq.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
		String title = sIndex % 2 == 0 ? null : "--发送邮件--";
		startActivity(emailIntent, title);
	}
	//创建事件的操作
	@SuppressLint("NewApi")
	public void onCreatCalender(View view) {
		Intent calendarIntent = new Intent(Intent.ACTION_INSERT, Events.CONTENT_URI);
		Calendar beginTime = Calendar.getInstance();
		beginTime.set(2014, 5, 1, 9, 30);
		Calendar endTime = Calendar.getInstance();
		endTime.set(2014, 5, 1, 18, 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");
		String title = sIndex % 2 == 0 ? null : "窗將活动";
		startActivity(calendarIntent, title);
	}

	private void startActivity(Intent intent, String title) {
		// If you invoke an intent and there is no app available on the device
		// that can handle the intent, your app will crash.
		PackageManager packageManager = getPackageManager();
		List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
		if (activities.size() > 0) {
			if (TextUtils.isEmpty(title)) {
				startActivity(intent);
			} else {
				// To show the chooser, create an Intent using createChooser()
				// and pass it to startActivity(). For example:
				//为选择器创建标题
				Intent chooser = Intent.createChooser(intent, title);
				startActivity(chooser);
			}
		} else {
			Toast.makeText(this, "没有找到符合要求的界面", Toast.LENGTH_SHORT).show();
		}
		sIndex++;
	}

}
laout文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onTel"
        android:text="打电话" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onMap"
        android:text="地图" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onWeb"
        android:text="浏览器" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onEmail"
        android:text="创建邮件" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onCreatCalender"
        android:text="创建事件" />

</LinearLayout>


注意

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

activities.size() > 0

用于判断是否有符合要求的界面如果没有直接调用startActivity(intent) 会导致程序crash

2.

如果activities.size() 大于1就会出现选择器

//为选择器创建标题
Intent chooser = Intent.createChooser(intent, title);
startActivity(chooser);

通过下面的图片就能看到加上


有标题的选择器:



默认标题的选择器


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值