尝试计算 phone 被解锁的次数
Trying to count number of times the phone is unlocked
几个月前我厌倦了这个但是失败了。
我想做的是计算用户解锁他的 phone 的次数并将其显示在屏幕上,但每次我解锁 phone.[=26= 时我都会得到模糊的数字]
我的代码如下。
我主要activityoncreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(getApplicationContext(), LockService.class));
TextView ticker;
ticker = (TextView) findViewById(R.id.textView);
ticker.setText(String.valueOf(Tick.tick));
Log.e("but this is awesome ", String.valueOf(Tick.tick));
}
服务class
public class LockService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
final BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
return super.onStartCommand(intent, flags, startId);
}
public class LocalBinder extends Binder {
LockService getService() {
return LockService.this;
}
}
}
BroadcastReceiver Class
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
@Override
public void onReceive(final Context context, final Intent intent) {
Log.e("test", "onReceive");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wasScreenOn = false;
Log.e("test", "wasScreenOn" + wasScreenOn);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
wasScreenOn = true;
Log.e("test", "wasScreenOn and user present" + wasScreenOn);
} else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Tick.tick ++;
Log.e("test", "userpresent" + Tick.tick);
}
}
}
请帮助我理解我做错了什么
我相信发生的事情是这样的:
每次打开 activity 时,您都会调用启动服务。因此,即使您的服务已经 运行,也会调用 onStartCommand。这样,您可以多次注册您的广播,然后当您解锁设备时,您的计数器会随着每次注册接收者而递增。
您应该执行以下选项之一:
1. 在您的清单中定义您的接收者,这样您就不必每次都处理注册和注销。
2. 在您的服务 onCreate 而不是 onStartCommand 中注册您的接收者。另外,确保你在你的服务 onDestroy 中注销它们。
几个月前我厌倦了这个但是失败了。
我想做的是计算用户解锁他的 phone 的次数并将其显示在屏幕上,但每次我解锁 phone.[=26= 时我都会得到模糊的数字]
我的代码如下。
我主要activityoncreate
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startService(new Intent(getApplicationContext(), LockService.class));
TextView ticker;
ticker = (TextView) findViewById(R.id.textView);
ticker.setText(String.valueOf(Tick.tick));
Log.e("but this is awesome ", String.valueOf(Tick.tick));
}
服务class
public class LockService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_USER_PRESENT);
final BroadcastReceiver mReceiver = new ScreenReceiver();
registerReceiver(mReceiver, filter);
return super.onStartCommand(intent, flags, startId);
}
public class LocalBinder extends Binder {
LockService getService() {
return LockService.this;
}
}
} BroadcastReceiver Class
public class ScreenReceiver extends BroadcastReceiver {
public static boolean wasScreenOn = true;
@Override
public void onReceive(final Context context, final Intent intent) {
Log.e("test", "onReceive");
if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
wasScreenOn = false;
Log.e("test", "wasScreenOn" + wasScreenOn);
} else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
wasScreenOn = true;
Log.e("test", "wasScreenOn and user present" + wasScreenOn);
} else if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)) {
Tick.tick ++;
Log.e("test", "userpresent" + Tick.tick);
}
}
}
请帮助我理解我做错了什么
我相信发生的事情是这样的: 每次打开 activity 时,您都会调用启动服务。因此,即使您的服务已经 运行,也会调用 onStartCommand。这样,您可以多次注册您的广播,然后当您解锁设备时,您的计数器会随着每次注册接收者而递增。 您应该执行以下选项之一: 1. 在您的清单中定义您的接收者,这样您就不必每次都处理注册和注销。 2. 在您的服务 onCreate 而不是 onStartCommand 中注册您的接收者。另外,确保你在你的服务 onDestroy 中注销它们。