获取imei信息时如何获取dialogFragment中的上下文
how to get the context in dialogFragment when get imei info
@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
上下文,当我调用 getContext(this)
时给出错误 image from android studio
public static String getUniqueIMEIId(Context context) {
TelephonyManager telephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
...
String imei = telephonyManager.getDeviceId();
...
}
具体错误是什么?是否显示需要 Context 但已通过 Dialog?
如果是,您可以只使用上面使用的 getActivity() 方法来访问上下文。
所以只要打电话
sIMEI = getUniqueIMEIId(getActivity());
将 getContext(this)
替换为 getActivity().getApplicationContext()
,它应该可以工作。
如果您使用 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
在主要情况下它们是相同的。而我们只需要 context
。所以我建议你使用 getContext()
@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
上下文,当我调用 getContext(this)
时给出错误 image from android studio
public static String getUniqueIMEIId(Context context) {
TelephonyManager telephonyManager = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
...
String imei = telephonyManager.getDeviceId();
...
}
具体错误是什么?是否显示需要 Context 但已通过 Dialog?
如果是,您可以只使用上面使用的 getActivity() 方法来访问上下文。
所以只要打电话
sIMEI = getUniqueIMEIId(getActivity());
将 getContext(this)
替换为 getActivity().getApplicationContext()
,它应该可以工作。
如果您使用 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
在主要情况下它们是相同的。而我们只需要 context
。所以我建议你使用 getContext()