Java tutorial
/* * Copyright (c) 2017 Ni YueMing<niyueming@163.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 net.nym.utilslibrary.utils; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.support.v4.content.FileProvider; import android.webkit.MimeTypeMap; import java.io.File; import java.util.Locale; /** * @author niyueming * @date 2017-05-26 * @time 17:14 */ public class IntentUtils { public static void callPhone(Context context, String phone) { Intent intent = new Intent(Intent.ACTION_VIEW); Uri data = Uri.parse("tel:" + phone); intent.setData(data); context.startActivity(intent); } public static void callView(Context context, Uri uri) { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(uri); context.startActivity(intent); } public static void callSMS(Context context, String phone) { Intent intent = new Intent(Intent.ACTION_VIEW); Uri data = Uri.parse("sms:" + phone); intent.setData(data); context.startActivity(intent); } public static void openQQChat(Context context, String qq) { try { String url = String.format("mqqwpa://im/chat?chat_type=wpa&uin=%s", qq); context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); } catch (Exception e) { e.printStackTrace(); Toaster.toaster(context, "?QQ"); } } public static void openWeixinChat(Context context, String weixin) { try { // String url=String.format("weixin://addfriend/%s",weixin); String url = String.format("weixin://contacts/profile/%s", weixin); context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); } catch (Exception e) { e.printStackTrace(); Toaster.toaster(context, "?"); } } public static void openApp(Context context, String packageName, String cls) { try { ComponentName componet = new ComponentName(packageName, cls); Intent intent = new Intent(); intent.setComponent(componet); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } catch (Exception e) { e.printStackTrace(); Toaster.toaster(context, "?APP"); } } public static void openFile(Context context, File file, String authority) { try { Intent intent = new Intent(Intent.ACTION_VIEW); Uri data; // 7.0 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { // "cn.com.firstcare.mobile.aph.fileprovider"???authorities data = FileProvider.getUriForFile(context, authority, file); // ? intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); } else { data = Uri.fromFile(file); } intent.setDataAndType(data, FileUtils.getMimeType(file)); Logger.e("getMimeType=%s", FileUtils.getMimeType(file)); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(intent); } catch (Exception e) { e.printStackTrace(); Toaster.toaster(context, "??app"); } } public static void openActivity(Context context, String packageName, String cls) { try { ComponentName componet = new ComponentName(packageName, cls); Intent intent = new Intent(); intent.setComponent(componet); context.startActivity(intent); } catch (Exception e) { e.printStackTrace(); } } }