zoukankan      html  css  js  c++  java
  • android4.4 添加快捷开关(以截屏为例)

    A,frameworksasepackagesSystemUI esvaluesconfig.xml

             添加: <bool name="quick_settings_show_screenshot">true</bool>

            

    B,frameworksasepackagesSystemUI esvaluesstrings.xml

             添加:<string name="quick_settings_screen_shot_label">Screen shot</string

    C,frameworksasepackagesSystemUI esvalues-zh-rCNstrings.xml

             添加:<string name="quick_settings_screen_shot_label" msgid="f9870466a61654f367d">屏幕截图</string>

    D,添加图标

            ①frameworksasepackagesSystemUI esdrawable-hdpiic_qs_screen_shot.png
    ②frameworksasepackagesSystemUI esdrawable-mdpiic_qs_screen_shot.png
    ③frameworksasepackagesSystemUI esdrawable-xhdpiic_qs_screen_shot.png

    E,添加单布局文件:

    frameworksasepackagesSystemUI eslayoutquick_settings_tile_screen_shot.xml

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Copyright (C) 2012 The Android Open Source Project
    
         Licensed under the Apache License, Version 2.0 (the "License");
         you may not use this file except in compliance with the License.
         You may obtain a copy of the License at
    
              http://www.apache.org/licenses/LICENSE-2.0
    
         Unless required by applicable law or agreed to in writing, software
         distributed under the License is distributed on an "AS IS" BASIS,
         WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
         See the License for the specific language governing permissions and
         limitations under the License.
    -->
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/TextAppearance.QuickSettings.TileView"
        android:id="@+id/screen_shot_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:drawableTop="@drawable/ic_qs_screen_shot"
        android:text="@string/quick_settings_screen_shot_label"
        />

    F,添加相应java代码:

             frameworksasepackagesSystemUIsrccomandroidsystemuistatusbarphoneQuickSettings.java

     import android.content.ServiceConnection;
    
    import android.os.IBinder;
    
    import android.os.Message;
    
    import android.os.Messenger;

    再在addTemporaryTiles() 方法里面的最后添加相应代码:

    ///////////////////////////////////////_1126/////////////////////////////////////////////
                // screen shot
                QuickSettingsTileView ScreenShotTile = (QuickSettingsTileView)
                        inflater.inflate(R.layout.quick_settings_tile, parent, false);
                ScreenShotTile.setContent(R.layout.quick_settings_tile_screen_shot, inflater);
                ScreenShotTile.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.i(TAG, "---------------------------------screen shot is clicked.");
                        //takeScreenshot();
                        mBar.collapseAllPanels(true);
                        mHandler.removeCallbacks(mScreenshotClick);
                        mHandler.postDelayed(mScreenshotClick, 500);
                    }
                });
                if (mContext.getResources().getBoolean(R.bool.quick_settings_show_screenshot)) {
                    parent.addView(ScreenShotTile);
            }

    随后在外部添加相应调用代码:

    /*******************_1126*********************************/
        
        // add by xxx, for screen shot in quick settings.
            private final Runnable mScreenshotClick = new Runnable() {
                public void run() {
                    takeScreenshot();
                }
            };
    
            private final Object mScreenshotLock = new Object();
            private ServiceConnection mScreenshotConnection = null;
            
            private final Runnable mScreenshotTimeout = new Runnable() {
                @Override public void run() {
                    synchronized (mScreenshotLock) {
                        if (mScreenshotConnection != null) {
                            mContext.unbindService(mScreenshotConnection);
                            mScreenshotConnection = null;
                        }
                    }
                }
            };
    
            // Assume this is called from the Handler thread.
            private void takeScreenshot() {
                Log.i("bright", "start take screen shot.");
                synchronized (mScreenshotLock) {
                    if (mScreenshotConnection != null) {
                        return;
                    }
                    ComponentName cn = new ComponentName("com.android.systemui",
                            "com.android.systemui.screenshot.TakeScreenshotService");
                    Intent intent = new Intent();
                    intent.setComponent(cn);
                    ServiceConnection conn = new ServiceConnection() {
                        @Override
                        public void onServiceConnected(ComponentName name, IBinder service) {
                            synchronized (mScreenshotLock) {
                                if (mScreenshotConnection != this) {
                                    return;
                                }
                                Messenger messenger = new Messenger(service);
                                Message msg = Message.obtain(null, 1);
                                final ServiceConnection myConn = this;
                                Handler h = new Handler(mHandler.getLooper()) {
                                    @Override
                                    public void handleMessage(Message msg) {
                                        synchronized (mScreenshotLock) {
                                            if (mScreenshotConnection == myConn) {
                                                mContext.unbindService(mScreenshotConnection);
                                                mScreenshotConnection = null;
                                                mHandler.removeCallbacks(mScreenshotTimeout);
                                            }
                                        }
                                    }
                                };
                                msg.replyTo = new Messenger(h);
                                msg.arg1 = msg.arg2 = 1;
                                try {
                                    messenger.send(msg);
                                } catch (RemoteException e) {
                                }
                            }
                        }
                        @Override
                        public void onServiceDisconnected(ComponentName name) {}
                    };
                    if (mContext.bindService(
                            intent, conn, Context.BIND_AUTO_CREATE)) {
                        mScreenshotConnection = conn;
                        mHandler.postDelayed(mScreenshotTimeout, 10000);
                    }
                }
            }
        
        /**************************************************/

    系统编译后的最终效果如下图(找了个关于SIM的图标代替):

          

    ------------------------------------------------------------------------------------- -------------------------------------- -------------------------

    参考:http://blog.csdn.net/yicao821/article/details/17416649

  • 相关阅读:
    【译】SQL Server误区30日谈Day3即时文件初始化特性可以在SQL Server中开启和关闭
    有关TSQL的10个好习惯
    【译】SQL Server误区30日谈Day6有关NULL位图的三个误区
    一次由重复索引导致的问题
    【译】SQL Server误区30日谈Day2DBCC CHECKDB会导致阻塞
    【译】SQL Server误区30日谈Day1正在运行的事务在服务器故障转移后继续执行
    CodeFileBaseClass 属性
    Mako 模板系统文档翻译(2) 语法
    终于搞定了 django 的 ajax 方式上传图片
    ASP.NET Ajax 调试技巧:用 FireBug 调试 UpdatePanel 不更新问题
  • 原文地址:https://www.cnblogs.com/Miami/p/4998441.html
Copyright © 2011-2022 走看看