当前位置: 移动技术网 > IT编程>移动开发>Android > Android Studio Intent隐式启动,发短信,拨号,打电话,访问网页等实例代码

Android Studio Intent隐式启动,发短信,拨号,打电话,访问网页等实例代码

2019年07月24日  | 移动技术网IT编程  | 我要评论

中餐烹调技术,多大点事儿 金大,红山教育网

android studio intent隐式启动,发短信,拨号,打电话,访问网页等实例代码

功能

创建5个按钮,隐式启动、发短信、拨号按钮、电话按钮、打开网页按钮。通过使用intent来完成各自按钮下的功能

代码目录如下

详细代码如下:

activity_main.xml代码如下

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="horizontal"
  android:paddingbottom="@dimen/activity_vertical_margin"
  android:paddingleft="@dimen/activity_horizontal_margin"
  android:paddingright="@dimen/activity_horizontal_margin"
  android:paddingtop="@dimen/activity_vertical_margin"
  tools:context=".mainactivity">

  <linearlayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical">

    <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="发短信" />

    <button
      android:id="@+id/button3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="拨号按钮" />

    <button
      android:id="@+id/button4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="电话按钮" />
  </linearlayout>

  <linearlayout
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2">

    <button
      android:id="@+id/button5"
      android:layout_width="match_parent"
      android:layout_height="192dp"
      android:text="打开百度网页"
      />

  </linearlayout>


</linearlayout>

firstlayout.xml代码如下:

<?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">
  <edittext
    android:id="@+id/edittext1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="这是第二个界面"/>

</linearlayout>

mainactivity.java代码如下:

package com.example.administrator.chang;

import android.app.activity;
import android.content.intent;
import android.net.uri;
import android.os.bundle;
import android.view.view;
import android.widget.button;

public class mainactivity extends activity {

  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_main);

    button button1= (button) findviewbyid(r.id.button1);
    button1.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        intent intent =new intent("com.example.administrator.chang.action_start");
        startactivity(intent);
      }
    });
    //发短信
    button button2= (button) findviewbyid(r.id.button2);
    button2.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        intent intent = new intent();
        intent.setaction(intent.action_sendto);
        intent.setdata(uri.parse("smsto:10086"));
        intent.putextra("sms_body","the sms text");
        startactivity(intent);
      }
    });
    // 拨号
    button button3= (button) findviewbyid(r.id.button3);
    button3.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        intent intent =new intent();
        intent.setaction(intent.action_dial);
        intent.setdata(uri.parse("tel:10086"));
        startactivity(intent);
      }
    });
    //打电话
    button button4= (button) findviewbyid(r.id.button4);
    button4.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        intent intent=new intent();
        intent.setaction(intent.action_call);
        intent.setdata(uri.parse("tel:10086"));
        startactivity(intent);
      }
    });

    button button5= (button) findviewbyid(r.id.button5);
    button5.setonclicklistener(new view.onclicklistener() {
      @override
      public void onclick(view v) {
        intent intent=new intent();
        intent.setaction(intent.action_view);
        intent.setdata(uri.parse("http://www.baidu.com"));
        startactivity(intent);
      }
    });

  }


}

secondactivity.java代码如下:

package com.example.administrator.chang;

import android.app.activity;
import android.os.bundle;

/**
 * created by administrator on 2015/8/19.
 */
public class secondactivity extends activity {
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.firstlayout);
  }
}

androidmanifest.xml代码如下:

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

  <uses-permission android:name="android.permission.call_phone" />
  <application
    android:allowbackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/apptheme">

    <activity
      android:name=".mainactivity"
      android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.main" />

        <category android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
    <activity android:name=".secondactivity">
      <intent-filter>
        <action android:name="com.example.administrator.chang.action_start" />
        <category android:name="android.intent.category.default" />
      </intent-filter>
    </activity>
  </application>

</manifest>

说明 :打电话按钮中需要添加用户权限,具体代码部分是

<uses-permission android:name="android.permission.call_phone" />
发短信按钮我用的是intent.setaction(intent.action_sendto);如过短信直接能发送出去需要添加用户权限
android.permission.send_sms

在安卓中严格区分大小写。例如下面两段代码代表不同的意思:

<categoryandroid:name="android.intent.category.launcher" />

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

结果如下:

主页面:
这里写图片描述
点击隐式启动后跳转到第二个页面
这里写图片描述
点击发送短信跳转到发送短信页面
这里写图片描述
点击拨号按钮跳转到系统拨号界面
这里写图片描述
点击电话按钮跳转到通话界面
这里写图片描述
点击打开百度网页按钮跳转到百度网站
这里写图片描述

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网