Intent调用系统组件

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

    <Button
        android:id="@+id/bt_web"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="网站" />

    <Button
        android:id="@+id/bt_map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="地图" />

    <Button
        android:id="@+id/bt_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="电话" />

    <Button
        android:id="@+id/bt_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="短信" />

    <Button
        android:id="@+id/bt_music"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="音乐" />
</LinearLayout>

package com.zdsoft.intent1201;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button bt_web, bt_map, bt_phone, bt_message, bt_music;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();

    }

    private void initView() {
        bt_web = (Button) findViewById(R.id.bt_web);
        bt_map = (Button) findViewById(R.id.bt_map);
        bt_phone = (Button) findViewById(R.id.bt_phone);
        bt_message = (Button) findViewById(R.id.bt_message);
        bt_music = (Button) findViewById(R.id.bt_music);
    }

    private void initListener() {
        bt_web.setOnClickListener(this);
        bt_map.setOnClickListener(this);
        bt_phone.setOnClickListener(this);
        bt_message.setOnClickListener(this);
        bt_music.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_web:
                //调用web浏览器
                Uri uri_web = Uri.parse("http://www.baidu.com");
                Intent intent_web = new Intent(Intent.ACTION_VIEW, uri_web);
                startActivity(intent_web);
                break;
            case R.id.bt_map:
                //打开地图
                Uri uri_map = Uri.parse("geo:113.671948,34.815454");
                Intent intent_map = new Intent(Intent.ACTION_VIEW, uri_map);
                startActivity(intent_map);
                break;
            case R.id.bt_phone:
                //拨打电话
                Uri uri_phone = Uri.parse("tel:10086");
                //调用拨号程序
                // Intent intent_phone=new Intent(Intent.ACTION_DIAL,uri_phone);
                Intent intent_phone = new Intent(Intent.ACTION_CALL, uri_phone);
                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent_phone);
                break;
            case R.id.bt_message:
                //发送短信
                Uri uri_message = Uri.parse("smsto:10086");
                Intent intent_message = new Intent(Intent.ACTION_SENDTO, uri_message);
                intent_message.putExtra("sms_body", "这是一条短信");
                startActivity(intent_message);
                break;
            case R.id.bt_music:
                //媒体播放
                Uri uri_music = Uri.parse("file:///sdcard/aaa.mp3");
                Intent intent_music = new Intent(Intent.ACTION_VIEW, uri_music);
                intent_music.setDataAndType(uri_music, "audio/mp3");
                startActivity(intent_music);
                break;
            default:
                break;
        }

    }


}

打电话需配置权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.zdsoft.intent1201">

    <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>
        </activity>
    </application>

</manifest>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值