Intent常用系统调用

/********************************************************** 
 * @文件名称:Test.java 
 * @创建时间:2014年11月20日 下午11:16:40
 * @修改历史:2014年11月20日
 **********************************************************/
package com.jinlin.intentdemo;

import java.io.IOException;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.MediaStore;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

/**
 * @author J!nl!n
 * @date 2014年11月20日
 * @time 下午11:16:40
 * @type Test.java
 * @todo
 */
public class Test extends Activity implements OnClickListener {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		findViewById(R.id.Button1).setOnClickListener(this);
		findViewById(R.id.Button2).setOnClickListener(this);
		findViewById(R.id.Button3).setOnClickListener(this);
		findViewById(R.id.Button4).setOnClickListener(this);
		findViewById(R.id.Button5).setOnClickListener(this);
		findViewById(R.id.Button6).setOnClickListener(this);
		findViewById(R.id.Button7).setOnClickListener(this);
		findViewById(R.id.Button8).setOnClickListener(this);
		findViewById(R.id.Button9).setOnClickListener(this);
		findViewById(R.id.Button10).setOnClickListener(this);
		findViewById(R.id.Button11).setOnClickListener(this);
		findViewById(R.id.Button12).setOnClickListener(this);
		findViewById(R.id.Button13).setOnClickListener(this);
		findViewById(R.id.Button14).setOnClickListener(this);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		// 拨打电话
		case R.id.Button1:
			Toast.makeText(Test.this, "正在启动拨号器,请稍后...", Toast.LENGTH_SHORT).show();
			Intent intent1 = new Intent(); // 创建一个intent
			intent1.setAction(Intent.ACTION_DIAL); // 设置intent的Action属性
			intent1.setData(Uri.parse("tel://")); // 设置intent的Date属性
			startActivity(intent1); // 启动Activity //启动Activity
			break;
		// 打开浏览器
		case R.id.Button2:
			try {
				Toast.makeText(Test.this, "正在启动浏览器,请稍后...", Toast.LENGTH_SHORT).show();
				Uri uri = Uri.parse("http://www.baidu.com"); // 将字符串转换为uri对象
				Intent intent2 = new Intent(Intent.ACTION_VIEW, uri); // 创建一个同时指定Action属性和Data属性的intent
				intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent2); // 启动Activity
			} catch (ActivityNotFoundException e) {
				Toast.makeText(Test.this, " 启动'浏览器'异常!\n请检查是否安装了该应用.", Toast.LENGTH_SHORT).show();
			}
			break;
		// 打开地图
		case R.id.Button3:
			try {
				Toast.makeText(Test.this, "正在打开地图,请稍后...", Toast.LENGTH_SHORT).show();
				Uri uri = Uri.parse("geo:38.899533,-77.036476");// 将字符串转换为uri对象
				Intent intent3 = new Intent();
				intent3.setAction(Intent.ACTION_VIEW);
				intent3.setData(uri);
				intent3.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent3);
			} catch (ActivityNotFoundException e) {
				Toast.makeText(Test.this, " 启动'地图'异常!\n请检查是否安装了该应用.", Toast.LENGTH_SHORT).show();
			}
			break;
		// 编辑短信(调用发送短信程序)
		case R.id.Button4:
			Toast.makeText(Test.this, "正在打开短信,请稍后...", Toast.LENGTH_SHORT).show();
			Intent intent4 = new Intent(Intent.ACTION_VIEW); // 创建一个带Action属性的intent
			intent4.setType("vnd.android-dir/mms-sms");
			startActivity(intent4);
			break;
		// 查看联系人
		case R.id.Button5:
			Toast.makeText(Test.this, "正在启动联系人,请稍后...", Toast.LENGTH_SHORT).show();
			Intent intent5 = new Intent(Intent.ACTION_VIEW, ContactsContract.Contacts.CONTENT_URI);
			startActivity(intent5);
			break;
		// 打开相机
		case R.id.Button6:
			Toast.makeText(Test.this, "正在启动相机,请稍后...", Toast.LENGTH_SHORT).show();
			Intent intent6 = new Intent();
			intent6.setAction(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA); // 启动相机app
			startActivity(intent6);
			break;
		// 打开图库
		case R.id.Button7:
			Toast.makeText(Test.this, "正在打开图库,请稍后...", Toast.LENGTH_SHORT).show();
			Intent intent7 = new Intent();
			intent7.setType("image/*");
			intent7.setAction(Intent.ACTION_GET_CONTENT);
			startActivity(intent7);
			break;
		// 打开计算器
		case R.id.Button8:
			Toast.makeText(Test.this, "正在启动计算器,请稍后...", Toast.LENGTH_SHORT).show();
			Intent intent8 = new Intent();
			intent8.setClassName("com.android.calculator2", "com.android.calculator2.Calculator"); // 调用setClassName指定了启动哪个应用程序
			startActivity(intent8);
			break;
		// 打开系统设置
		case R.id.Button9:
			Intent intentSet = new Intent(Settings.ACTION_SETTINGS);
			startActivity(intentSet);
			break;
		// 打开时钟
		case R.id.Button10:
			try {
				Intent intentclock = new Intent();
				intentclock.setClassName("com.android.deskclock", "com.android.deskclock.DeskClock");
				startActivity(intentclock);
			} catch (ActivityNotFoundException e) {
				Toast.makeText(Test.this, " 启动'时钟'异常!\n请检查是否安装了该应用.", Toast.LENGTH_SHORT).show();
			}
			break;
		// 打开文件管理器
		case R.id.Button11:
			try {
				Intent intentFile = new Intent();
				intentFile.setAction(Intent.ACTION_VIEW);
				intentFile.setType("text/plain");
				startActivity(intentFile);
			} catch (ActivityNotFoundException e) {
				Toast.makeText(Test.this, " 启动'文件管理器'异常!\n请检查是否安装了该应用.", Toast.LENGTH_SHORT).show();
			}
			break;
		// 打开QQ
		case R.id.Button12:
			try {
				Toast.makeText(Test.this, "正在打开QQ聊天工具,请稍后...", Toast.LENGTH_SHORT).show();
				Intent intent12 = new Intent();
				intent12.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.SplashActivity");
				intent12.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent12);
			} catch (ActivityNotFoundException e) {
				Toast.makeText(Test.this, " 启动'QQ'异常!\n请检查是否安装了该应用.", Toast.LENGTH_SHORT).show();
			}
			break;
		// 打开微信
		case R.id.Button13:
			try {
				Toast.makeText(Test.this, "正在启动微信客户端,请稍后...", Toast.LENGTH_SHORT).show();
				Intent intent13 = new Intent();
				intent13.setClassName("com.tencent.mm", "com.tencent.mm.ui.LauncherUI");
				intent13.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(intent13);
			} catch (ActivityNotFoundException e) {
				Toast.makeText(Test.this, " 启动'微信'异常!\n请检查是否安装了该应用.", Toast.LENGTH_SHORT).show();
			}
			break;
		// 重启手机
		case R.id.Button14:
			String cmd = "su -c reboot";
			try {
				Toast.makeText(Test.this, "正在重启手机,请稍后...", Toast.LENGTH_SHORT).show();
				Runtime.getRuntime().exec(cmd);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				new AlertDialog.Builder(Test.this).setTitle("Error").setMessage(e.getMessage())
						.setPositiveButton("OK", null).show();
			}
			break;
		default:
			break;
		}
	}
}

源码下载


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值