PackageManager manager = getPackageManager();
Intent launchIntentForPackage = manager.getLaunchIntentForPackage("要打开软件的包名");
if (launchIntentForPackage != null) {
startActivity(launchIntentForPackage);
}
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);
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);
Intent intent = new Intent();
intent.setAction("android.intent.action.DELETE");
intent.addCategory("android.intent.category.DEFAULT");
intent.setData(Uri.parse("package:" + "卸载的包名"));
startActivityForResult(intent, 0);
Intent intent = new Intent();
intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
intent.setData(Uri.parse("package:" + "软件包名"));
startActivity(intent);
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);
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;
}
//获取内存路径
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;
}
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;
}
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;
}
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
windowManager.getDefaultDisplay().getMetrics(outMetrics);
width = outMetrics.widthPixels;
height = outMetrics.heightPixels;
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各种碎片化小知识或小技巧,以及整理Android面试知识点为主,也会不定期将开发老司机日常工作中踩过的坑,平时自学的一些知识总结出来进行分享。每天一点干货小知识把你的碎片时间充分利用起来。