下面方法中包含一些常用的获取设备唯一标识符,网络状态,isp等信息。
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import com.unity3d.player.UnityPlayerActivity;
import com.unity3d.player.UnityPlayerNativeActivity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.provider.Settings.Secure;
import android.telephony.TelephonyManager;
import android.util.Log;
public class testss extends UnityPlayerActivity{
private String m_AndroidID;
private static final String TAG = "[PlugIn]";
// ==============================================
// Network
//
private static final int NetworkType_Invalid = 0;
private static final int NetworkType_Unknown = 1;
private static final int NetworkType_Mobile = 2;
private static final int NetworkType_Wifi = 3;
private static final int NetworkMobile_Unknown = 0;
private static final int NetworkMobile_2G = 1;
private static final int NetworkMobile_3G = 2;
private static final int NetworkMobile_4G = 3;
//获取MAC地址
public String GetMacAddress()
{
WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (wifiMan != null && wifiMan.getConnectionInfo() != null)
{
WifiInfo wifiInf = wifiMan.getConnectionInfo();
return wifiInf.getMacAddress();
}
return "";
}
//获取网络类型
public int GetNetworkType()
{
ConnectivityManager connectivityMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityMgr == null)
return NetworkType_Invalid;
NetworkInfo networkInfo = connectivityMgr.getActiveNetworkInfo();
if (networkInfo == null)
return NetworkType_Invalid;
switch (networkInfo.getType())
{
case ConnectivityManager.TYPE_MOBILE:
return NetworkType_Mobile;
case ConnectivityManager.TYPE_WIFI:
return NetworkType_Wifi;
default:
return NetworkType_Unknown;
}
}
/*
* GPRS 2G(2.5) General Packet Radia Service 114kbps EDGE 2G(2.75G) Enhanced
* Data Rate for GSM Evolution 384kbps UMTS 3G WCDMA 联通3G Universal Mobile
* Telecommunication System 完整的3G移动通信技术标准 CDMA 2G 电信 Code Division Multiple
* Access 码分多址 EVDO_0 3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution - Data Only
* (Data Optimized) 153.6kps - 2.4mbps 属于3G EVDO_A 3G 1.8mbps - 3.1mbps
* 属于3G过渡,3.5G 1xRTT 2G CDMA2000 1xRTT (RTT - 无线电传输技术) 144kbps 2G的过渡, HSDPA
* 3.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet Access 14.4mbps HSUPA
* 3.5G High Speed Uplink Packet Access 高速上行链路分组接入 1.4 - 5.8 mbps HSPA 3G
* (分HSDPA,HSUPA) High Speed Packet Access IDEN 2G Integrated Dispatch
* Enhanced Networks 集成数字增强型网络 (属于2G,来自维基百科) EVDO_B 3G EV-DO Rev.B 14.7Mbps
* 下行 3.5G LTE 4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡,升级版 LTE
* Advanced 才是4G EHRPD 3G CDMA2000向LTE 4G的中间产物 Evolved High Rate Packet Data
* HRPD的升级 HSPAP 3G HSPAP 比 HSDPA 快些
*/
public int GetNetworkMobileClass()
{
// only valid in mobile network
TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
if (tm == null)
return NetworkMobile_Unknown;
switch (tm.getNetworkType())
{
case TelephonyManager.NETWORK_TYPE_GPRS:
case TelephonyManager.NETWORK_TYPE_EDGE:
case TelephonyManager.NETWORK_TYPE_CDMA:
case TelephonyManager.NETWORK_TYPE_1xRTT:
case TelephonyManager.NETWORK_TYPE_IDEN:
return NetworkMobile_2G;
case TelephonyManager.NETWORK_TYPE_UMTS:
case TelephonyManager.NETWORK_TYPE_EVDO_0:
case TelephonyManager.NETWORK_TYPE_EVDO_A:
case TelephonyManager.NETWORK_TYPE_HSDPA:
case TelephonyManager.NETWORK_TYPE_HSUPA:
case TelephonyManager.NETWORK_TYPE_HSPA:
case TelephonyManager.NETWORK_TYPE_EVDO_B:
case TelephonyManager.NETWORK_TYPE_EHRPD:
case TelephonyManager.NETWORK_TYPE_HSPAP:
return NetworkMobile_3G;
case TelephonyManager.NETWORK_TYPE_LTE:
return NetworkMobile_4G;
default:
return NetworkMobile_Unknown;
}
}
// ================================================
// IMEI on GSM; MEID for CDMA
//
public String GetIMEIOrMEID()
{
// only valid in mobile network
TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
if (tm == null)
return null;
return tm.getDeviceId();
}
// ================================================
// ISP
public String GetISP()
{
TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
if (tm == null)
return "";
return tm.getNetworkOperator();
}
// ================================================
// Nation
public String GetNationISO()
{
TelephonyManager tm = (TelephonyManager) this.getSystemService(TELEPHONY_SERVICE);
if (tm == null)
return "";
return tm.getNetworkCountryIso();
}
// 获取谁被UDID
public String GetUDID()
{
if (m_AndroidID != null && !m_AndroidID.isEmpty())
return m_AndroidID;
m_AndroidID = Secure.getString(getContentResolver(), Secure.ANDROID_ID);
return m_AndroidID;
}
// 应用第一次安装的时间
public long Get1stInstallTime()
{
PackageManager packageManager = getPackageManager();
try
{
PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
return packageInfo.firstInstallTime;// 应用第一次安装的时间
}
catch (Exception e)
{
return 0;
}
}
//重新启动游戏
public void RestartPackage(int delayMS)
{
Log.i(TAG, "RestartPackage");
finish();
// Intent intent =
// getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// startActivity(intent);
if (delayMS < 0)
delayMS = 2000;
Intent intent = new Intent(this, UnityPlayerNativeActivity.class);
PendingIntent mypi = PendingIntent.getActivity(getApplicationContext(), 0, intent, Intent.FLAG_ACTIVITY_NEW_TASK);
AlarmManager al = (AlarmManager) getSystemService(ALARM_SERVICE);
al.set(AlarmManager.RTC, System.currentTimeMillis() + delayMS, mypi);
// System.exit(0);
// android.os.Process.killProcess(android.os.Process.myPid());
}
// ================================================
// IsRooted
//
public boolean IsRooted()
{
return _CheckBuildTag("test-keys") || _FindBinary("Test.apk");
}
static boolean _FindBinary(String binaryName)
{
String[] places = { "/system/app/", "/sbin/", "/system/bin/",
"/system/xbin/", "/data/local/xbin/", "/data/local/bin/",
"/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/" };
for (String where : places)
{
if (new File(where + binaryName).exists())
{
return true;
}
}
return false;
}
static boolean _CheckBuildTag(String tag)
{
String buildTag = android.os.Build.TAGS;
if (buildTag != null && buildTag.contains(tag))
return true;
return false;
}
// Turn GBK Code
public String Turn_GBK_URLCode(String strInput)
{
try
{
String str = URLEncoder.encode(strInput, "GBK");
return str;
}
catch (UnsupportedEncodingException e)
{
}
return "";
}
}
在Unity中常用到的一些安卓方法封装 | 拾荒者 http://www.shihuangzhe.org/?p=532