您好我的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;
}
如果密码等于456为false,我想在我的MainActivity中更改myStartCommand中当前为true的运行值。 我该如何做到这一点。
答案 0 :(得分:1)
创建新的Intent(Context,Kiosk.class)并调用intent.putExtra(String key,boolean value),然后用Activity.starService(Intent)方法启动你的服务