Android學習筆記之使用意圖開啟內建應用程式程式組件

來源:互聯網
上載者:User

標籤:android學習筆記之使用意圖開啟內建


(1)布局檔案如下:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >    <Button        android:id="@+id/button1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:onClick="IntentTest"        android:text="從Google搜尋內容" />    <Button        android:id="@+id/button2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/button1"        android:layout_below="@+id/button1"        android:onClick="IntentTest"        android:text="遊覽網頁" />    <Button        android:id="@+id/button3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/button2"        android:layout_below="@+id/button2"        android:onClick="IntentTest"        android:text="顯示地圖" />    <Button        android:id="@+id/button4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/button3"        android:layout_below="@+id/button3"        android:onClick="IntentTest"        android:text="撥打到電話" />    <Button        android:id="@+id/button5"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/button4"        android:layout_centerVertical="true"        android:onClick="IntentTest"        android:text="發簡訊" />    <Button        android:id="@+id/button6"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/button5"        android:layout_below="@+id/button5"        android:onClick="IntentTest"        android:text="播放多媒體" />    <Button        android:id="@+id/button7"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignRight="@+id/button4"        android:layout_below="@+id/button6"        android:onClick="IntentTest"        android:text="安裝APK" />    <Button        android:id="@+id/button8"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignLeft="@+id/button7"        android:layout_below="@+id/button7"        android:onClick="IntentTest"        android:text="卸載APK" /></RelativeLayout>

(2)MainActivity.java

package com.example.intent_openinterapplicationapi;import android.app.Activity;import android.app.SearchManager;import android.content.Intent;import android.net.Uri;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {private Button button1;private Button button2;private Button button3;private Button button4;private Button button5;private Button button6;private Button button7;private Button button8;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button1 = (Button) this.findViewById(R.id.button1);button2 = (Button) this.findViewById(R.id.button2);button3 = (Button) this.findViewById(R.id.button3);button4 = (Button) this.findViewById(R.id.button4);button5 = (Button) this.findViewById(R.id.button5);button6 = (Button) this.findViewById(R.id.button6);button7 = (Button) this.findViewById(R.id.button7);button8 = (Button) this.findViewById(R.id.button8);}public void IntentTest(View v) {switch (v.getId()) {case R.id.button1:Intent intent = new Intent();intent.setAction(Intent.ACTION_WEB_SEARCH);intent.putExtra(SearchManager.QUERY, "searchString");startActivity(intent);break;case R.id.button2:Uri uri = Uri.parse("http://www.baidu.com");Intent intent2 = new Intent(Intent.ACTION_VIEW, uri);startActivity(intent2);break;case R.id.button3:Uri uri1 = Uri.parse("geo:38.899533,77.036476");Intent intent3 = new Intent(Intent.ACTION_VIEW, uri1);startActivity(intent3);break;case R.id.button4:Uri uri2 = Uri.parse("tel:18080808080");Intent intent4 = new Intent(Intent.ACTION_DIAL, uri2);startActivity(intent4);break;case R.id.button5:Intent intent5 = new Intent(Intent.ACTION_VIEW);intent5.putExtra("sms_body", "The SMS TEXT!");intent5.setType("vnd.android-dir/mms-sms");startActivity(intent5);break;case R.id.button6:Intent intent6 = new Intent(Intent.ACTION_VIEW);Uri uri3 = Uri.parse("file:///sdcard/song.mp3");intent6.setDataAndType(uri3, "audio/mp3");startActivity(intent6);break;case R.id.button7:Uri installUri = Uri.fromParts("package", "xxx", null);Intent intent7 = new Intent(Intent.ACTION_PACKAGE_ADDED);startActivity(intent7);break;case R.id.button8:Uri deletellUri = Uri.fromParts("package", "strPackageName", null);Intent intent8 = new Intent(Intent.ACTION_DELETE, deletellUri);startActivity(intent8);break;default:break;}}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}


Android學習筆記之使用意圖開啟內建應用程式程式組件

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.