2017-09-02 110 views
3

如何获取android设备ID?我不知道什么是“背景”。如何获取android设备ID?

import android.content.Context; 
import android.provider.Settings; 

public class getDeviceID { 

    public void getAndroidID(Context context) { 
     String android_id= Settings.System.getString(context.getContentResolver(), 
       Settings.Secure.ANDROID_ID); 

     System.out.println(android_id); 
    } 
} 
+0

的[可能的复制有一个独特的Android设备ID?](https://stackoverflow.com/questions/2785485/is -athere-a-unique-android-device-id) –

+0

上下文是当前类的实例,您将在其中调用此方法。 –

+2

@DeepakRana不,它不是。它正在运行的活动,服务或应用程序。不是班级。如果完全是这样,你会使用它。 –

回答

0

为你的应用程序是这样

private Context context = this; 

将这个系里面的onCreate

创建上下文
String android_id = Settings.Secure.getString(context.getContentResolver(), 
      Settings.Secure.ANDROID_ID); 

    Toast.makeText(context, "android_id= " + android_id, Toast.LENGTH_LONG).show(); 

而且更多地了解context参考this link