总结安卓(Android)中常用的跳转工具
程序员文章站
2024-03-07 19:27:21
话不多说了,直接上代码,这篇文章包含了一些基本的并且常用的跳转工具,一起来看看吧。
首先,这是需要的对应的权限。
话不多说了,直接上代码,这篇文章包含了一些基本的并且常用的跳转工具,一起来看看吧。
首先,这是需要的对应的权限。
<uses-permission android:name="android.permission.call_phone" /> <uses-permission android:name="android.permission.send_sms" /> <uses-permission android:name="android.permission.read_external_storage" />
代码:
import android.app.activity; import android.content.context; import android.content.intent; import android.database.cursor; import android.net.uri; import android.os.environment; import android.provider.mediastore; import android.provider.settings; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.toast; import java.io.file; import frame.zmit.cn.publicutils.r; /** * created by joy on 2016/7/21. * 工具集合 */ public class mainactivity extends appcompatactivity { private string mimagepath;//返回的图片路径 private final static int request_code_gallery = 0x11; private final static int request_code_camera = 0x12; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); /** * 打开微信 */ findviewbyid(r.id.btn_open_weixin).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = getpackagemanager().getlaunchintentforpackage("com.tencent.mm"); startactivity(intent); } }); /** * 跳转到qq聊天界面 */ findviewbyid(r.id.btn_open_qq).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string mqqnumber = "98*****08"; string url11 = "mqqwpa://im/chat?chat_type=wpa&uin=" + mqqnumber + "&version=10"; startactivity(new intent(intent.action_view, uri.parse(url11))); } }); /** * 跳转到联系人界面 */ findviewbyid(r.id.btn_people_interface).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(); intent.setclassname("com.android.contacts", "com.android.contacts.activities.peopleactivity"); startactivity(intent); } }); /** * 跳转到拨号界面 */ findviewbyid(r.id.btn_telephone_interface).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string phone = "157*****737"; intent intent = new intent(intent.action_dial, uri.parse("tel:" + phone)); intent.setflags(intent.flag_activity_new_task); startactivity(intent); } }); /** * 直接拨打电话 */ findviewbyid(r.id.btn_telephone).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string phone = "157*****737"; intent intentphone = new intent(intent.action_call, uri.parse("tel:" + phone)); startactivity(intentphone); } }); /** * 跳转到短信界面 */ findviewbyid(r.id.btn_message_interface).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(intent.action_view); intent.settype("vnd.android-dir/mms-sms"); startactivity(intent); } }); /** * 跳转到短信界面 * 指定号码和短信内容 */ findviewbyid(r.id.btn_message).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { string phone = "157*****737"; string message = "这是一个自定义的内容"; intent intent = new intent(intent.action_sendto, uri.parse("smsto:" + phone)); intent.putextra("sms_body", message); startactivity(intent); } }); /** * 跳转到设置界面 */ findviewbyid(r.id.btn_setting_interface).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(settings.action_settings); startactivity(intent); } }); /** * 跳转到蓝牙界面 */ findviewbyid(r.id.btn_bluetooth_interface).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(settings.action_bluetooth_settings); startactivity(intent); } }); /** * 调用浏览器 */ findviewbyid(r.id.btn_browser).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(); intent.setaction("android.intent.action.view"); uri content_url = uri.parse("http://www.baidu.com"); intent.setdata(content_url); startactivity(intent); } }); /** * 打开照相机 */ findviewbyid(r.id.btn_camera).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { mimagepath = chooseimagefromcamera(mainactivity.this, request_code_camera, "cameraimage"); } }); /** * 打开相册并获取图片地址 */ findviewbyid(r.id.btn_gallery).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(intent.action_pick, null); intent.setdataandtype(mediastore.images.media.external_content_uri, "image/*"); startactivityforresult(intent, request_code_gallery); } }); } public string chooseimagefromcamera(activity activity, int requestcode, string imagename) { intent cameraintent = new intent(android.provider.mediastore.action_image_capture); file file = new file(environment.getexternalstoragedirectory() + "/images"); if (!file.exists()) { file.mkdirs(); } string savepath = environment.getexternalstoragedirectory() + "/images/" + imagename + string.valueof(system.currenttimemillis()) + ".jpg"; uri muri = uri.fromfile( new file(savepath)); cameraintent.putextra(android.provider.mediastore.extra_output, muri); cameraintent.putextra("return-data", true); activity.startactivityforresult(cameraintent, requestcode); return savepath; } public string onactivityresultforchooseimagefromgallery( context context, int requestcode, int resultcode, intent data) { string imagepath = null; if (data != null) { if ("file".equals(data.getdata().getscheme())) { /** 有些低版本机型返回的uri模式为file*/ imagepath = data.getdata().getpath(); } else { uri selectedimage = data.getdata(); string[] filepathcolumn = {mediastore.images.media.data}; cursor cursor = context.getcontentresolver().query(selectedimage, filepathcolumn, null, null, null); cursor.movetofirst(); int columnindex = cursor.getcolumnindex(filepathcolumn[0]); imagepath = cursor.getstring(columnindex); cursor.close(); } return imagepath; } else { return null; } } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); switch (requestcode) { case request_code_gallery: mimagepath = onactivityresultforchooseimagefromgallery(mainactivity.this, requestcode, resultcode, data); if (mimagepath != null && mimagepath.length() > 0) { toast.maketext(mainactivity.this, mimagepath, toast.length_short).show(); } break; case request_code_camera: if (mimagepath != null && mimagepath.length() > 0) { toast.maketext(mainactivity.this, mimagepath, toast.length_short).show(); } break; } } }
总结
总结安卓(android)中常用的跳转工具到这就结束了,这篇文章整理的这些跳转工具类很常见,所以才是最实用的,希望本文对大家日常开发android能有所帮助。