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" />
结果如下:
主页面:
点击隐式启动后跳转到第二个页面
点击发送短信跳转到发送短信页面
点击拨号按钮跳转到系统拨号界面
点击电话按钮跳转到通话界面
点击打开百度网页按钮跳转到百度网站
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android Studio Intent隐式启动,发短信,拨号,打电话,访问网页等实例代码全部内容,希望文章能够帮你解决Android Studio Intent隐式启动,发短信,拨号,打电话,访问网页等实例代码所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)