Framwork之各种Manager

Framwork之各种Manager_第1张图片

 

一,PackageManager,

      可以获取系统应用的相关信息,也可以获取当app的应用名字,包名,应用图标,权限versionName,vertionCode等各种消息.

1,获取本应用的相关信息:

try {
    PackageManager pm = this.getPackageManager();
    PackageInfo info = pm.getPackageInfo(this.getPackageName(), 0);
    mVersionName = info.versionName;
    mVersionCode = info.versionCode;
    ApplicationInfo applicationInfo = pm.getApplicationInfo(getPackageName(), 0);
    mApplicationName = (String) pm.getApplicationLabel(applicationInfo);
    mDrawable = applicationInfo.loadIcon(pm);

} catch (PackageManager.NameNotFoundException e) {
    e.printStackTrace();
    showToast("未发现版本号");
}
StringBuilder sb = new StringBuilder();
String[] appPremission = new PackageManagerUtil(this).getAppPremission(getPackageName());
for (int i = 0; i < appPremission.length; i++) {

    sb.append(appPremission[i]).append(",           ");

}
mBinding.permission.setText("权限 :     "+sb.toString());
mBinding.applicationName.setText("应用名字 :     " + new PackageManagerUtil(this).getAppName(getPackageName()));
mBinding.versionName.setText("versionName :     "+new PackageManagerUtil(this).getAppVersion(getPackageName()));
mBinding.packageName.setText("包名        :     " +this.getPackageName());
mBinding.icon.setImageDrawable(new PackageManagerUtil(this).getAppIcon(getPackageName()));

Log.e("date","权限 :   "+sb.toString());
Log.e("date","应用签名 :   "+new PackageManagerUtil(this).getAppSignature(getPackageName()));

2,获取系统应用的相关信息

private ArrayList  mlistAppInfo = new ArrayList();
// 获得所有启动Activity的信息,类似于Launch界面
public void queryAppInfo() {
    PackageManager pm = this.getPackageManager(); // 获得PackageManager对象
    Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
    // 通过查询,获得所有ResolveInfo对象.
    List resolveInfos = pm.queryIntentActivities(mainIntent, PackageManager.MATCH_DEFAULT_ONLY);
    // 调用系统排序 , 根据name排序
    // 该排序很重要,否则只能显示系统应用,而不能列出第三方应用程序
    Collections.sort(resolveInfos, new ResolveInfo.DisplayNameComparator(pm));
    if (mlistAppInfo != null) {
        mlistAppInfo.clear();
        for (ResolveInfo reInfo : resolveInfos) {
            String activityName = reInfo.activityInfo.name; // 获得该应用程序的启动Activity的name
            String pkgName = reInfo.activityInfo.packageName; // 获得应用程序的包名
            String appLabel = (String) reInfo.loadLabel(pm); // 获得应用程序的Label
            Drawable icon = reInfo.loadIcon(pm); // 获得应用程序图标
            // 为应用程序的启动Activity 准备Intent
            Intent launchIntent = new Intent();
            launchIntent.setComponent(new ComponentName(pkgName,
                    activityName));
            // 创建一个AppInfo对象,并赋值
            AppInfo appInfo = new AppInfo();
            appInfo.setAppLabel(appLabel);
            appInfo.setPkgName(pkgName);
            appInfo.setAppIcon(icon);
            appInfo.setIntent(launchIntent);
            mlistAppInfo.add(appInfo); // 添加至列表中
            Log.e("date","系统应用的appLabel是:   "+appLabel);
            Log.e("date","系统应用的pkgName是:   "+pkgName);
            Log.e("date","系统应用的图标是:   "+icon);
            Log.e("date","启动系统应用的Intent是:   "+launchIntent);
        }
    }
}

2.1,跳转至改系统应用

Intent intent = mlistAppInfo.get(position).getIntent();
startActivity(intent);

 

二,ActivityManager可以获取当前进程的名字,uid,pid

 

 

mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List runningAppProcesseList = mActivityManager.getRunningAppProcesses();
for (int i = 0; i < runningAppProcesseList.size(); i++) {
    ActivityManager.RunningAppProcessInfo processInfo = runningAppProcesseList.get(i);
    int pid = processInfo.pid;
    int uid = processInfo.uid;
    String processName = processInfo.processName;

    int[] memoryPid = new int[pid];
    Debug.MemoryInfo[] processMemoryInfo = mActivityManager.getProcessMemoryInfo(memoryPid);
    //int totalPss = processMemoryInfo[0].getTotalPss();

    Log.e("date","pid是" + pid);
    Log.e("date","uid是" + uid);
    Log.e("date","processName是" + processName);
    //Log.e("date","totalPss是" + totalPss);
    mBinding.pid.setText("pid是:    "+pid);
    mBinding.uid.setText("uid是:    "+uid);
    mBinding.processName.setText("processName是:    "+processName);
}

三,通过WifiManager获取mac地址

四,通过TelephonyManager获取device_id

public class HappyDeviceInfoUtil {

    public static String getDeviceInfo(Context context) {
        try {
            org.json.JSONObject json = new org.json.JSONObject();
            android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            String device_id = null;
            if (checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
                device_id = tm.getDeviceId();
            }
            String mac = getMac(context);

            json.put("mac", mac);
            if (TextUtils.isEmpty(device_id)) {
                device_id = mac;
            }
            if (TextUtils.isEmpty(device_id)) {
                device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
                        android.provider.Settings.Secure.ANDROID_ID);
            }
            json.put("device_id", device_id);
            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String getMac(Context context) {
        String mac = "";
        if (context == null) {
            return mac;
        }
        if (Build.VERSION.SDK_INT < 23) {
            mac = getMacBySystemInterface(context);
        } else {
            mac = getMacByJavaAPI();
            if (TextUtils.isEmpty(mac)){
                mac = getMacBySystemInterface(context);
            }
        }
        return mac;

    }

    @TargetApi(9)
    private static String getMacByJavaAPI() {
        try {
            Enumeration interfaces = NetworkInterface.getNetworkInterfaces();
            while (interfaces.hasMoreElements()) {
                NetworkInterface netInterface = interfaces.nextElement();
                if ("wlan0".equals(netInterface.getName()) || "eth0".equals(netInterface.getName())) {
                    byte[] addr = netInterface.getHardwareAddress();
                    if (addr == null || addr.length == 0) {
                        return null;
                    }
                    StringBuilder buf = new StringBuilder();
                    for (byte b : addr) {
                        buf.append(String.format("%02X:", b));
                    }
                    if (buf.length() > 0) {
                        buf.deleteCharAt(buf.length() - 1);
                    }
                    return buf.toString().toLowerCase(Locale.getDefault());
                }
            }
        } catch (Throwable e) {
        }
        return null;
    }

    private static String getMacBySystemInterface(Context context) {
        if (context == null) {
            return "";
        }
        try {
            WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (checkPermission(context, Manifest.permission.ACCESS_WIFI_STATE)) {
                WifiInfo info = wifi.getConnectionInfo();
                return info.getMacAddress();
            } else {
                return "";
            }
        } catch (Throwable e) {
            return "";
        }
    }

    public static boolean checkPermission(Context context, String permission) {
        boolean result = false;
        if (context == null) {
            return result;
        }
        if (Build.VERSION.SDK_INT >= 23) {
            try {
                Class clazz = Class.forName("android.content.Context");
                Method method = clazz.getMethod("checkSelfPermission", String.class);
                int rest = (Integer) method.invoke(context, permission);
                if (rest == PackageManager.PERMISSION_GRANTED) {
                    result = true;
                } else {
                    result = false;
                }
            } catch (Throwable e) {
                result = false;
            }
        } else {
            PackageManager pm = context.getPackageManager();
            if (pm.checkPermission(permission, context.getPackageName()) == PackageManager.PERMISSION_GRANTED) {
                result = true;
            }
        }
        return result;
    }

}

}

 

五,NotificationManager创建通知栏

private NotificationManager mNotificationManager;

/** * 创建通知栏 */
private void setNotification() { 
    if (mNotificationManager == null) {
         mNotificationManager = (NotificationManager)                 
            getSystemService(Context.NOTIFICATION_SERVICE); 

         mBuilder = new NotificationCompat.Builder(this);//CHANNEL
         mBuilder.setContentTitle("开始下载") //设置通知标题 
        .setContentText("正在连接服务器")//设置通知内容 
        .setSmallIcon(R.mipmap.logo) //设置小图标             
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.logo))     
        .setOngoing(true) 
        .setAutoCancel(true) 
        .setWhen(System.currentTimeMillis()); //显示当前时间 
        mNotificationManager.notify(NOTIFY_ID, mBuilder.build());
    }
}

 

/**
 * 销毁时清空一下对notify对象的持有
 */
@Override
public void onDestroy() {
    mNotificationManager = null;
    super.onDestroy();
}

 

 

 

你可能感兴趣的:(Android实用代码)