1. 程式人生 > >cocos creator 保持螢幕常亮 螢幕不休眠

cocos creator 保持螢幕常亮 螢幕不休眠

此教程唯小白教程 只為了實現功能 安卓原生 很簡單 一學就會

具體原理什麼的多參考 android螢幕喚醒帖子等

教程開始

第一步 首先打包遊戲 安卓包這個就不用說了大家都會

構建-編譯 成功後 第二步 找到打包後的 Android 原工程 (如果你的打包目錄和我的一樣那目錄為)

你的工程目錄\build\jsb-default\frameworks\runtime-src\proj.android-studio

第三步 拖著這個資料夾 把他拖到 Android Studio 裡 也就是用Android Studio 開啟這個資料夾

(這裡怎麼使用 Android Studio 安裝SKD NDK等 我就不講了 百度一搜一大把  預設你們都是弄好的

)

開啟之後是這個樣子的

接下來怎麼修改 內容呢 首先找到 AndroidManifest.xml 檔案開啟許可權

位置在 這裡 

這裡我右邊圈上的 是你需要加上去的  是什麼自己查 我也不講了 簡單粗暴

xmlns:tools="http://schemas.android.com/tools"

<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.DEVICE_POWER"
tools:ignore="ProtectedPermissions" />

加上去之後 大家找到 string.xml檔案 位置在

很多教程是不需要加的 但是我這裡不在這裡獲取這個tag會報錯 我怕也有的同學和我有一樣的問題 所以我就加上了

至於是哪個tag 我待會會指出

<string name="Sylvia">Sylvia</string>

直接複製就完了 Sylvia 這個字串自己設定 一個就行了 唯一的就可以

然後 最後一步 也就是最重要的一步 找到 AppActivity 這個指令碼 位置在

程式碼我放在下邊 所有大家如果沒在這裡實現其他功能 就可以直接替換成我下邊的程式碼

如果實現過其他功能 直接複製我 ********************圈在中間的程式碼進去就可以了

/****************************************************************************
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
 
http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
package org.cocos2dx.javascript;


import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;

import android.os.Bundle;
import org.cocos2dx.javascript.SDKWrapper;
import com.cocos.analytics.CAAgent;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;

// **********************************************************************
import org.cocos2d.testtime.R;
import android.os.PowerManager;
// **********************************************************************


public class AppActivity extends Cocos2dxActivity {
    // **********************************************************************
    private PowerManager.WakeLock mWakeLock;
    // **********************************************************************

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Workaround in https://stackoverflow.com/questions/16283079/re-launch-of-activity-on-home-button-but-only-the-first-time/16447508
        if (!isTaskRoot()) {
            // Android launched another instance of the root activity into an existing task
            //  so just quietly finish and go away, dropping the user back into the activity
            //  at the top of the stack (ie: the last state of this task)
            // Don't need to finish it again since it's finished in super.onCreate .
            return;
        }
        // DO OTHER INITIALIZATION BELOW
        
        SDKWrapper.getInstance().init(this);
        CAAgent.enableDebug(false);

        // **********************************************************************
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,getString(R.string.Sylvia));
        mWakeLock.acquire();
        // **********************************************************************

    }
	
    @Override
    public Cocos2dxGLSurfaceView onCreateView() {
        Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
        // TestCpp should create stencil buffer
        glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
        SDKWrapper.getInstance().setGLSurfaceView(glSurfaceView);
        return glSurfaceView;
    }

    @Override
    protected void onResume() {
        super.onResume();
        SDKWrapper.getInstance().onResume();
        if (CAAgent.isInited())
            CAAgent.onResume(this);

        // **********************************************************************
        if(mWakeLock == null){
            PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
            mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK,getString(R.string.Sylvia));
            mWakeLock.acquire();
        }
        // **********************************************************************

    }

    @Override
    protected void onPause() {
        super.onPause();
        SDKWrapper.getInstance().onPause();
        if (CAAgent.isInited())
            CAAgent.onPause(this);

        // **********************************************************************
        if(mWakeLock != null){
            mWakeLock.release();
            mWakeLock = null;
        }
        // **********************************************************************
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SDKWrapper.getInstance().onDestroy();
        if (CAAgent.isInited())
            CAAgent.onDestroy();

        // **********************************************************************
        if(mWakeLock != null) {
            mWakeLock.release();
        }
        // **********************************************************************

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        SDKWrapper.getInstance().onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        SDKWrapper.getInstance().onNewIntent(intent);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        SDKWrapper.getInstance().onRestart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        SDKWrapper.getInstance().onStop();
    }
        
    @Override
    public void onBackPressed() {
        SDKWrapper.getInstance().onBackPressed();
        super.onBackPressed();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        SDKWrapper.getInstance().onConfigurationChanged(newConfig);
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        SDKWrapper.getInstance().onRestoreInstanceState(savedInstanceState);
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        SDKWrapper.getInstance().onSaveInstanceState(outState);
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onStart() {
        SDKWrapper.getInstance().onStart();
        super.onStart();
    }
}

劃重點  剛剛在stirng裡自己加的tag 如果和我的不一樣 大家自己修改一下

換成自己修改過的就好了 全部改好之後 儲存 打包到手機上測試效果吧

最後祝大家......祝大家牛逼吧.