我正在编写代码来获取任何版本高于 2.3 的 Android 设备的 MAC 地址。一般来说,我在 HiActivity.java 中编写了以下代码:
package com.hi.address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import android.app.Activity;
import android.os.Bundle;
public class HiActivity extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void Display()
{
try {
InetAddress addresses = InetAddress.getByName(InetAddress.getLocalHost().getHostName());
NetworkInterface ni= NetworkInterface.getByInetAddress(addresses);
byte[] mac = ni.getHardwareAddress();
System.out.println("====="+mac);
for(int i=0; i< mac.length; i++)
{
System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
}
}catch(Exception e){
System.out.println("exception was occureddddddddd"+e);
}
}
public void onStart(Bundle savedInstanceState) {
System.out.println(" 999999999999999999999999");
Display();
}
}
而不是在 Eclipse 中以调试模式给出任何输出 它在 LogCat 中给出以下内容
07-07 23:13:52.340: INFO/installd(228): move /data/dalvik-cache/data@[email protected]@classes.dex -> /data/dalvik-cache/data@[email protected]@classes.dex
07-07 23:13:52.340: DEBUG/PackageManager(284): New package installed in /data/app/com.hi.address-1.apk
07-07 23:13:52.470: WARN/PackageManager(284): Unable to load service info ResolveInfo{41789898 com.smlds.smlStartService p=0 o=0 m=0x108000}
07-07 23:13:52.470: WARN/PackageManager(284): org.xmlpull.v1.XmlPullParserException: No android.accounts.AccountAuthenticator meta-data
07-07 23:13:52.470: WARN/PackageManager(284): at android.content.pm.RegisteredServicesCache.parseServiceInfo(RegisteredServicesCache.java:377)
07-07 23:13:52.470: WARN/PackageManager(284): at android.content.pm.RegisteredServicesCache.generateServicesMap(RegisteredServicesCache.java:261)
07-07 23:13:52.470: WARN/PackageManager(284): at android.content.pm.RegisteredServicesCache$1.onReceive(RegisteredServicesCache.java:110)
07-07 23:13:52.470: WARN/PackageManager(284): at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:716)
07-07 23:13:52.470: WARN/PackageManager(284): at android.os.Handler.handleCallback(Handler.java:587)
07-07 23:13:52.470: WARN/PackageManager(284): at android.os.Handler.dispatchMessage(Handler.java:92)
07-07 23:13:52.470: WARN/PackageManager(284): at android.os.Looper.loop(Looper.java:132)
07-07 23:13:52.470: WARN/PackageManager(284): at com.android.server.ServerThread.run(SystemServer.java:682)
07-07 23:13:52.490: DEBUG/PackageManager(284): generateServicesMap(android.accounts.AccountAuthenticator): 8 services unchanged
07-07 23:13:52.500: DEBUG/AccountTypeManager(422): Registering external account type=com.sec.android.app.snsaccountlinkedin.account_type, packageName=com.sec.android.app.snsaccountli
07-07 23:13:52.510: DEBUG/PackageManager(284): generateServicesMap(android.content.SyncAdapter): 18 services unchanged
07-07 23:13:52.510: INFO/ApplicationPolicy(284): registerPackageChangeReceiver() onReceive: Broadcast Received for PKG:com.hi.address
07-07 23:13:52.520: INFO/DmAppInfo(284): getApplicationsList found row counts : 0 For Pkg : com.hi.address
07-07 23:13:52.520: DEBUG/szipinf(284): Initializing inflate state
07-07 23:13:52.520: DEBUG/GTalkService(399): [GTalkService.1] handlePackageInstalled: re-initialize providers
07-07 23:13:52.530: DEBUG/GTalkService(399): [RawStanzaProvidersMgr] ##### searchProvidersFromIntent
07-07 23:13:52.530: DEBUG/AccountTypeManager(422): Registering external account type=com.sec.android.app.snsaccountfacebook.account_type, packageName=com.sec.android.app.snsaccountfb
07-07 23:13:52.550: DEBUG/AccountTypeManager(422): Registering external account type=com.smlds.accountType, packageName=com.smlds
07-07 23:13:52.560: INFO/AccountTypeManager(422): Loaded meta-data for 6 account types, 2 accounts in 9ms
07-07 23:13:52.650: DEBUG/dalvikvm(422): GC_CONCURRENT freed 526K, 9% free 6904K/7559K, paused 2ms+3ms
07-07 23:13:52.690: DEBUG/dalvikvm(284): GC_EXPLICIT freed 1645K, 52% free 18542K/38151K, paused 7ms+10ms
07-07 23:13:52.700: DEBUG/AndroidRuntime(5253): Shutting down VM
07-07 23:13:52.700: INFO/AndroidRuntime(5253): NOTE: attach of thread 'Binder Thread #3' failed
07-07 23:13:52.710: DEBUG/dalvikvm(5253): GC_CONCURRENT freed 102K, 87% free 344K/2560K, paused 0ms+1ms
07-07 23:13:52.710: DEBUG/jdwp(5253): Got wake-up signal, bailing out of select
07-07 23:13:52.710: DEBUG/dalvikvm(5253): Debugger has detached; object registry had 1 entries
07-07 23:13:52.840: DEBUG/dalvikvm(362): GC_FOR_ALLOC freed 8092K, 43% free 13640K/23815K, paused 37ms
07-07 23:13:52.930: DEBUG/AndroidRuntime(5269): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-07 23:13:52.930: DEBUG/AndroidRuntime(5269): CheckJNI is OFF
07-07 23:13:52.930: DEBUG/AndroidRuntime(5269): Set to default setting_5 : language=-Duser.language=en propLang=en
07-07 23:13:52.930: ERROR/AndroidRuntime(5269): Set to default setting_6 : region=-Duser.region=US propRegn=US
07-07 23:13:52.940: DEBUG/AndroidRuntime(5269): setted country_code = UK & IRE
07-07 23:13:52.940: DEBUG/AndroidRuntime(5269): setted sales_code = XEU
07-07 23:13:52.940: DEBUG/AndroidRuntime(5269): found sales_code tag = <XEU>, </XEU>
07-07 23:13:52.950: DEBUG/dalvikvm(362): GC_FOR_ALLOC freed 4042K, 53% free 11324K/23815K, paused 33ms
07-07 23:13:53.050: DEBUG/LibQmg_native(5269): register_android_app_LibQmg
07-07 23:13:53.080: DEBUG/AndroidRuntime(5269): Calling main entry com.android.commands.am.Am
07-07 23:13:53.090: INFO/ActivityManager(284): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.hi.address/.HiActivity } from pid 5269
07-07 23:13:53.100: INFO/SurfaceFlinger(223): id=160(35) createSurface 0xa05bc for pid 284 (1x1),2 flag=0, Starting com.hi.address
07-07 23:13:53.100: DEBUG/WindowManager(284): addWindowToListInOrder() : current mSkipWidgetFocus='true'
07-07 23:13:53.110: DEBUG/AndroidRuntime(5269): Shutting down VM
07-07 23:13:53.120: INFO/AndroidRuntime(5269): NOTE: attach of thread 'Binder Thread #3' failed
07-07 23:13:53.120: DEBUG/dalvikvm(5269): GC_CONCURRENT freed 103K, 86% free 374K/2560K, paused 0ms+1ms
07-07 23:13:53.130: DEBUG/dalvikvm(362): GC_CONCURRENT freed 1169K, 51% free 11766K/23815K, paused 3ms+11ms
07-07 23:13:53.140: DEBUG/jdwp(5269): Got wake-up signal, bailing out of select
07-07 23:13:53.140: DEBUG/dalvikvm(5269): Debugger has detached; object registry had 1 entries
07-07 23:13:53.260: DEBUG/dalvikvm(362): GC_CONCURRENT freed 1359K, 50% free 12140K/23815K, paused 2ms+3ms
07-07 23:13:53.350: DEBUG/dalvikvm(362): GC_FOR_ALLOC freed 1308K, 49% free 12273K/23815K, paused 29ms
07-07 23:13:53.480: DEBUG/dalvikvm(362): GC_CONCURRENT freed 1354K, 47% free 12839K/23815K, paused 2ms+3ms
07-07 23:13:53.610: WARN/ActivityManager(284): Activity pause timeout for ActivityRecord{409bfaf0 com.android.launcher/com.android.launcher2.Launcher}
07-07 23:13:53.620: DEBUG/dalvikvm(362): GC_FOR_ALLOC freed 1603K, 45% free 13116K/23815K, paused 32ms
07-07 23:13:53.620: INFO/ActivityManager(284): Start proc com.hi.address for activity com.hi.address/.HiActivity: pid=5278 uid=10116 gids={}
07-07 23:13:53.700: INFO/System.out(5278): *********************************************************
07-07 23:13:53.700: DEBUG/WindowManager(284): addWindowToListInOrder() : current mSkipWidgetFocus='true'
07-07 23:13:53.710: VERBOSE/TLINE(5278): new: android.text.TextLine@4070ce08
07-07 23:13:53.710: INFO/SurfaceFlinger(223): id=161(1) createSurface 0x7f6dc for pid 5278 (1x1),2 flag=0, com.hi.address/com.hi.address.HiActivity
07-07 23:13:53.750: INFO/WindowManager(284): Putting input method here!
07-07 23:13:53.750: DEBUG/WindowManager(284): w.mAttrs.token=ActivityRecord{4173b240 com.hi.address/.HiActivity}
07-07 23:13:53.750: DEBUG/WindowManager(284): w.mToken=AppWindowToken{426016a0 token=ActivityRecord{4173b240 com.hi.address/.HiActivity}}
07-07 23:13:53.750: DEBUG/WindowManager(284): token=AppWindowToken{426016a0 token=ActivityRecord{4173b240 com.hi.address/.HiActivity}}
07-07 23:13:53.750: INFO/WindowManager(284): Placing input method @3
07-07 23:13:53.750: DEBUG/CLIPBOARD(284): Hide Clipboard dialog inside hideSoftInput() !
07-07 23:13:53.750: INFO/InputManagerService(284): [imme] in windowGainedFocus()j, viewHasFocus is false OR is TextEditor is false, now call hideCurrentInputLocked(InputMethodManager.HIDE_NOT_ALWAYS,null)
07-07 23:13:53.790: DEBUG/dalvikvm(362): GC_CONCURRENT freed 1501K, 43% free 13660K/23815K, paused 3ms+5ms
07-07 23:13:53.840: DEBUG/CLIPBOARD(5278): Hide Clipboard dialog at Starting input: finished by someone else... !
07-07 23:13:53.870: WARN/SocialHubWidget(1064): [SocialHubWidgetProvider] onReceive() >>
07-07 23:13:53.870: WARN/SocialHubWidget(1064): [SocialHubWidgetProvider] action >> action : sec.android.intent.action.HOME_PAUSE
07-07 23:13:53.870: WARN/SocialHubWidget(1064): [SocialHubWidgetProvider] action >> checkNetwork(context) : false
07-07 23:13:53.890: ERROR/com.samsung.app(1072): [MSC]>>> WeatherWidgetProvider.java:322 [0:0] onReceive()@@@ sec.android.intent.action.HOME_PAUSE
07-07 23:13:53.890: ERROR/com.samsung.app(1072): [MSC]>>> WidgetIdManager.java:39 [0:0] AccuWeatherClockWidgetID_Length
07-07 23:13:53.890: ERROR/com.samsung.app(1072): [MSC]>>> WidgetIdManager.java:40 [0:0] getPrefIDs() : length = 0
07-07 23:13:53.890: ERROR/com.samsung.app(1072): [MSC]>>> WeatherWidgetProvider.java:1554 [0:0] disable handler
07-07 23:13:53.900: ERROR/(1080): onReceive sec.android.intent.action.HOME_PAUSE
07-07 23:13:53.900: DEBUG/ProgramMonitor(4443): START onReceive()_intent : Intent { act=sec.android.intent.action.HOME_PAUSE flg=0x10 cmp=com.sec.android.widgetapp.programmonitorwidget/.ProgramMonitorProvider }
07-07 23:13:53.900: DEBUG/CLIPBOARD(362): Hide Clipboard dialog at Starting input: finished by someone else... !
07-07 23:13:53.910: INFO/PhotoAppWidgetProvider(1101): Gallery PhotoWidget PauseSlideShow
07-07 23:13:53.910: INFO/ActivityManager(284): Displayed com.hi.address/.HiActivity: +300ms
07-07 23:13:53.920: INFO/SurfaceFlinger(223): id=157 Removed com.android.launcher/com.android.launcher2.Launcher idx=1 Map Size=4
07-07 23:13:53.920: WARN/Surface(362): Not initializing the shared buffer client because mSurface=0x0 stats=0
我的代码错误在哪里?我尝试调试但无法找到我的错误。
/*getMAC() 方法返回 MAC 地址 */
public String getMAC() {
WifiManager wifiMan = (WifiManager)AppUtils.mContext.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
String macadd=wifiInf.getMacAddress();
System.out.println("MAC address info---- "+macadd);
if(macadd==null){
macadd="1A:DC:5C:8E:15:7B";
}
return macadd;
}
使用
android.telephony.TelephonyManager.getDeviceId()
获取设备的唯一标识符(GSM 上为 IMEI,CDMA 上为 MEID)。
同时在清单中设置
READ_PHONE_STATE
权限