android_9 Intent 基础

mail.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存" />
     <Button
        android:id="@+id/submit"
        android:layout_width="wrap_content"
package com.mhm.button.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ButtonDemoActivity extends Activity {
    /** Called when the activity is first created. */
	
	private Button btn_save;
	
	private Button btn_submit;
	
	private Button btn_call;
	
	private Button btn_sms;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn_save = (Button)findViewById(R.id.save);
        btn_submit = (Button)findViewById(R.id.submit);
        btn_call = (Button)findViewById(R.id.btn_call);
        btn_sms = (Button)findViewById(R.id.btn_sms);
        
        btn_save.setOnClickListener(listener);
        btn_submit.setOnClickListener(listener);
        btn_call.setOnClickListener(listener);
        btn_sms.setOnClickListener(listener);
    }
    
    private OnClickListener listener = new OnClickListener() {
		public void onClick(View v) {
			Button b = (Button)v;
			switch (b.getId()) {
			case R.id.save:
				Toast t = Toast.makeText(getApplicationContext(), "保存成功~", Toast.LENGTH_SHORT);
				t.setGravity(Gravity.CENTER_VERTICAL, 0, -100);
				t.show();
				break;
				
			case R.id.submit:
				t = Toast.makeText(getApplicationContext(), "提交成功~", Toast.LENGTH_SHORT);
				t.setGravity(Gravity.CENTER_VERTICAL, 0, 100);
				t.show();
				break;
				
			case R.id.btn_call:
				Intent intent = new Intent();
				intent.setAction(Intent.ACTION_CALL);
				intent.setData(Uri.parse("tel:187********"));
				startActivity(intent);
				break;
				
			case R.id.btn_sms:
				intent = new Intent();
				intent.setAction(Intent.ACTION_SENDTO);
				intent.setData(Uri.parse("smsto:5554"));
				intent.putExtra("sms_body", "Hello world");
				startActivity(intent);
				break;
				
			default:
				t = Toast.makeText(getApplicationContext(), "按钮成功~", Toast.LENGTH_SHORT);
				t.setGravity(Gravity.CENTER_VERTICAL, 0, 200);
				t.show();
				break;
			}
		}
	};
}
         android:layout_height="wrap_content"
        android:text="提交" />
     <Button
        android:id="@+id/btn_call"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CALL" />
     <Button
        android:id="@+id/btn_sms"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="MESSAGE" />

</LinearLayout>

 

拨电话

intent.setData(Uri.parse("tel:187********"));

发消息

intent.putExtra("sms_body", "Hello world");

 

 

使拨号和发短信能通过android的允许

在AndroidManifest.xml中添加,是application的节点下面添加。

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

 

 

 

你可能感兴趣的:(android)