如何获取Android设备ID?

4
如何获取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);
    }
}

可能是是否存在唯一的Android设备ID?的重复问题。 - Kartik Shandilya
上下文是当前类的实例,在其中您将调用此方法。 - Deepak Rana
2
@DeepakRana 不是这样的。正在运行的是Activity、Service或Application,而不是类。如果只是这样,你会使用this。 - Gabe Sechan
如果您是新手,请先从教程网站或Android开发者网站学习基础知识。在发布问题之前,请先搜索相关问题。 - Rajesh N
1个回答

3
像这样为您的应用程序创建上下文:
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的更多信息,请参考此链接


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接