1、实现效果
2、布局
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上网"
android:id="@+id/web"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨号"
android:id="@+id/dial"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发送短信"
android:id="@+id/mess"/>
</LinearLayout>
3、java代码
package com.qf.intentapplication;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
public class WebActivity extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.weblayout);
Button bt=findViewById(R.id.web);
Button bt_dial=findViewById(R.id.dial);
Button bt_message=findViewById(R.id.mess);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.setAction("android.intent.action.VIEW");
intent.setData(Uri.parse("http://www.baidu.com"));
startActivity(intent);
}
});
bt_dial.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.setAction("android.intent.action.DIAL");
intent.setData(Uri.parse("tel:12345678910"));
startActivity(intent);
}
});
bt_message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent();
intent.setAction(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:12345678910"));
intent.putExtra("sms_body","helloworld");
startActivity(intent);
}
});
}
}