gpt4 book ai didi

java - Android Java获取onStartCommand方法

转载 作者:行者123 更新时间:2023-12-01 11:13:58 24 4
gpt4 key购买 nike

你好,我的 MainActivity.java 中有这个:

   @Override
public void onBackPressed() {
Context context = getApplicationContext();
CharSequence text = "myText";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
myDialog = new Dialog(this);
myDialog.setContentView(R.layout.dialog_signin);
myDialog.setCancelable(false);
password = (EditText) myDialog.findViewById(R.id.password);
myDialog.show();
Button lbtn = (Button) myDialog.findViewById(R.id.loginButton);
lbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Context context = getApplicationContext();
CharSequence passwordCorrect = "Password correct";
CharSequence passwordIncorrect = "Password wrong";
int duration = Toast.LENGTH_SHORT;
if (password.getText().toString().equals("456")) {
Toast.makeText(context, passwordCorrect, duration).show();
// onstartCommand method here
} else {
Toast.makeText(context, passwordIncorrect, duration).show();
// onstartCommand method here
}
}
});
}

这在我的 Kiosk.java 中:

@Override
public void onDestroy() {
Log.i(TAG, "Stopping service 'KioskService'");
running = false;
super.onDestroy();
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i(TAG, "Starting service 'KioskService'");
running = true;
ctx = this;

t = new Thread(new Runnable() {
@Override
public void run() {
do {
handleKioskMode();
try {
Thread.sleep(INTERVAL);
} catch (InterruptedException e) {
Log.i(TAG, "Thread interrupted: 'KioskService'");
}
} while (running);
stopSelf();
}
});

t.start();
return Service.START_NOT_STICKY;

}

我想将 onStartCommand 中的当前运行值更改为 true,如果密码等于 456,则将 MainActivity 中的运行值更改为 false。我该如何实现这一点。

最佳答案

创建新的Intent(Context, Kiosk.class)并调用intent.putExtra(String key, boolean value),然后使用Activity.starService(Intent)方法启动您的服务

关于java - Android Java获取onStartCommand方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32053661/

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