【问题标题】:How to get android id in java without using of the class Activity?java - 如何在不使用Activity类的情况下在java中获取android id?
【发布时间】:2014-08-09 23:28:25
【问题描述】:

我不能使用以下方法,因为我的类没有扩展 Activity 类: Is there a unique Android device ID?

【问题讨论】:

  • 将上下文对象传递给方法。
  • 使你的类成员对象静态。然后你可以使用Class.member 这样就可以了。
  • 感谢所有答案,但是因为我没有使用活动类,我无法访问上下文对象。我对另一个 apk 进行检测,这就是我不能使用活动类的原因。您的所有解决方案都使用 Activity 类(直接或间接)
  • ...在构造函数参数中发送上下文

标签: java android android-activity


【解决方案1】:

这样试试

public class Sample {


public static String getId(Context c)
{

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

     return android_id;
    }
}

当你需要设备ID时

String id = Sample.getId(youractivity.this);// if ur in an activity

或者试试这个

 String id =Sample.getId(getApplicationContext());

【讨论】:

    【解决方案2】:

    这样试试

    import android.content.Context;
    import android.provider.Settings.Secure;
    
    public class sample {
    
    
        public String getId(Context c)
        {
    
             String android_id = Secure.getString(c.getContentResolver(),
                                                                        Secure.ANDROID_ID); 
    
             return android_id;
        }
    }
    

    从您的活动中传递上下文

    sample s=new sample();
    
        String udid=s.getId(getApplicationContext());
        Toast.makeText(getApplicationContext(), udid, Toast.LENGTH_LONG).show();
    

    【讨论】:

    • 感谢您的回答,但是因为我没有使用活动类,所以无法访问上下文对象。我对另一个 apk 进行检测,这就是我不能使用活动类的原因。
    猜你喜欢
    • 2018-09-28
    • 2016-06-22
    • 1970-01-01
    • 2015-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多