作用:可以将 intent 理解成 activity 之间的粘合剂,不管是自己建的activity还是自带的(比如电话activity)。Intent是一个将要执行的动作的抽象的描述,一般来说是作为参数来使用,由 Intent来协助完成 Android各个组件之间的通讯。
先看一下运行截图:
点击哪一个按钮对应开启相对功能。
直接上代码:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bj"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="150dp"
android:textSize="25dp"
android:background="@drawable/bj2"
android:textColor="@color/colorAccent"
android:text="
已开启老人模式 "/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:background="@drawable/shape"
android:text="电话"
android:textColor="@color/colorAccent"
android:layout_marginBottom="8dp"
android:id="@+id/firsta"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="短信"
android:textColor="@color/colorAccent"
android:background="@drawable/shape"
android:layout_marginBottom="8dp"
android:id="@+id/firstb"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="百度"
android:textColor="@color/colorAccent"
android:background="@drawable/shape"
android:id="@+id/firstc"/>
<Button
android:layout_width="match_parent"
android:layout_marginBottom="8dp"
android:layout_height="wrap_content"
android:text="邮箱"
android:textColor="@color/colorAccent"
android:background="@drawable/shape"
android:id="@+id/firstd"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="地图"
android:textColor="@color/colorAccent"
android:background="@drawable/shape"
android:id="@+id/firste"/>
<Button
android:layout_width="match_parent"
android:layout_marginBottom="8dp"
android:layout_height="wrap_content"
android:text="多媒体"
android:textColor="@color/colorAccent"
android:background="@drawable/shape"
android:id="@+id/firstf"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="图片"
android:textColor="@color/colorAccent"
android:background="@drawable/shape"
android:id="@+id/firsth"/>
<Button
android:layout_marginBottom="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="拍照"
android:textColor="@color/colorAccent"
android:background="@drawable/shape"
android:id="@+id/firsti"/>
<Button
android:layout_width="match_parent"
android:layout_marginBottom="8dp"
android:layout_height="wrap_content"
android:text="安装设备"
android:textColor="@color/colorAccent"
android:background="@drawable/shape"
android:id="@+id/firstj"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:text="系统设置"
android:textColor="@color/colorAccent"
android:background="@drawable/shape"
android:id="@+id/firstk"/>
</LinearLayout>
</ScrollView>
package com.example.intentapp;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import java.io.File;
public class MainActivity extends AppCompatActivity {
private Button buttona;
private Button buttonb;
private Button buttonc;
private Button buttond;
private Button buttone;
private Button buttonf;
private Button buttonh;
private Button buttoni;
private Button buttonj;
private Button buttonk;
private Button buttonl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttona = (Button) findViewById(R.id.firsta);
buttonb = (Button) findViewById(R.id.firstb);
buttonc = (Button) findViewById(R.id.firstc);
buttond = (Button) findViewById(R.id.firstd);
buttone = (Button) findViewById(R.id.firste);
buttonf = (Button) findViewById(R.id.firstf);
buttonh = (Button) findViewById(R.id.firsth);
buttoni = (Button) findViewById(R.id.firsti);
buttonj = (Button) findViewById(R.id.firstj);
buttonk = (Button) findViewById(R.id.firstk);
setListeners();
}
public void setListeners(){
OnClick onClick=new OnClick();
buttona.setOnClickListener(onClick);
buttonb.setOnClickListener(onClick);
buttonc.setOnClickListener(onClick);
buttond.setOnClickListener(onClick);
buttone.setOnClickListener(onClick);
buttonf.setOnClickListener(onClick);
buttonh.setOnClickListener(onClick);
buttoni.setOnClickListener(onClick);
buttonj.setOnClickListener(onClick);
buttonk.setOnClickListener(onClick);
}
private class OnClick implements View.OnClickListener{
@Override
public void onClick(View v) {
Intent intent = null;
Uri uri;
switch (v.getId()){
case R.id.firsta:
//跳转到login
// 调用拨打电话,给10010拨打电话
uri = Uri.parse("tel:17660946113");
intent = new Intent(Intent.ACTION_DIAL, uri);
break;
case R.id.firstb:
//跳转到login
// 调用拨打电话,给10010拨打电话
// 给10010发送内容为“Hello”的短信
uri = Uri.parse("smsto:10010");
intent = new Intent(Intent.ACTION_SENDTO, uri);
intent.putExtra("sms_body", "Hello");
break;
case R.id.firstc:
// 打开百度主页
uri = Uri.parse("https://www.baidu.com");
intent = new Intent(Intent.ACTION_VIEW, uri);
break;
case R.id.firstd:
//发邮箱
uri = Uri.parse("mailto:someone@domain.com");
intent = new Intent(Intent.ACTION_SENDTO, uri);
break;
case R.id.firste:
// 打开Google地图中国北京位置(北纬39.9,东经116.3)
uri = Uri.parse("geo:39.9,116.3");
intent = new Intent(Intent.ACTION_VIEW, uri);
break;
case R.id.firstf:
intent = new Intent(Intent.ACTION_VIEW);
uri = Uri.parse("file:///sdcard/foo.mp3");
intent.setDataAndType(uri, "audio/mp3");
uri = Uri.withAppendedPath(MediaStore.Audio.Media.INTERNAL_CONTENT_URI, "1");
intent = new Intent(Intent.ACTION_VIEW, uri);
break;
case R.id.firsth:
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
break;
case R.id.firsti:
// 打开拍照程序
intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
break;
case R.id.firstj:
String fileName = Environment.getExternalStorageDirectory() + "/myApp.apk";
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(fileName)),
"application/vnd.android.package-archive");
break;
case R.id.firstk:
// 进入系统设置界面
intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
startActivity(intent);
break;
}
startActivity(intent);
}
}
}