首页

android 点亮或者熄灭屏幕 示例源码

java

2020-6-29

package com.sharenew.screenoff;

import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    Button buttonOff;
    Button buttonOn;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    turnOnScreen();
                    break;
                case 2:

                    break;
            }
        }
    };
    DevicePolicyManager policyManager;
    ComponentName adminReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonOff = (Button) findViewById(R.id.button_off);
        adminReceiver = new ComponentName(MainActivity.this, ScreenOffAdminReceiver.class);
        buttonOff.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {



                boolean admin = policyManager.isAdminActive(adminReceiver);
                if (admin) {
                    policyManager.lockNow();
                    handler.sendEmptyMessageDelayed(1,3000);
                } else {
                    Toast.makeText(MainActivity.this,"没有设备管理权限",
                            Toast.LENGTH_LONG).show();
                }
            }
        });
        buttonOn = (Button) findViewById(R.id.button_on);
        buttonOn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                turnOnScreen();
            }
        });
        mPowerManager = (PowerManager) getSystemService(POWER_SERVICE);
        policyManager = (DevicePolicyManager) MainActivity.this.getSystemService(Context.DEVICE_POLICY_SERVICE);

        Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN,  adminReceiver);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"开启后就可以使用锁屏功能了...");//显示位置见图二

        startActivityForResult(intent, 0);
    }


    private PowerManager mPowerManager;
    private PowerManager.WakeLock mWakeLock;

    public void turnOnScreen() {
        // turn on screen
        Log.v("ProximityActivity", "ON!");
        mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "tag");
        mWakeLock.acquire();
        mWakeLock.release();
    }

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

    /**
     * 检测用户是否开启了超级管理员
     */
    private void isOpen() {
        if(policyManager.isAdminActive(adminReceiver)){//判断超级管理员是否激活

            Toast.makeText(MainActivity.this,"设备已被激活",
                    Toast.LENGTH_LONG).show();

        }else{

            Toast.makeText(MainActivity.this,"设备没有被激活",
                    Toast.LENGTH_LONG).show();

        }
    }






}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
.gradle/4.1/fileChanges/last-build.bin , 1
.gradle/4.1/fileContent/fileContent.lock , 17
.gradle/4.1/fileHashes/fileHashes.bin , 293769
.gradle/4.1/fileHashes/fileHashes.lock , 17
.gradle/4.1/fileHashes/resourceHashesCache.bin , 19823
.gradle/4.1/javaCompile/classAnalysis.bin , 1066218
.gradle/4.1/javaCompile/jarAnalysis.bin , 434550
.gradle/4.1/javaCompile/javaCompile.lock , 17
.gradle/4.1/javaCompile/taskHistory.bin , 47183
.gradle/4.1/javaCompile/taskJars.bin , 20570
.gradle/4.1/taskHistory/fileSnapshots.bin , 1857057
.gradle/4.1/taskHistory/taskHistory.bin , 61532
.gradle/4.1/taskHistory/taskHistory.lock , 17
.gradle/buildOutputCleanup/built.bin , 0
.gradle/buildOutputCleanup/cache.properties , 51
.gradle/buildOutputCleanup/cache.properties.lock , 2
.idea/compiler.xml , 686
.idea/copyright/profiles_settings.xml , 74
.idea/gradle.xml , 722
.idea/libraries/com_android_support_animated_vector_drawable_25_2_0.xml , 886
.idea/libraries/com_android_support_appcompat_v7_25_2_0.xml , 802
.idea/libraries/com_android_support_support_annotations_25_2_0_jar.xml , 701
.idea/libraries/com_android_support_support_compat_25_2_0.xml , 816
.idea/libraries/com_android_support_support_core_ui_25_2_0.xml , 823
.idea/libraries/com_android_support_support_core_utils_25_2_0.xml , 844
.idea/libraries/com_android_support_support_fragment_25_2_0.xml , 830
.idea/libraries/com_android_support_support_media_compat_25_2_0.xml , 858
.idea/libraries/com_android_support_support_v4_25_2_0.xml , 472
.idea/libraries/com_android_support_support_vector_drawable_25_2_0.xml , 879
.idea/libraries/com_android_support_test_espresso_espresso_core_2_2_2.xml , 844
.idea/libraries/com_android_support_test_espresso_espresso_idling_resource_2_2_2.xml , 921
.idea/libraries/com_android_support_test_exposed_instrumentation_api_publish_0_5.xml , 957
.idea/libraries/com_android_support_test_rules_0_5.xml , 747
.idea/libraries/com_android_support_test_runner_0_5.xml , 754
.idea/libraries/com_google_android_gms_play_services_appindexing_8_4_0.xml , 517
.idea/libraries/com_google_android_gms_play_services_base_8_4_0.xml , 496
.idea/libraries/com_google_android_gms_play_services_basement_8_4_0.xml , 508
.idea/libraries/com_google_code_findbugs_jsr305_2_0_1_jar.xml , 359
.idea/libraries/com_squareup_javawriter_2_1_1_jar.xml , 538
.idea/libraries/javax_annotation_javax_annotation_api_1_2_jar.xml , 590
.idea/libraries/javax_inject_javax_inject_1_jar.xml , 528
.idea/libraries/junit_junit_4_12_jar.xml , 487
.idea/libraries/org_hamcrest_hamcrest_core_1_3_jar.xml , 543
.idea/libraries/org_hamcrest_hamcrest_integration_1_3_jar.xml , 578
.idea/libraries/org_hamcrest_hamcrest_library_1_3_jar.xml , 557
.idea/misc.xml , 2134
.idea/modules.xml , 932
.idea/runConfigurations.xml , 564
.idea/vcs.xml , 167
.idea/workspace.xml , 139112
AndroidScreenOnAndOff-master.iml , 905
README.md , 198
app/.gitignore , 7
app/app.iml , 11619
app/build/generated/mockable-android-25.v3.jar , 2623488
app/build/generated/source/buildConfig/androidTest/debug/com/sharenew/screenoff/test/BuildConfig.java , 461
app/build/generated/source/buildConfig/debug/com/sharenew/screenoff/BuildConfig.java , 451
app/build/generated/source/r/androidTest/debug/android/app/R.java , 241
app/build/generated/source/r/androidTest/debug/android/support/test/R.java , 250
app/build/generated/source/r/androidTest/debug/android/support/test/espresso/R.java , 362
app/build/generated/source/r/androidTest/debug/android/support/test/espresso/idling/R.java , 266
app/build/generated/source/r/androidTest/debug/android/support/test/rule/R.java , 358
app/build/generated/source/r/androidTest/debug/com/sharenew/screenoff/test/R.java , 422643
app/build/generated/source/r/debug/android/support/compat/R.java , 252
app/build/generated/source/r/debug/android/support/coreui/R.java , 252
app/build/generated/source/r/debug/android/support/coreutils/R.java , 255
app/build/generated/source/r/debug/android/support/fragment/R.java , 254
app/build/generated/source/r/debug/android/support/graphics/drawable/R.java , 263
app/build/generated/source/r/debug/android/support/graphics/drawable/animated/R.java , 272
app/build/generated/source/r/debug/android/support/mediacompat/R.java , 257
app/build/generated/source/r/debug/android/support/v4/R.java , 248
app/build/generated/source/r/debug/android/support/v7/appcompat/R.java , 110550
app/build/generated/source/r/debug/com/google/android/gms/R.java , 10543
app/build/generated/source/r/debug/com/google/android/gms/appindexing/R.java , 10555
app/build/generated/source/r/debug/com/google/android/gms/base/R.java , 10548
app/build/generated/source/r/debug/com/sharenew/screenoff/R.java , 419849
app/build/intermediates/blame/res/androidTest/debug/multi-v2/debug.json , 395878
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-af.json , 4342
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-am.json , 4336
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ar.json , 4344
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-az-rAZ.json , 4330
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-b+sr+Latn.json , 1156
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-be-rBY.json , 1145
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-bg.json , 4348
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-bn-rBD.json , 4374
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-bs-rBA.json , 1145
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ca.json , 4348
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-cs.json , 4345
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-da.json , 4340
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-de.json , 4346
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-el.json , 4350
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-en-rAU.json , 4364
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-en-rGB.json , 4364
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-en-rIN.json , 4364
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-es-rUS.json , 4374
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-es.json , 4346
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-et-rEE.json , 4374
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-eu-rES.json , 4369
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-fa.json , 4346
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-fi.json , 4340
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-fr-rCA.json , 4378
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-fr.json , 4352
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-gl-rES.json , 4372
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-gu-rIN.json , 4368
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-h720dp-v13.json , 809
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-hdpi-v4.json , 838
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-hi.json , 4347
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-hr.json , 4346
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-hu.json , 4350
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-hy-rAM.json , 4368
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-in.json , 4348
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-is-rIS.json , 4362
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-it.json , 4342
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-iw.json , 4338
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ja.json , 4330
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ka-rGE.json , 4371
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-kk-rKZ.json , 4367
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-km-rKH.json , 4366
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-kn-rIN.json , 4371
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ko.json , 4326
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ky-rKG.json , 4368
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-land.json , 830
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-large-v4.json , 924
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ldltr-v21.json , 806
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-lo-rLA.json , 4366
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-lt.json , 4350
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-lv.json , 4347
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-mk-rMK.json , 4296
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ml-rIN.json , 4374
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-mn-rMN.json , 4366
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-mr-rIN.json , 4371
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ms-rMY.json , 4372
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-my-rMM.json , 4377
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-nb.json , 4338
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ne-rNP.json , 4378
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-night-v8.json , 893
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-nl.json , 4350
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-pa-rIN.json , 4364
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-pl.json , 4347
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-port.json , 785
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-pt-rBR.json , 4374
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-pt-rPT.json , 4374
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-pt.json , 4350
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ro.json , 4350
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-ru.json , 4346
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-si-rLK.json , 4370
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sk.json , 4344
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sl.json , 4347
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sq-rAL.json , 4370
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sr.json , 4348
app/build/intermediates/blame/res/androidTest/debug/multi-v2/values-sv.json , 4344
最多只能显示150条信息!
没有账号? 忘记密码?

社交账号快速登录