android常用函数

package com.cqytjr.util;

import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.WindowManager;

/**
 * app处理工具类
 * 
 * @author chenliang
 * @version v1.0
 * @date 2014-2-20
 */
public class AppUtil {
	/**
	 * 描述:安装APK
	 * 
	 * @param context
	 *            the context
	 * @param file
	 *            apk文件路径
	 */
	public static void installApk(Context context, File file) {
		Intent intent = new Intent();
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.setAction(android.content.Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(file),
				"application/vnd.android.package-archive");
		context.startActivity(intent);
	}

	/**
	 * 描述:卸载程序.
	 * 
	 * @param context
	 *            the context
	 * @param packageName
	 *            要卸载程序的包名
	 */
	public static void uninstallApk(Context context, String packageName) {
		Intent intent = new Intent(Intent.ACTION_DELETE);
		Uri packageURI = Uri.parse("package:" + packageName);
		intent.setData(packageURI);
		context.startActivity(intent);
	}

	/**
	 * 获取版本号
	 * 
	 * @param context
	 * @return
	 */
	public static int getVersionCode(Context context) {
		try {
			return context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionCode;
		} catch (NameNotFoundException e) {
			LogUtil.error("获取版本号失败");
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 获取版本名
	 * 
	 * @param context
	 * @return
	 */
	public static String getVersionName(Context context) {
		try {
			return context.getPackageManager().getPackageInfo(
					context.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
			LogUtil.error("获取版本名失败");
			e.printStackTrace();
		}
		return "";
	}

	/**
	 * 获取设备的IMEI/设备号
	 * 
	 * @param context
	 * @return IMEI/设备号
	 */
	public static String getIMEI(Activity context) {
		TelephonyManager tm = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return tm.getDeviceId();
	}

	/**
	 * 保存屏幕常亮
	 * 
	 * @param context
	 */
	public static void keepLight(Activity activity) {
		// 保持屏幕常亮
		activity.getWindow().addFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	/**
	 * 获取本机IP地址
	 */
	public static String getLocalIpToString() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface
					.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf
						.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			Log.e("WifiPreference IpAddress", ex.toString());
		}
		return null;
	}

	/**
	 * 获取本机电话号码
	 * 
	 * @param context
	 * @return
	 */
	public static String getPhone_num(Context context) {
		String phone = ((TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE)).getLine1Number();
		return phone;
	}

	/**
	 * Role:Telecom service providers获取手机服务商信息 <BR>
	 * 需要加入权限<uses-permission
	 * android:name="android.permission.READ_PHONE_STATE"/> <BR>
	 */
	public static String getProvidersName(Context context) {
		String ProvidersName = null;
		// 返回唯一的用户ID;就是这张卡的编号神马的
		String IMSI = ((TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE)).getSubscriberId();
		// IMSI号前面3位460是国家,紧接着后面2位00 02是中国移动,01是中国联通,03是中国电信。
		System.out.println(IMSI);
		if (IMSI.startsWith("46000") || IMSI.startsWith("46002")) {
			ProvidersName = "中国移动";
		} else if (IMSI.startsWith("46001")) {
			ProvidersName = "中国联通";
		} else if (IMSI.startsWith("46003")) {
			ProvidersName = "中国电信";
		}
		return ProvidersName;
	}

	/**
	 * 获取本机MAC地址
	 * 
	 * @param context
	 * @return
	 */
	public static String getMac(Context context) {
		String macAddress = null;
		WifiManager wifiMgr = null;
		Object obj = context.getSystemService(Context.WIFI_SERVICE);
		if (null != obj) {
			wifiMgr = (WifiManager) obj;
		}
		WifiInfo info = (null == wifiMgr ? null : wifiMgr.getConnectionInfo());
		if (null != info) {
			macAddress = info.getMacAddress();
		}
		return macAddress;
	}
}

  

转载于:https://www.cnblogs.com/hualuoshuijia/p/10137713.html

### 关于 Android Studio 中 SPI 的常用函数及其用法 在 Android 开发中,Service Provider Interface (SPI) 是一种用于实现插件化架构的设计模式。它允许开发者通过接口定义功能,并由具体的类来实现这些功能。虽然 Android 官方并未提供专门针对 SPI 的工具集[^1],但在 Java 和 Kotlin 生态系统中,可以通过 `java.util.ServiceLoader` 来加载服务提供商实例。 以下是常见的 SPI 使用场景及相关方法: #### 1. **核心 API** `java.util.ServiceLoader` 是 Java 提供的核心类,用于动态加载实现了特定接口的服务提供者。其主要方法如下: - **load(Class<S> service)** 加载指定接口的所有实现类。此方法会扫描 classpath 下的 `META-INF/services/` 文件夹中的配置文件并返回对应的实现类对象集合[^2]。 - **iterator()** 返回一个迭代器,可以遍历所有已加载的服务提供者实例。 - **reload()** 刷新当前 ServiceLoader 实例的状态,重新读取配置文件并加载新的服务提供者。 #### 2. **典型代码示例** 以下是一个简单的例子展示如何使用 `ServiceLoader` 动态加载服务提供者: ```java import java.util.ServiceLoader; public class SpiExample { public static void main(String[] args) { // 创建 ServiceLoader 并加载 MyInterface 接口的实现 ServiceLoader<MyInterface> loader = ServiceLoader.load(MyInterface.class); // 遍历所有的实现类 for (MyInterface impl : loader) { System.out.println("调用了:" + impl.execute()); } } } // 定义接口 interface MyInterface { String execute(); } ``` 假设我们在项目的 `META-INF/services/com.example.MyInterface` 文件中指定了两个实现类路径,则上述代码将会依次调用这两个实现类的方法。 --- #### 3. **内存监控与优化建议** 尽管 SPI 不直接涉及性能问题,但如果 SPI 被大量使用可能导致内存泄漏或 GC 过于频繁的情况。此时可借助 Android 工具链进行分析: - **Memory Monitor**: 可视化应用运行期间的内存变化情况,帮助识别是否存在异常增长。 - **DDMS Heap Tool**: 手动触发垃圾回收操作以便更精确地定位潜在问题。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值