启动其他应用程序

最近SB项目主管要求APP进入系统设置界面,要有按钮来返回之前的APP,插了一下网上的资料,具体做法如下:

public void showFlowButton(Activity context){

    mContext = context.getApplicationContext();
    //获取系统窗口服务
    windowManager =  (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
    // 类型
    params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
    // WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
    // 设置flag
    int flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL;
    // | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
    // 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,弹出的View收不到Back键的事件
    params.flags = flags;
    // 不设置这个弹出框的透明遮罩显示为黑色
    params.format = PixelFormat.TRANSLUCENT;
    // FLAG_NOT_TOUCH_MODAL不阻塞事件传递到后面的窗口
    // 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时,后面的应用图标由不可长按变为可长按
    // 不设置这个flag的话,home页的划屏会有问题
    params.width = WindowManager.LayoutParams.WRAP_CONTENT;
    params.height = WindowManager.LayoutParams.WRAP_CONTENT;
    params.gravity = Gravity.CENTER;
    windowManager.addView(getButton(),params);

}
Button button;
private View getButton(){

    if (button!=null){

        button.setVisibility(Button.VISIBLE);
        return button;
    }

    button = new Button(mContext);
    button.setText("back");
    button.setTextSize(35);
    button.setWidth(200);
    button.setHeight(150);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Log.d("TAG","---back---");
            button.setVisibility(Button.GONE);
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            ComponentName cn = new ComponentName("ch.hc.juice", "ch.view.main.SettingAndDebugActivity");
            intent.setComponent(cn);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//这个flag一定要加上,不然会报错让你加上
            mContext.startActivity(intent);
        }
    });
    return button;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值