android小知识(2)

1、Android启动外部应用

(1)、根据包名启动

PackageManager manager = getPackageManager();

Intent launchIntentForPackage = manager.getLaunchIntentForPackage("要打开软件的包名");

if (launchIntentForPackage != null) {

    startActivity(launchIntentForPackage);

}

(2)、启动应用的某个界面

Intent intent = new Intent();

intent.addCategory(Intent.CATEGORY_LAUNCHER);

intent.setAction(Intent.ACTION_MAIN);

intent.setComponent(new ComponentName(“包名”, "界面的类名");

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

startActivity(intent);

2、打开软件安装页面

Intent intent = new Intent();

intent.setAction("android.intent.action.VIEW");

intent.addCategory("android.intent.category.DEFAULT");

intent.setDataAndType(Uri.fromFile(new File("apk所在路径")), "application/vnd.android.package-archive");

startActivityForResult(intent, 0);

3、卸载软件

Intent intent = new Intent();

intent.setAction("android.intent.action.DELETE");

intent.addCategory("android.intent.category.DEFAULT");

intent.setData(Uri.parse("package:" + "卸载的包名"));

startActivityForResult(intent, 0);

4、打开软件详情页

Intent intent = new Intent();

intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");

intent.setData(Uri.parse("package:" + "软件包名"));

startActivity(intent);

5、分享内容

Intent intent = new Intent();

intent.setAction("android.intent.action.SEND");

intent.addCategory("android.intent.category.DEFAULT");

intent.setType("text/plain");

intent.putExtra(Intent.EXTRA_TEXT, "分享的内容");

startActivity(intent);

6、动态获取服务是否开启的方法

public static boolean isRunningService(String className,Context context){

    ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    List runningServices = activityManager.getRunningServices(300);//300表示正在运行的服务的上限个数,一般300足够了

    //遍历集合

    for (RunningServiceInfo runningServiceInfo : runningServices) {

        ComponentName service = runningServiceInfo.service;

        //正在运行的服务的全类名

        String className2 = service.getClassName();

        //正在运行的服务的全类名和传递过来的服务的全类名比较,相同正在运行返回true,不相同表示服务没有运行返回false

        if (className.equals(className2)) {

        return true;

        }

    }

    return false;

}

7、获取内存卡可用空间

public static long getAvailableROM(){

    //获取内存路径

    File path = Environment.getDataDirectory();

    //硬盘的API操作

    StatFs stat = new StatFs(path.getPath());

    long blockSize = stat.getBlockSize();//获取每块的大小

    long totalBlocks = stat.getBlockCount();//获取总块数

    long availableBlocks = stat.getAvailableBlocks();//获取可用的块数

    return availableBlocks*blockSize;

}

8、获取SD卡可用空间

public static long getAvailableSD(){

    File path = Environment.getExternalStorageDirectory();

    StatFs stat = new StatFs(path.getPath());

    long blockSize = stat.getBlockSize();//每块的大小

    long totalBlocks = stat.getBlockCount();//总块数

    long availableBlocks = stat.getAvailableBlocks();//可用的块数

    return availableBlocks*blockSize;

}

9、获取剩余内存和总内存

public static long getAvailableRam(Context context){

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    MemoryInfo outInfo = new MemoryInfo();

    am.getMemoryInfo(outInfo);

    //空闲的内存

    return outInfo.availMem;

}

public static long getTotalRam(Context context){

    ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);

    MemoryInfo outInfo = new MemoryInfo();

    am.getMemoryInfo(outInfo);

    //总的内存

    return outInfo.totalMem;

}

10、获取手机屏幕高度和宽度

WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

DisplayMetrics outMetrics = new DisplayMetrics();

windowManager.getDefaultDisplay().getMetrics(outMetrics);

width = outMetrics.widthPixels;

height = outMetrics.heightPixels;

11、判断网络活动状态

public static boolean isNetworkConnected(Context context) {

    if (context != null) {

        ConnectivityManager mConnectivityManager = (ConnectivityManager) context

        .getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();

        if (mNetworkInfo != null) {

        return mNetworkInfo.isAvailable();

        }

    }

    return false;

}


android小知识(2)_第1张图片
欢迎您扫一扫上面的微信公众号,订阅我的个人公众号! 本公众号将以推送Android各种碎片化小知识或小技巧,以及整理Android面试知识点为主,也会不定期将开发老司机日常工作中踩过的坑,平时自学的一些知识总结出来进行分享。每天一点干货小知识把你的碎片时间充分利用起来。

你可能感兴趣的:(android小知识(2))