Android后台切回到应用显示广告页

看到市面上的很多app从后台切回到app会有广告页显示,因此也尝试了下,经过测试,以下的代码可以实现。

/**
 * Created by myc on 2017/6/17.
 * More Code on 1101255053@qq.com
 * Description:
 */
public class BaseActivity extends Activity {
    private static final String TAG = "BaseActivity";
    //用来控制应用前后台切换的逻辑
    private boolean isCurrentRunningForeground = true;
    @Override
    protected void onStart() {
        super.onStart();
        if (!isCurrentRunningForeground) {
            isCurrentRunningForeground=true;
            //处理跳转到广告页逻辑
            Intent intennt=new Intent(this,LoadingActivity.class);
            startActivity(intennt);
            Log.e(TAG, ">>>>>>>>>>>>>>>>>>>切回前台 activity process");
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        isCurrentRunningForeground = isRunningForeground();
        if (!isCurrentRunningForeground) {
            Log.e(TAG,">>>>>>>>>>>>>>>>>>>切到后台 activity process");
        }
    }


    public boolean isRunningForeground() {
        ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcessInfos = activityManager.getRunningAppProcesses();
        // 枚举进程,查看该应用是否在运行  
        for (ActivityManager.RunningAppProcessInfo appProcessInfo : appProcessInfos) {
            if (appProcessInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                if (appProcessInfo.processName.equals(this.getApplicationInfo().processName)) {
                    Log.e(TAG,"EntryActivity isRunningForeGround");
                    return true;
                }
            }
        }
        Log.e(TAG, "EntryActivity isRunningBackGround");
        return false;
    }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
如果你想在 Android 应用内实现广告悬浮窗,可以使用 Android 的 PopupWindow 类来实现。下面是一个简单的实现方式: 1. 创建一个广告悬浮窗的布局文件,例如 ad_float.xml: ```xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/ad_float_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent"> <!-- 在这里添加你的广告布局 --> </RelativeLayout> ``` 2. 在需要显示广告悬浮窗的地方创建并显示 PopupWindow: ```java // 加载广告悬浮窗的布局文件 View adView = LayoutInflater.from(this).inflate(R.layout.ad_float, null); // 在这里添加你的广告布局 // 创建 PopupWindow 对象 PopupWindow popupWindow = new PopupWindow(adView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, false); // 设置 PopupWindow 的背景为透明 popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); // 设置 PopupWindow 的位置 popupWindow.showAtLocation(getWindow().getDecorView(), Gravity.CENTER, 0, 0); ``` 3. 在不需要显示广告悬浮窗的地方隐藏 PopupWindow: ```java if (popupWindow != null && popupWindow.isShowing()) { popupWindow.dismiss(); } ``` 注意:在使用 PopupWindow 显示广告悬浮窗时,需要确保不会影响到用户的正常操作,例如不要遮挡到重要的界面元素,也不要让广告悬浮窗一直显示导致用户体验下降。同时,也要遵守广告业界的相关规定,例如不要过于频繁地显示广告等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值