简体   繁体   English

无法获取唯一的Android设备ID

[英]Can't get the unique Android device ID

Everyone is talking about this code: 每个人都在谈论这段代码:

import android.provider.Settings.Secure;

private String android_id = Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID);

When I write this, there will be error like: 当我写这篇文章时,会出现类似以下的错误:

"The method getContext() is undefined for the type Bridge" “对于类型Bridge,未定义方法getContext()”

How can I fix that? 我该如何解决?

这也应该解决问题,

String android_id = Secure.getString(this.getContentResolver(),Secure.ANDROID_ID);

Have you tried using getApplicationContext() instead of getContext()? 您是否尝试过使用getApplicationContext()而不是getContext()?

package de.vogella.android.deviceinfo;

    import android.app.Activity;
    import android.os.Bundle;
    import android.provider.Settings.Secure;
    import android.widget.Toast;

    public class ShowDeviceInfo extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            String deviceId = Secure.getString(this.getContentResolver(),
                    Secure.ANDROID_ID);
            Toast.makeText(this, deviceId, Toast.LENGTH_SHORT).show();
        }
    } 

http://blog.vogella.com/2011/04/11/android-unique-identifier/ http://blog.vogella.com/2011/04/11/android-unique-identifier/

These links might help: 这些链接可能有助于:

http://developer.android.com/reference/android/app/Activity.html http://developer.android.com/reference/android/app/Activity.html

getApplication() vs. getApplicationContext() getApplication()与getApplicationContext()

Difference between getContext() , getApplicationContext() , getBaseContext() , getContext(), and "this" getContext(),getApplicationContext(),getBaseContext(),getContext()和“ this”之间的区别

Firstly you should pass an instance of Context into your Bridge class. 首先,您应该将Context实例传递给Bridge类。
To achieve this, you can pass a Context to your constructor, or wherever you want. 为此,您可以将Context传递给构造函数,也可以传递给任何您想要的地方。

As you know, every method belongs to some class in Java. 如您所知,每个方法都属于Java中的某个类。 So when you call the method not from some object, like you do with getContext() , this is equal to calling this.getContext() . 因此,当您不是从某个对象调用该方法时,就像使用getContext() ,这等同于调用this.getContext() So you try to call the method from the instance of a class you are operation in now ( Bridge ), which does not have such a method defined. 因此,您尝试从当前正在操作的类的实例( Bridge )中调用该方法,该类尚未定义此类方法。 That's why you get the error 这就是为什么你会得到错误

The method getContext() is undefined for the type Bridge 对于类型Bridge,未定义方法getContext()

I had the same issue. 我遇到过同样的问题。

It was that my class was not extending AppCompatActivity as getContentResolver() is a method which resides in that class. 那是我的类没有扩展AppCompatActivity因为getContentResolver()是驻留在该类中的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM