您的位置:首页 > 移动开发 > Android开发

Android中Intent的用法介绍

2015-07-18 23:23 671 查看
1.调用系统自带浏览器

Uri uri = Uri.parse("http://www.baidu.com");

Intent intent = new Intent(Intent.ACTION_VIEW, uri);

intent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");

startActivity(intent);
添加权限:

</pre><pre name="code" class="java"><uses-permission android:name="android.permission.INTERNET"></uses-permission>


2.调用google搜索相应的内容

Intent intent = new Intent();
intent.setAction(Intent.ACTION_WEB_SEARCH);
intent.putExtra(SearchManager.QUERY, "search keywords");
startActivity(intent);


3.发短信(进入系统编辑短信界面)

Uri uri = Uri.parse("smsto:" + "13312966705");
Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "sms content");
startActivity(intent);


添加权限:

<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>


4.打电话

Uri uri = Uri.parse("tel:" + "13312966705");
Intent intent = new Intent(Intent.ACTION_DIAL, uri);
startActivity(intent);
添加权限:

<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>


5.卸载apk

Uri uri = Uri.fromParts("package", strPackageName, null);  
Intent intent = new Intent(Intent.ACTION_DELETE, uri);  
startActivity(intent);


6.安装apk
这里提供一个可行的函数,

private void installApp(Context context, File appFile) {
		// 创建URI
		Uri uri = Uri.fromFile(appFile);
		// 创建Intent意图
		Intent intent = new Intent(Intent.ACTION_VIEW);
		// 设置Uri和类型
		intent.setDataAndType(uri, "application/vnd.android.package-archive");
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		// 执行意图进行安装
		context.startActivity(intent);
	}
另外关于intent安装apk的例子文章大家可以参考这两篇文章

http://blog.csdn.net/johnsonblog/article/details/7581070

http://edison-cool911.iteye.com/blog/695077

7.Intent实现页面跳转与传递参数

传递参数并跳转

<span style="white-space:pre">		</span>Intent intent = new Intent();
                //Intent传递参数
                intent.putExtra("testIntent", "123");
                intent.setClass(FirstActivity.this, SecondActivity.class);
                FirstActivity.this.startActivity(intent);


接收参数

<span style="font-weight: bold; white-space: pre;">	</span>//使用Intent对象得到FirstActivity传递来的参数
        Intent intent = getIntent();
        String value = intent.getStringExtra("testIntent");


我写了以上代码的测试demo,需要的可以下载看看

源码

链接:http://pan.baidu.com/s/1i3ImBvJ 密码:h0ab
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: