-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathMainActivity.java
More file actions
165 lines (138 loc) · 6.26 KB
/
MainActivity.java
File metadata and controls
165 lines (138 loc) · 6.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package com.sample.battery;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.sample.battery.alarm.IAlarmManagerHook;
import com.sample.battery.alarm.MyAlarmReceiver;
import com.sample.battery.gps.ILocationManagerHook;
import com.sample.battery.wakelock.IPowerManagerHook;
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
public class MainActivity extends Activity {
public static Context sContext;
private PowerManager.WakeLock wakeLock;
private LocationManager locationManager;
private LocationListener locationListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sContext = getApplicationContext();
final Button hookAlarm = (Button) findViewById(R.id.hook_alarm);
hookAlarm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new IAlarmManagerHook(sContext).onInstall();
}
});
final Button hookWakelock = (Button) findViewById(R.id.hook_wakelock);
hookWakelock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new IPowerManagerHook(sContext).onInstall();
}
});
final Button hookGPS = (Button) findViewById(R.id.hook_gps);
hookGPS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new ILocationManagerHook(sContext).onInstall();
}
});
final Button wakelockAcquire = (Button) findViewById(R.id.wakelock_acquire);
wakelockAcquire.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PowerManager powerManager = (PowerManager) sContext.getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, this.getClass().getName());//持有唤醒锁
wakeLock.setReferenceCounted(false);
wakeLock.acquire(60 * 1000); //亮屏60s
}
});
final Button wakelockRelease = (Button) findViewById(R.id.wakelock_release);
wakelockRelease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (wakeLock != null){
wakeLock.release();//释放锁,灭屏
}
}
});
final Button alarmSet = (Button) findViewById(R.id.alarm_set);
alarmSet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlarmManager alarmService = (AlarmManager) sContext.getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(sContext, MyAlarmReceiver.class).setAction("intent_alarm");
PendingIntent broadcast = PendingIntent.getBroadcast(sContext, 0, alarmIntent, 0);//通过广播接收
alarmService.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 2000, broadcast);//INTERVAL毫秒后触
}
});
final Button alarmCancel = (Button) findViewById(R.id.alarm_cancel);
alarmCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlarmManager alarmService = (AlarmManager) sContext.getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(sContext, MyAlarmReceiver.class).setAction("intent_alarm");
PendingIntent broadcast = PendingIntent.getBroadcast(sContext, 0, alarmIntent, 0);
alarmService.cancel(broadcast);
}
});
final Button gpsRequest = (Button) findViewById(R.id.gps_request);
gpsRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(MainActivity.this, ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{ACCESS_FINE_LOCATION}, 100);
} else {
locationManager = (LocationManager) sContext.getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 3000, 1, locationListener);
}
}
});
final Button gpsRemove = (Button) findViewById(R.id.gps_remove);
gpsRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (locationManager != null && locationListener != null) {
// 关闭程序时将监听器移除
locationManager.removeUpdates(locationListener);
}
}
});
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
Log.e("HOOOOOOOOK", location.toString());
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
};
}
@Override
public void onRequestPermissionsResult(
int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}