Java tutorial
/* * Copyright (C) 2016 venshine.cn@gmail.com * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.hch.beautyenjoy.tools; import android.Manifest; import android.app.Activity; import android.app.SearchManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.provider.ContactsContract; import android.provider.MediaStore; import android.provider.Settings; import android.support.v4.app.ActivityCompat; import android.widget.Toast; /** * Intent? * * @author venshine */ public class IntentUtils { /** * * @param context */ public static void market(Context context) { Uri uri = Uri.parse("market://details?id=" + context.getPackageName()); Intent intent = new Intent(Intent.ACTION_VIEW, uri); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); if (componentName != null) { context.startActivity(intent); } else { Toast.makeText(context, "", Toast.LENGTH_SHORT).show(); } } /** * send email * * @param email email address */ public static void sendEmail(Context context, String email) { Intent data = new Intent(Intent.ACTION_SENDTO); data.setData(Uri.parse("mailto:" + email)); data.putExtra(Intent.EXTRA_SUBJECT, "[ " + AppTools.getApplicationName(context) + " ] " + "??"); ComponentName componentName = data.resolveActivity(context.getPackageManager()); if (componentName != null) { context.startActivity(data); } else { Toast.makeText(context, "", Toast.LENGTH_SHORT).show(); } } /** * Search a word in a browser */ public static void search(Context context, String string) { Intent intent = new Intent(Intent.ACTION_WEB_SEARCH); intent.putExtra(SearchManager.QUERY, string); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); if (componentName != null) { context.startActivity(intent); } } /** * Open url in a browser */ public static void openUrl(Context context, String url) { Uri uri = Uri.parse(url); Intent intent = new Intent(Intent.ACTION_VIEW, uri); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); if (componentName != null) { context.startActivity(intent); } } /** * Open map in a map app */ public static void openMap(Context context, String parh) { Uri uri = Uri.parse(parh); Intent intent = new Intent(Intent.ACTION_VIEW, uri); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); if (componentName != null) { context.startActivity(intent); } } /** * Open dial */ public static void openDial(Context context) { Intent intent = new Intent(Intent.ACTION_CALL_BUTTON); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); if (componentName != null) { context.startActivity(intent); } } /** * Open dial with a number */ public static void openDial(Context context, String number) { Uri uri = Uri.parse("tel:" + number); Intent intent = new Intent(Intent.ACTION_DIAL, uri); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); if (componentName != null) { context.startActivity(intent); } } /** * Call up, requires Permission "android.permission.CALL_PHONE" */ public static void call(Context context, String number) { Uri uri = Uri.parse("tel:" + number); Intent intent = new Intent(Intent.ACTION_CALL, uri); if (ActivityCompat.checkSelfPermission(context, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) { return; } // context.startActivity(intent); } /** * Send message */ public static void sendMessage(Context context, String sendNo, String sendContent) { Uri uri = Uri.parse("smsto:" + sendNo); Intent intent = new Intent(Intent.ACTION_SENDTO, uri); intent.putExtra("sms_body", sendContent); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); if (componentName != null) { context.startActivity(intent); } } /** * Open contact person */ public static void openContacts(Context context) { Intent intent = new Intent(Intent.ACTION_VIEW, ContactsContract.Contacts.CONTENT_URI); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); if (componentName != null) { context.startActivity(intent); } } /** * Open system settings */ public static void openSettings(Context context) { openSettings(context, Settings.ACTION_SETTINGS); } /** * Open system settings * * @param action The action contains global system-level device preferences. */ public static void openSettings(Context context, String action) { if (!StringUtils.isEmpty(action)) { Intent intent = new Intent(action); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); if (componentName != null) { context.startActivity(intent); } } else { openSettings(context); } } /** * Open camera */ public static void openCamera(Context context) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); ComponentName componentName = intent.resolveActivity(context.getPackageManager()); if (componentName != null) { context.startActivity(intent); } } /** * Take camera, this photo data will be returned in onActivityResult() */ public static void takeCamera(Activity activity, int requestCode) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); activity.startActivityForResult(intent, requestCode); } /** * Choose photo, this photo data will be returned in onActivityResult() */ public static void choosePhoto(Activity activity, int requestCode) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("image/*"); activity.startActivityForResult(intent, requestCode); } /** * Open App Detail page */ public static void openAppDetail(String packageName, Context context) { Intent intent = new Intent(); final int apiLevel = Build.VERSION.SDK_INT; if (apiLevel >= 9) { intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); Uri uri = Uri.fromParts("package", packageName, null); intent.setData(uri); } else { final String appPkgName = (apiLevel == 8 ? "pkg" : "com.android.settings.ApplicationPkgName"); intent.setAction(Intent.ACTION_VIEW); intent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails"); intent.putExtra(appPkgName, packageName); } context.startActivity(intent); } }