android的常用方法,后续会一直增加

来源:互联网 发布:淘宝店家身份查询 编辑:程序博客网 时间:2024/05/14 23:20

简单的xml布局文件:

<RelativeLayout 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"    tools:context=".MainActivity">    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="打开网页"        android:id="@+id/onClickWeb"        android:onClick="onClickWeb" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="打开拨号面板"        android:id="@+id/onClickCallPhone"        android:onClick="onClickCallPhone"        android:layout_below="@+id/onClickWeb" />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="拨打电话"        android:id="@+id/callPhone"        android:onClick="callPhone"        android:layout_below="@+id/onClickCallPhone"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="打开发短信面板"        android:id="@+id/onClickSendMessage"        android:onClick="onClickSendMessage"        android:layout_below="@+id/callPhone"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="发短信"        android:id="@+id/onClickMessage"        android:onClick="onClickMessage"        android:layout_below="@+id/onClickSendMessage"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="播放多媒体"        android:id="@+id/media"        android:onClick="media"        android:layout_below="@+id/onClickMessage"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="卸载程序"        android:id="@+id/uninstall"        android:onClick="uninstall"        android:layout_below="@+id/media"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="安装程序"        android:id="@+id/install"        android:onClick="install"        android:layout_below="@+id/uninstall"/></RelativeLayout>

相应的实现方法:

package com.example.zhushuyong.myintent;import android.content.Intent;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import java.io.File;public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    /**     * 打开网页     * @param v     */    public void onClickWeb(View v){        Uri data=Uri.parse("http://www.jeecms.com");        String action= Intent.ACTION_VIEW;        Intent intent=new Intent(action,data);        startActivity(intent);    }    /**     * 打开拨号面板     * @param v     */    public void onClickCallPhone(View v){        Uri data=Uri.parse("tel:123456789");        String action=Intent.ACTION_DIAL;        Intent intent=new Intent(action,data);        startActivity(intent);    }    /**     * 拨打电话     * @param v     */    public void callPhone(View v){        Uri data=Uri.parse("tel:123456789");        String action=Intent.ACTION_CALL;        Intent intent=new Intent(action,data);        startActivity(intent);    }    /**     * 发送短信面板     * @param v     */    public void onClickSendMessage(View v){        Intent intent=new Intent();        String action=Intent.ACTION_VIEW;        intent.setAction(action);        intent.putExtra("sms_body", "你今天吃了吗");        intent.setType("vnd.android-dir/mms-sms");        startActivity(intent);    }    /**     * 发送短信     * @param v     */    public void onClickMessage(View v){        Uri data=Uri.parse("smsto:123456789");        String action=Intent.ACTION_SENDTO;        Intent intent=new Intent(action,data);        intent.putExtra("sms_body", "一起吃个饭呗");        startActivity(intent);    }    /**     * 播放多媒体     * @param v     */    public void media(View v){        Uri data=Uri.parse("file:///sdcard/Music/Wiz Khalifa;Charlie Puth-See You Again.mp3");        String action=Intent.ACTION_VIEW;        Intent intent=new Intent();        intent.setAction(action);        intent.setDataAndType(data, "audio/mp3");        startActivity(intent);    }    /**     * 卸载程序     * @param v     */    public void uninstall(View v){        Uri data=Uri.parse("package:包名");        String action=Intent.ACTION_DELETE;        Intent intent=new Intent(action,data);        startActivity(intent);    }    /**     * 安装程序     * @param v     */    public void install(View v){        Uri data=Uri.fromFile(new File("sd卡路径"));        String action=Intent.ACTION_VIEW;        Intent intent=new Intent();        intent.setAction(action);        intent.setDataAndType(data,"application/vnd.android.package-archive");        startActivity(intent);    }}

布局图片
这里写图片描述

0 0
原创粉丝点击