<?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>