Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 總結安卓(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能有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved