簡體   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