天天看點

android 中顯示意圖和隐式意圖(附帶實作打電話)

未來的我,希望能看到這篇文章的時候,想一想什麼都走過來了,還有什麼走不得呢,無論何時何地,不要停下學習的腳步。

<a href="https://s5.51cto.com/oss/201711/11/461170c0ad6c5b0f25d599e3fc9f6200.png-wh_500x0-wm_3-wmp_4-s_186349798.png" target="_blank"></a>

<a href="https://s5.51cto.com/oss/201711/11/507e56dd973ce26d09d057be6005afa8.png-wh_500x0-wm_3-wmp_4-s_900754730.png" target="_blank"></a>

mainactivity篇

package com.example.qingdan;

import android.os.Bundle;

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.view.Menu;

import android.view.View;

public class MainActivity extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

public  void click1(View v) {

//【1】建立意圖對象

Intent intent =new Intent();

//【2】設定撥打電話的動作

intent.setAction(Intent.ACTION_CALL);

//【3】設定撥打的資料

intent.setData(Uri.parse("tel:"+119));

//【4】開啟Activity

startActivity(intent);

//隐式意圖

public void click2(View v){

//[1]建立意圖對象

Intent intent=new Intent();

//[2]設定跳轉的動作

intent.setAction("com.iteheima.comm");

//設定category

intent.addCategory("android.intent.category.DEFAULT");

//設定資料

//intent.setData(Uri.parse("itheima:"+110));

//設定資料類型

//intent.setType("aa/bb");//調用setdate會自動清除setdata

//是以出現此種情況,我們應該調用如下程式設計

intent.setDataAndType(Uri.parse("itheima:"+110), "aa/bb");//itheima是data中已經配置好了的,是以上述同理隻能用‘tel’

//開啟activity

//顯示意圖

//點選按鈕跳轉到 TestActivity 

public void click3(View v) {

//[1]建立意圖對象   意圖就是我要完成一件事 

Intent intent = new Intent(this,test3activity.class);

//[2]設定包名和類名  packageName:目前應用的包名

//intent.setClassName("com.itheima.newactivity", "com.itheima.newactivity.Test3Activity");

 //[3]開啟Activity  

 startActivity(intent);

test3activity篇

public class test3activity extends Activity{

// TODO 自動生成的方法存根

//加載布局

setContentView(R.layout.activity_test);

testactivity篇

public class testactivity extends Activity{

//當Activity

layout——activitymain篇

&lt;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" &gt;

    &lt;Button

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:onClick="click1"

    android:text="撥打電話"/&gt;

    android:onClick="click2"

    android:text="跳轉到activity"/&gt;

    android:onClick="click3"

&lt;/LinearLayout&gt;

activitymain——test3篇

&lt;?xml version="1.0" encoding="utf-8"?&gt;

    android:orientation="vertical" &gt;

    &lt;TextView 

        android:layout_width="match_parent"

    android:text="wo shi test"

        /&gt;

activitymain-test篇

    &lt;TextView

        android:layout_height="wrap_content"

        android:text="我是testActivity333" /&gt;

android manifest篇

&lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.qingdan"

    android:versionCode="1"

    android:versionName="1.0" &gt;

    &lt;uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" /&gt;

    &lt;uses-permission android:name="android.permission.CALL_PHONE"/&gt;

    &lt;application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" &gt;

        &lt;activity

            android:name="com.example.qingdan.MainActivity"

              android:icon="@drawable/head1"

            android:label="我是第二個頁面" &gt;

            &lt;intent-filter&gt;

                &lt;action android:name="android.intent.action.MAIN" /&gt;

                &lt;category android:name="android.intent.category.LAUNCHER" /&gt;

            &lt;/intent-filter&gt;

        &lt;/activity&gt;

            android:name="com.example.qingdan.testactivity"

             android:icon="@drawable/head2"

            android:label="我是第一個頁面" &gt;

                               《!----》主入口》

                &lt;action android:name="com.iteheima.comm" /&gt;

                &lt;data android:scheme="itheima"

                    android:mimeType="aa/bb"

                    /&gt;

                &lt;category android:name="android.intent.category.DEFAULT" /&gt;

            《!我們可以建立出多個過濾器,隻要,前面name,category能比對到就行了,有一個就行哪怕data改變也可以》

         &lt;!--配置Activity3  --&gt;

        &lt;activity android:name="com.example.qingdan.test3activity"&gt;&lt;/activity&gt;

    &lt;/application&gt;

&lt;/manifest&gt;

      本文轉自眉間雪 51CTO部落格,原文連結:http://blog.51cto.com/13348847/1980916,如需轉載請自行聯系原作者

繼續閱讀