Android基础——Intent的Action和Data属性
Posted zsbenn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android基础——Intent的Action和Data属性相关的知识,希望对你有一定的参考价值。
布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="打电话" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="发短信" /> </LinearLayout>
java调用
package com.example.myintentii; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button1 = (Button)findViewById(R.id.button1); Button button2 = (Button)findViewById(R.id.button2); //设置监听器对象 button1.setOnClickListener(listener); button2.setOnClickListener(listener); } View.OnClickListener listener = new View.OnClickListener() { @Override//点了一下按钮的监听器 public void onClick(View v) { Intent intent = new Intent(); Button button = (Button)v; switch (button.getId()){ case R.id.button1: //打电话 intent.setAction(intent.ACTION_DIAL);//设置打电话ACTION intent.setData(Uri.parse("tel:00000000"));//设置电话号 startActivity(intent); break; case R.id.button2: //发短信 intent.setAction(intent.ACTION_SENDTO); intent.setData(Uri.parse("smsto:11111111")); intent.putExtra("sms_body","Hello world");//编辑短信内容 startActivity(intent); break; } } }; }
以上是关于Android基础——Intent的Action和Data属性的主要内容,如果未能解决你的问题,请参考以下文章
android基础:Intents 和 intent-filter 的匹配规则
Android零基础入门第80节:Intent 属性详解(下)
Android 通过Intent调用系统功能和Action动作和服务广播大全
getActivity() 调用导致 RuntimeException:无法启动意图 Intent act=android.intent.action.MAIN