Skip to content

Commit 4613dd4

Browse files
Auto delete file
1 parent f84a226 commit 4613dd4

8 files changed

Lines changed: 72 additions & 86 deletions

File tree

app/build.gradle

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
apply plugin: 'com.android.application'
1+
2+
plugins {
3+
id 'com.android.application'
4+
}
25

36
android {
4-
compileSdkVersion 33
5-
namespace 'com.example.wakelockapp' // <- Thêm dòng này
7+
namespace 'com.example.wakelockapp'
8+
compileSdk 33
69

7-
810
defaultConfig {
911
applicationId "com.example.wakelockapp"
10-
minSdkVersion 21
11-
targetSdkVersion 33
12+
minSdk 21
13+
targetSdk 33
1214
versionCode 1
1315
versionName "1.0"
14-
15-
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1616
}
1717

1818
buildTypes {
@@ -25,5 +25,4 @@ namespace 'com.example.wakelockapp' // <- Thêm dòng này
2525

2626
dependencies {
2727
implementation 'androidx.appcompat:appcompat:1.6.1'
28-
implementation 'com.google.android.material:material:1.9.0'
2928
}

app/src/main/AndroidManifest.xml

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
>
3+
package="com.example.wakelockapp">
34

4-
<!-- Permission để giữ CPU awake -->
55
<uses-permission android:name="android.permission.WAKE_LOCK" />
66

77
<application
88
android:allowBackup="true"
9-
10-
android:label="@string/app_name"
11-
9+
android:label="WakeLockApp"
1210
android:supportsRtl="true"
13-
android:theme="@style/Theme.WakeLockApp">
14-
<activity
15-
android:name=".MainActivity"
16-
android:exported="true"
17-
android:launchMode="singleTop">
11+
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
12+
13+
<activity android:name=".MainActivity">
1814
<intent-filter>
1915
<action android:name="android.intent.action.MAIN" />
2016
<category android:name="android.intent.category.LAUNCHER" />
2117
</intent-filter>
2218
</activity>
19+
20+
<receiver android:name=".WakeLockReceiver" android:exported="true"/>
2321
</application>
2422
</manifest>
Lines changed: 23 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,40 @@
1+
12
package com.example.wakelockapp;
23

4+
import android.app.AlertDialog;
35
import android.content.Context;
4-
import android.content.Intent;
6+
import android.content.DialogInterface;
57
import android.os.Bundle;
68
import android.os.PowerManager;
7-
import android.util.Log;
89
import androidx.appcompat.app.AppCompatActivity;
910

1011
public class MainActivity extends AppCompatActivity {
11-
private static final String TAG = "WakeLockApp";
1212
private PowerManager.WakeLock wakeLock;
1313

1414
@Override
1515
protected void onCreate(Bundle savedInstanceState) {
1616
super.onCreate(savedInstanceState);
17-
setContentView(R.layout.activity_main);
1817

1918
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
20-
wakeLock = powerManager.newWakeLock(
21-
PowerManager.PARTIAL_WAKE_LOCK,
22-
"WakeLockApp::MyWakeLockTag"
23-
);
24-
25-
handleIntent(getIntent());
26-
}
27-
28-
@Override
29-
protected void onNewIntent(Intent intent) {
30-
super.onNewIntent(intent);
31-
setIntent(intent);
32-
handleIntent(intent);
33-
}
34-
35-
private void handleIntent(Intent intent) {
36-
String state = intent.getStringExtra("state");
37-
if ("on".equals(state)) {
38-
if (!wakeLock.isHeld()) {
39-
wakeLock.acquire();
40-
Log.i(TAG, "WakeLock acquired via command.");
41-
}
42-
} else if ("off".equals(state)) {
43-
if (wakeLock.isHeld()) {
44-
wakeLock.release();
45-
Log.i(TAG, "WakeLock released via command.");
46-
}
47-
} else {
48-
Log.i(TAG, "No state extra: " + state);
49-
}
50-
}
51-
52-
@Override
53-
protected void onDestroy() {
54-
super.onDestroy();
55-
if (wakeLock.isHeld()) {
56-
wakeLock.release();
57-
}
19+
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WakeLockApp::MainLock");
20+
21+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
22+
builder.setTitle("Wake Lock")
23+
.setMessage("Bật hay tắt Wake Lock?")
24+
.setPositiveButton("Bật", new DialogInterface.OnClickListener() {
25+
public void onClick(DialogInterface dialog, int id) {
26+
if (!wakeLock.isHeld()) {
27+
wakeLock.acquire();
28+
}
29+
}
30+
})
31+
.setNegativeButton("Tắt", new DialogInterface.OnClickListener() {
32+
public void onClick(DialogInterface dialog, int id) {
33+
if (wakeLock.isHeld()) {
34+
wakeLock.release();
35+
}
36+
}
37+
});
38+
builder.create().show();
5839
}
5940
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
package com.example.wakelockapp;
3+
4+
import android.content.BroadcastReceiver;
5+
import android.content.Context;
6+
import android.content.Intent;
7+
import android.os.PowerManager;
8+
import android.util.Log;
9+
10+
public class WakeLockReceiver extends BroadcastReceiver {
11+
private static PowerManager.WakeLock wakeLock;
12+
13+
@Override
14+
public void onReceive(Context context, Intent intent) {
15+
String action = intent.getStringExtra("state");
16+
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
17+
18+
if (wakeLock == null) {
19+
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "WakeLockApp::ReceiverLock");
20+
}
21+
22+
if ("on".equals(action) && !wakeLock.isHeld()) {
23+
wakeLock.acquire();
24+
Log.i("WakeLockReceiver", "WakeLock acquired via broadcast");
25+
} else if ("off".equals(action) && wakeLock.isHeld()) {
26+
wakeLock.release();
27+
Log.i("WakeLockReceiver", "WakeLock released via broadcast");
28+
}
29+
}
30+
}

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 16 deletions
This file was deleted.

app/src/main/res/values/strings.xml

Lines changed: 0 additions & 3 deletions
This file was deleted.

app/src/main/res/values/themes.xml

Lines changed: 0 additions & 5 deletions
This file was deleted.

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
2+
// Top-level build file where you can add configuration options common to all sub-projects/modules.
13
buildscript {
24
repositories {
35
google()
46
mavenCentral()
57
}
68
dependencies {
7-
classpath "com.android.tools.build:gradle:7.4.1"
9+
classpath 'com.android.tools.build:gradle:8.1.0'
810
}
911
}
1012

0 commit comments

Comments
 (0)