Android开发

总结安卓(Android)中常用的跳转工具

本文主要是介绍总结安卓(Android)中常用的跳转工具,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

话不多说了,直接上代码,这篇文章包含了一些基本的并且常用的跳转工具,一起来看看吧。

首先,这是需要的对应的权限。

<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能有所帮助。

这篇关于总结安卓(Android)中常用的跳转工具的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!