gpt4 book ai didi

android - handler.removeCallbacksAndMessages 不工作

转载 作者:行者123 更新时间:2023-11-29 00:10:48 27 4
gpt4 key购买 nike

我的应用程序有一个注册 ScreenStateReceiver 的服务,它会在屏幕打开时创建一个处理程序,并在屏幕关闭时删除它。

我的问题是 handler.removeCallbacksAndMessages 在屏幕关闭时不起作用。我知道这一点,因为每当屏幕打开时,我都会从创建的所有处理程序中获取多个日志。

我的代码:

public class mainActivity extends ActionBarActivity {

static Button setButton;
static Context activityContext;

static SharedPreferences preferences;
static SharedPreferences.Editor editor;

static long handlerDelay;
static Handler handler;
static Runnable notificationRunnable;
static Runnable lockRunnable;
static String handlerType;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

setButton = (Button) findViewById(R.id.setButton);
activityContext = this;

preferences = getSharedPreferences("values", Context.MODE_PRIVATE);
editor = preferences.edit();

setButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

setButton.setEnabled(false);
setButton.setText("Done");
editor.putBoolean("appSet", true).apply();

handlerDelay = 10000;
editor.putLong("handlerDelay", handlerDelay).apply();

editor.putString("handlerType", "Notification").apply();

Intent intentService = new Intent(activityContext, mainActivity.service.class);
startService(intentService);

lockRunnable = new Runnable() {
@Override
public void run() {
Toast.makeText(activityContext, "Lock Handler", Toast.LENGTH_SHORT).show();
}
};

handler = new Handler();
notificationRunnable = new Runnable() {
@Override
public void run() {
Toast.makeText(activityContext, "Notification Handler", Toast.LENGTH_SHORT).show();

editor.putString("handlerType", "lock").apply();
editor.putLong("handlerDelay", 10 * 1000).apply();
handlerDelay = preferences.getLong("handlerDelay", 0);
handler.postDelayed(lockRunnable, handlerDelay);
}
};
handler.postDelayed(notificationRunnable, handlerDelay);
}
});
}

static public class screenStateReceiver extends BroadcastReceiver {

@Override
public void onReceive(final Context context, Intent intent) {

preferences = context.getSharedPreferences("values", Context.MODE_PRIVATE);
editor = preferences.edit();

handler = new Handler();

if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {

handler.removeCallbacksAndMessages(null);
}

if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {

handlerDelay = preferences.getLong("handlerDelay", 0);
editor.putLong("handlerDelay", handlerDelay).apply();

handlerType = preferences.getString("handlerType", "notification");

if(!(handlerDelay < 0)) {
if (handlerType.equals("notification")) {
notificationRunnable = new Runnable() {
@Override
public void run() {
Toast.makeText(context, "Notification Handler", Toast.LENGTH_SHORT).show();

editor.putString("handlerType", "lock").apply();
editor.putLong("handlerDelay", 10 * 1000).apply();
handlerDelay = preferences.getLong("handlerDelay", 0);
handler.postDelayed(lockRunnable, handlerDelay);
}
};
handler.postDelayed(notificationRunnable, handlerDelay);
}

else if(handlerType.equals("lock")) {
lockRunnable = new Runnable() {
@Override
public void run() {
if(handlerType.equals("lock")) {
Toast.makeText(context, "Lock Handler", Toast.LENGTH_SHORT).show();
Log.e("lock handler", "Lock Handler");
editor.putString("handlerType",null).apply();
}
}
};
handler.postDelayed(lockRunnable, handlerDelay);
}
}
}
}
}

static public class service extends Service {

BroadcastReceiver mReceiver;

@Override
public void onCreate() {
super.onCreate();
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
mReceiver = new screenStateReceiver();
registerReceiver(mReceiver, filter);
}

@Override
public void onDestroy(){
super.onDestroy();
unregisterReceiver(mReceiver);
}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}
}

应用应该在启动第二个 runnable 后启动另一个 runnable。

最佳答案

这是因为在 BroadcastRecevier 中您再次实例化处理程序。您应该删除 handler = new Handler();,否则您将在不同的 Handler 上调用 removeCallbacks

关于android - handler.removeCallbacksAndMessages 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30289831/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com