码迷,mamicode.com
首页 > 移动开发 > 详细

Android 禁止状态栏下拉

时间:2016-05-12 19:06:46      阅读:1369      评论:0      收藏:0      [点我收藏+]

标签:

概述

因为需求所以贴出该代码,该源码需要系统共享,且开源板子上才能安装,其次需要将编译的apk进行platform签名,仅仅是记录一下开发过程.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Object statusBarManagerService;
    private Method methodDisable;
    private TextView tvInfo;
//    SCREENPINNING_MODE_FLAG = StatusBarManager.DISABLE_NONE
//    | StatusBarManager.DISABLE_EXPAND
//    | StatusBarManager.DISABLE_NOTIFICATION_ICONS
//    | StatusBarManager.DISABLE_NOTIFICATION_ALERTS
//    // | StatusBarManager.DISABLE_CLOCK
//    // | StatusBarManager.DISABLE_SYSTEM_INFO
//    | StatusBarManager.DISABLE_HOME
//    | StatusBarManager.DISABLE_SEARCH
//    | StatusBarManager.DISABLE_BACK
//    | StatusBarManager.DISABLE_RECENT;
    private final static int SCREENPINNING_MODE_FLAG = 57081856;
    private final static int NORMAL_MODE_FLAG = 0;
    private final static int SCREENPINNING_MODE_WITH_BACK_FLAG = 52887552;
    private final static int DATE_SETTINGS_REQUEST = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        tvInfo = (TextView) findViewById(R.id.tv_info);
        // Get android @hide class android.app.StatusBarManager by reflection.
        try
        {//反射拿到状态栏管理器
            Class clsStatusBarManager = Class.forName("android.app.StatusBarManager");
            Context appContext = getApplicationContext();
            if (appContext != null) {
                statusBarManagerService = appContext.getSystemService("statusbar");
                methodDisable = clsStatusBarManager.getMethod("disable", int.class);
            }
        } catch (ClassNotFoundException e) {
            reflectionError(e);
        } catch (NoSuchMethodException e) {
            reflectionError(e);
        }
    }

    private void reflectionError(Exception ex) {
        tvInfo.setBackgroundColor(Color.RED);
        tvInfo.setText("reflection error: " + ex.toString());
    }

    private void disableStatusBar(int flag) {
        try {//实现的核心.
            methodDisable.invoke(statusBarManagerService, flag);
        } catch (IllegalAccessException e) {
            reflectionError(e);
        } catch (InvocationTargetException e) {
            reflectionError(e);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_pin: {
                disableStatusBar(SCREENPINNING_MODE_FLAG);
                break;
            }
            case R.id.bt_unpin: {
                disableStatusBar(NORMAL_MODE_FLAG);
                break;
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            // Enable BACK button to return from Settings
            disableStatusBar(SCREENPINNING_MODE_WITH_BACK_FLAG);
            startActivityForResult(new Intent(Settings.ACTION_DATE_SETTINGS), DATE_SETTINGS_REQUEST);
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    protected void onActivityResult(int requestCode, int resultCode,
                                    Intent data) {
        if (requestCode == DATE_SETTINGS_REQUEST) {
            // Disable BACK button when returning from Settings
            disableStatusBar(SCREENPINNING_MODE_FLAG);
        }
    }
}

最后需要在配置文件添加:xml
android:sharedUserId="android.uid.system"

Android 禁止状态栏下拉

标签:

原文地址:http://blog.csdn.net/u010316858/article/details/51354759

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!