gpt4 book ai didi

java - 获取imei信息时如何获取dialogFragment中的上下文

转载 作者:行者123 更新时间:2023-11-30 05:19:22 26 4
gpt4 key购买 nike

@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.layout_reg_dialog, null);
TextView tvIMEI = null;

builder.setView(view)
.setTitle("Activer la Version");
registerKey = view.findViewById(R.id.regiter_key);
tvIMEI = view.findViewById(R.id.tvIMEI);
String sIMEI;
sIMEI = getUniqueIMEIId(getContext(this));
tvIMEI.setText(sIMEI);
return builder.create();
}

getUniqueIMEIID 不是 abel this context ,当我调用 getContext(this) 时给出错误 image from android studio

public static String getUniqueIMEIId(Context context) {
TelephonyManager telephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
...
String imei = telephonyManager.getDeviceId();
...
}

最佳答案

如果您使用dialogFragment创建对话框,并且getUniqueIMEIId如下所示:

public static String getUniqueIMEIId(Context context) {
try {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return "";
}
String imei = telephonyManager.getDeviceId();
Log.e("imei", "=" + imei);
if (imei != null && !imei.isEmpty()) {
return imei;
} else {
return android.os.Build.SERIAL;
}
} catch (Exception e) {
e.printStackTrace();
}
return "not_found";
}

getContext() 是。

Return the Context this fragment is currently associated with but the getUniqueIMEIId method needs the context

getActivity()

Return the FragmentActivity this fragment is currently associated with. May return null if the fragment is associated with a Context instead

在主要情况下它们是相同的。我们只需要上下文。所以我建议你使用 getContext()

关于java - 获取imei信息时如何获取dialogFragment中的上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59810706/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com