其它一些不常用的工具方法OtherHelper

来源:互联网 发布:java面试宝典软件 编辑:程序博客网 时间:2024/06/04 18:50

OtherHelper

打开微信、直接分享到微信、随机颜色、判断应用是否在后台等

import java.util.List;import java.util.Random;import android.app.Activity;import android.app.ActivityManager;import android.app.ActivityManager.RunningAppProcessInfo;import android.app.ActivityManager.RunningTaskInfo;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.graphics.drawable.Drawable;import android.net.Uri;import android.text.Html;import android.widget.TextView;public class OtherHelper {    private OtherHelper util;    public OtherHelper getInstance() {        if (util == null) {            util = new OtherHelper();        }        return util;    }    private OtherHelper() {        super();    }    /**     * 打开微信     *      * @param context     * @param url     * @return     */    public boolean openWeChat(Context context, String url) {        if (PhoneHelper.getInstance().isInstall("com.tencent.mm")) {            try {                Intent localIntent = new Intent("android.intent.action.VIEW");                localIntent.setData(Uri.parse(url));                localIntent.setPackage("com.tencent.mm");                context.startActivity(localIntent);                if (context instanceof Activity)                    ((Activity) context).overridePendingTransition(                            android.R.anim.slide_in_left,                            android.R.anim.slide_out_right);                return true;            } catch (Exception e) {                e.printStackTrace();                return false;            }        }        return false;    }    /**     * 直接分享到微信     *      * @param context     * @param text     * @param subject     * @param title     * @param isFriend     *            是分享到好友还是朋友圈     */    public void shareToWeChat(Context context, String text, String subject,            String title, boolean isFriend) {        Intent intent = new Intent();        String str = "";        if (isFriend) {            str = "com.tencent.mm.ui.tools.ShareImgUI";        } else {            str = "com.tencent.mm.ui.tools.ShareToTimeLineUI";        }        ComponentName comp = new ComponentName("com.tencent.mm", str);        intent.setComponent(comp);        intent.setAction("android.intent.action.SEND");        intent.setType("text/*");        intent.putExtra(Intent.EXTRA_TEXT, text);        intent.putExtra(Intent.EXTRA_SUBJECT, subject);        intent.putExtra(Intent.EXTRA_TITLE, title);        // intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));        context.startActivity(intent);    }    /**     * 得到随机颜色     *      * @return     */    public String getRandomColor() {        String r, g, b;        Random random = new Random();        r = Integer.toHexString(random.nextInt(256)).toUpperCase();        g = Integer.toHexString(random.nextInt(256)).toUpperCase();        b = Integer.toHexString(random.nextInt(256)).toUpperCase();        r = r.length() == 1 ? "0" + r : r;        g = g.length() == 1 ? "0" + g : g;        b = b.length() == 1 ? "0" + b : b;        return "#" + r + g + b;    }    /**     * 在TextView中插入图片     *      * @param context     * @param tv     * @param rid     * @param size     */    public void textViewInImag(final Context context, TextView tv, int rid,            final int size) {        String imgStr = "<img src=\"" + rid + "\"/>";        Html.ImageGetter imageGetter = new Html.ImageGetter() {            public Drawable getDrawable(String arg0) {                int id = Integer.parseInt(arg0);                Drawable draw = context.getResources().getDrawable(id);                draw.setBounds(0, 0, size, size);                return draw;            }        };        tv.append(Html.fromHtml(imgStr, imageGetter, null));    }    /**     * 当前应用是否在后台运行     *      * @param context     * @return     */    public boolean isAppRunBackground(Context context) {        ActivityManager activityManager = (ActivityManager) context                .getSystemService(Context.ACTIVITY_SERVICE);        List<RunningAppProcessInfo> appProcesses = activityManager                .getRunningAppProcesses();        for (RunningAppProcessInfo appProcess : appProcesses) {            if (appProcess.processName.equals(context.getPackageName())) {                if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {                    return true;                } else {                    return false;                }            }        }        return false;    }    /**     * 当前应用是否在后台运行 需要权限<uses-permission     * android:name="android.permission.GET_TASKS" />     *      * @param context     * @return     */    public boolean isAppRunBackGround(final Context context) {        ActivityManager am = (ActivityManager) context                .getSystemService(Context.ACTIVITY_SERVICE);        List<RunningTaskInfo> tasks = am.getRunningTasks(1);        if (!tasks.isEmpty()) {            ComponentName topActivity = tasks.get(0).topActivity;            if (!topActivity.getPackageName().equals(context.getPackageName())) {                return true;            }        }        return false;    }    /**     * 后台服务是否在运行     *      * @param context     * @param cl     * @return     */    public boolean isServiceRunning(Context context, String serName) {        boolean isRunning = false;        ActivityManager activityManager = (ActivityManager) context                .getSystemService(Context.ACTIVITY_SERVICE);        List<ActivityManager.RunningServiceInfo> serviceList = activityManager                .getRunningServices(Integer.MAX_VALUE);        if (serviceList == null || serviceList.size() == 0) {            return false;        }        for (int i = 0; i < serviceList.size(); i++) {            if (serviceList.get(i).service.getClassName().equals(serName)) {                isRunning = true;                break;            }        }        return isRunning;    }    /**     * activity是否在运行     *      * @param context     * @param className     * @return     */    public boolean isActivityRunning(Context context, String className) {        ActivityManager activityManager = (ActivityManager) context                .getSystemService(Context.ACTIVITY_SERVICE);        List<ActivityManager.RunningTaskInfo> appList = activityManager                .getRunningTasks(1000);        for (ActivityManager.RunningTaskInfo running : appList) {            if (className.equals(running.baseActivity.getClassName())) {                return true;            }        }        return false;    }    /**     * 最近是否运行过App     * @param context     * @param packageName     * @return     */    public boolean isRecentAppRunning(Context context, String packageName) {        ActivityManager activityManager = (ActivityManager) context                .getSystemService(Context.ACTIVITY_SERVICE);        List<ActivityManager.RecentTaskInfo> appList = activityManager                .getRecentTasks(100, 1);        for (ActivityManager.RecentTaskInfo running : appList) {            if (running.origActivity.getPackageName().equals(packageName)) {                return true;            }        }        return false;    }}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 物金所倒闭投资怎么办 电商平台欺骗客户怎么办 pdf电脑打开是乱码怎么办 excel表格打开是乱码怎么办 win10安装软件出现乱码怎么办 华为手机速度越来越慢怎么办 oppo手机速度越来越慢怎么办 安卓手机速度越来越慢怎么办 青桔单车忘了锁怎么办 华为手机反应太慢了怎么办 魅族关机键失灵怎么办 oppa79手机开不开机怎么办 黑衣服洗完发白怎么办 白衣服被黑衣服染色了怎么办 评职称单位领导不推荐怎么办 支付宝被限制收款怎么办 在淘宝上下单想写两个地址怎么办 注销了的支付宝怎么办 狗狗黑色毛发红怎么办 蘑菇街直播间被禁言了怎么办 收了发票不付款怎么办 退款要先收发票怎么办 淘宝退款了又收到货怎么办 商家收货后拒绝退款怎么办 申请退货退款卖家不处理怎么办 淘宝买东西换货卖家不发货怎么办 淘宝自动默认付款没发货怎么办 支付宝支付失败可钱扣了怎么办 苹果nfc感应坏了怎么办 老鼠添过的盘子怎么办 ie浏览器页面显示网页错误怎么办 Ⅵvo手机声音小怎么办 小米手机预约错了怎么办 小米note二手没解锁怎么办 艾灸后脸色越黑怎么办 淘宝软件类目不能上架宝贝怎么办 ae中没有mpg格式怎么办 淘宝小二处理不公怎么办 遇到卖保险的人怎么办 租房合同没理家电清单怎么办 普雪油烟机坏了怎么办