繁体   English   中英

Android Java获取onStartCommand方法

[英]Android Java get onStartCommand method

您好,我的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的运行值更改为MainActivity内部的当前值(如果password等于456为false)。 我如何做到这一点。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM