第一个应用:一键打电话

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    //命名空间
    package="com.itheima.callhoney"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.CALL_PHONE"/>    //注意这里:添加了电话的权限

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itheima.callhoney.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

res/layout/activity_main.xml

<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:onClick="callhoney"
        android:text="打电话"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    
</RelativeLayout>

 

MainActivity.java

package com.itheima.callhoney;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
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 callhoney(View v){
        
        // 不同的组件之间 ,相互 调用的时候, 激活的时候, 一般采用的是 意图 --- intent
        // 意图 包含 你具体要  做 什么 
        // 动作:  做
        // 数据:  什么 
        // 例如:  泡 茶 , 泡网吧, 泡 妞, 泡 面
        
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_CALL);  //动作 , 打电话 
        intent.setData(Uri.parse("tel://5201314"));  // 数据, 给 谁打 电话
        
        //发送意图 出去, 告诉 系统我要打电话. 
        startActivity(intent);
        
    }
    
}

 

posted on 2017-11-22 10:54  maogefff  阅读(197)  评论(0编辑  收藏  举报

导航