我有应用程序发送了哪条消息,并在BroadcastReceiver
之后的某个时间收到了其报告。此报告将通过我正在使用DialogFragment
的对话框显示给用户,如下所示。
myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(firstTime)
firstTime = false;
boolean anyError = false;
switch (getResultCode()) {
case Activity.RESULT_OK:
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
case SmsManager.RESULT_ERROR_NO_SERVICE:
case SmsManager.RESULT_ERROR_NULL_PDU:
case SmsManager.RESULT_ERROR_RADIO_OFF:
anyError = true;
break;
}
sent.add(anyError);
CustomAlertDialogFragment customAlertDialogFragment = CustomAlertDialogFragment.newInstance("Title",logMessage);
customAlertDialogFragment.show(getActivity().getSupportFragmentManager(),"TAG");
sent.clear();
}
};
CustomDialog
的代码如下。
public class CustomAlertDialogFragment extends DialogFragment {
public static CustomAlertDialogFragment newInstance(String title, String content) {
CustomAlertDialogFragment customAlertDialogFragment = new CustomAlertDialogFragment();
Bundle args = new Bundle();
args.putString("title",title);
args.putString("content", content);
customAlertDialogFragment.setArguments(args);
return customAlertDialogFragment;
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String content = getArguments().getString("content");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// title bar string
builder.setTitle(title);
builder.setPositiveButton(R.string.ok, null);
builder.setMessage(content);
AlertDialog errorDialog = builder.create();
// return the Dialog object
return errorDialog;
}
}
如果用户同时移动到任何其他Fragment
或Activity
,则不会显示该对话框。它应该抛出NullPointerException
如果它没有得到上下文但它没有。什么可能是替代或解决方案。我已经提到其他SO问题,其中成员要求使用通知,但我的要求是对话。请帮忙。
答案 0 :(得分:0)
我猜是Dialogue
正在抛出一个名为window leaked
的异常。在您的情况下,我认为您可能会考虑使用Application
类。
无论如何,您可以在BroadcastReceiver
课程中为您的Application
课程注册Context
,该课程将接收广播并按需显示对话框。
要检索getApplicationContext()
,您可以使用public class MyApplication extends Application {
// Declare the BroadcastReceiver in your Application class
BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(firstTime)
firstTime = false;
boolean anyError = false;
switch (getResultCode()) {
case Activity.RESULT_OK:
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
case SmsManager.RESULT_ERROR_NO_SERVICE:
case SmsManager.RESULT_ERROR_NULL_PDU:
case SmsManager.RESULT_ERROR_RADIO_OFF:
anyError = true;
break;
}
sent.add(anyError);
CustomAlertDialogFragment customAlertDialogFragment = CustomAlertDialogFragment.newInstance("Title",logMessage);
customAlertDialogFragment.show(getApplicationContext().getSupportFragmentManager(), "TAG");
sent.clear();
}
};
@Override
public void onResume() {
super.onCreate();
// Register the receiver here
}
@Override
public void onPause() {
super.onCreate();
// Un-register the receiver here
}
}
。
Application
要在清单中注册<application
android:name="Yourpackage.MyApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:logo="@drawable/your_logo"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:icon">
<!-- Your activities -->
</application>
课程,您需要定义名称
tsv