解决办法就是就是利用dialog来显示
一、准备actvity
dialog是依附于activity创建的,利用Application.ActivityLifecycleCallbacks获得activity
/**
* 类描述:MyApplication 生命周期
*/
public class MyActivityLifecycleCallbacks implements Application.ActivityLifecycleCallbacks {
private Activity mActivity;
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
this.mActivity = activity;
}
@Override
public void onActivityStarted(Activity activity) {
this.mActivity = activity;
}
@Override
public void onActivityResumed(Activity activity) {
this.mActivity = activity;
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
public Activity getCurrentActivity() {
return mActivity;
}
}
二、在MyApplication内注册
public class MyApplication extends Application {
@SuppressLint("StaticFieldLeak")
private static MyApplication instance;
private MyActivityLifecycleCallbacks activityLifecycleCallbacks;
@Override
public void onCreate() {
super.onCreate();
//initMyActivityLifecycleCallbacks
initMyActivityLifecycleCallbacks ();
}
public static MyApplication getInstance() {
return instance;
}
private void initMyActivityLifecycleCallbacks () {
activityLifecycleCallbacks = new MyActivityLifecycleCallbacks();
registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
}
public Activity getCurrentActivity() {
if (activityLifecycleCallbacks != null) {
return activityLifecycleCallbacks.getCurrentActivity();
}
}
}
三、创建Toast
/**
* ToastUtil
*/
public class ToastUtil {
private static Handler mHandler = new Handler(Looper.getMainLooper());
public static void showRealToast(String message) {
Toast customToast = Toast.makeText(MyApplication.getInstance().getApplicationContext(), message, Toast.LENGTH_SHORT);
if (isNotificationsEnabled()) {
customToast.show();
} else {
Activity currentActivity = MyApplication.getInstance().getCurrentActivity();
if (currentActivity != null) {
showToastDialog(currentActivity, customToast);
}
}
}
private static boolean isNotificationsEnabled() {
boolean isOpened = false;
Context applicationContext = MyApplication.getInstance().getApplicationContext();
try {
//android api 24
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
isOpened = NotificationManagerCompat.from(applicationContext).areNotificationsEnabled();
} else {
isOpened = isAppOpsManagerNotificationEnable(applicationContext);
}
} catch (Exception e) {
e.printStackTrace();
}
return isOpened;
}
private static boolean isAppOpsManagerNotificationEnable(Context applicationContext) {
AppOpsManager mAppOps = (AppOpsManager) applicationContext.getSystemService(APP_OPS_SERVICE);
ApplicationInfo appInfo = applicationContext.getApplicationInfo();
String pkg = applicationContext.getApplicationContext().getPackageName();
int uid = appInfo.uid;
Class appOpsClass = null;
/* Context.APP_OPS_MANAGER */
try {
appOpsClass = Class.forName(AppOpsManager.class.getName());
Method checkOpNoThrowMethod = appOpsClass.getMethod("checkOpNoThrow", Integer.TYPE, Integer.TYPE, String.class);
Field opPostNotificationValue = appOpsClass.getDeclaredField("OP_POST_NOTIFICATION");
int value = (int) opPostNotificationValue.get(Integer.class);
return ((int) checkOpNoThrowMethod.invoke(mAppOps, value, uid, pkg) == AppOpsManager.MODE_ALLOWED);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static AppCompatDialog toastDialog;
private static void showToastDialog(Activity currentActivity, Toast customToast) {
dismissToastDialog();
toastDialog = new AppCompatDialog(currentActivity, R.style.Style_Toast_Dialog);
toastDialog.setCancelable(false);
toastDialog.setCanceledOnTouchOutside(false);
toastDialog.setContentView(customToast.getView());
Window toastDialogWindow = toastDialog.getWindow();
if (toastDialogWindow != null) {
WindowManager.LayoutParams layoutParams = toastDialogWindow.getAttributes();
layoutParams.gravity = customToast.getGravity();
layoutParams.horizontalMargin = customToast.getHorizontalMargin();
layoutParams.verticalMargin = customToast.getVerticalMargin();
toastDialogWindow.setAttributes(layoutParams);
}
toastDialog.show();
//2s消失
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
toastDialog.dismiss();
}
}, 2000);
}
private static void dismissToastDialog() {
if (toastDialog != null) {
mHandler.removeCallbacksAndMessages(null);
toastDialog.dismiss();
toastDialog = null;
}
}
}
Style资源
<style name="Style_Toast_Dialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
最后调用ToastUtil.showRealToast(String message);