`
king_tt
  • 浏览: 2108363 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

【Android 开发教程】使用Intent调用内置应用程序

 
阅读更多

本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。

原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/


我们已经了解了如何在自己的单个应用中调用activity。但是,android开发中比较重要的一点,就是使用intent调用其他应用的activity。特别地,你的应用可以调用系统中的许多“内置”应用。所谓的“内置”应用,指的就是系同级别的应用,比如Browser,Phone,Sms等等。举个例子,如果你的应用需要打开一个网页,可以使用Intent对象去调用浏览器,浏览器把网页显示出来,而不是要自己创建一个浏览器。。。

下面的例子展示如何调用系统中的几个比较常用的“内置”应用。

1. 创建一个工程,Intents。

2. main.xml中的代码。

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <Button
  7. android:id="@+id/btn_webbrowser"
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:onClick="onClickWebBrowser"
  11. android:text="WebBrowser"/>
  12. <Button
  13. android:id="@+id/btn_makecalls"
  14. android:layout_width="fill_parent"
  15. android:layout_height="wrap_content"
  16. android:onClick="onClickMakeCalls"
  17. android:text="MakeCalls"/>
  18. <Button
  19. android:id="@+id/btn_showMap"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:onClick="onClickShowMap"
  23. android:text="ShowMap"/>
  24. <Button
  25. android:id="@+id/btn_launchMyBrowser"
  26. android:layout_width="fill_parent"
  27. android:layout_height="wrap_content"
  28. android:onClick="onClickLaunchMyBrowser"
  29. android:text="LaunchMyBrowser"/>
  30. </LinearLayout>
3. IntentsActivity.java中的代码。
  1. publicclassIntentsActivityextendsActivity{
  2. intrequest_Code=1;
  3. /**Calledwhentheactivityisfirstcreated.*/
  4. @Override
  5. publicvoidonCreate(BundlesavedInstanceState){
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.main);
  8. }
  9. publicvoidonClickWebBrowser(Viewview){
  10. Intenti=newIntent("android.intent.action.VIEW");
  11. i.setData(Uri.parse("http://www.amazon.com"));
  12. startActivity(i);
  13. }
  14. publicvoidonClickMakeCalls(Viewview){
  15. Intenti=newIntent(android.content.Intent.ACTION_DIAL,
  16. Uri.parse("tel:+651234567"));
  17. startActivity(i);
  18. }
  19. publicvoidonClickShowMap(Viewview){
  20. Intenti=newIntent(android.content.Intent.ACTION_VIEW,
  21. Uri.parse("geo:37.827500,-122.481670"));
  22. startActivity(i);
  23. }
  24. }
4. 调试。


程序启动之后:

点击WebBrowser按钮:

点击MakeCalls按钮:

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics