2

My problem is... I have 2 events.. onCreate and Onclick. I have a thread running inside onCreate and i have to stop it inside onClick. Is this possible? If yes, please provide me with some hints/ code snippets how to implement this.

My code inside onCreate event is this:-

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_screen);

    button = (Button) findViewById(R.id.btn);
        button.setOnClickListener(this);
        try{
            bright = Settings.System.getInt(getContentResolver(), 
                     Settings.System.SCREEN_BRIGHTNESS);
            }catch(Exception ex){
                ex.printStackTrace();
                bright = 1.0f;
            }


        lp = getWindow().getAttributes();
         new Thread(new Runnable(){

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while (true) {
                try {
                    Thread.sleep(10000);
                    mHandler.post(new Runnable() {

                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            lp.screenBrightness= dim;
                            getWindow().setAttributes(lp);
                            // SLEEP 2 SECONDS HERE ...
                            final Handler handler = new Handler(); 
                            Timer t = new Timer(); 

                            t.schedule(new TimerTask() { 
                                    public void run() { 
                                            handler.post(new Runnable() { 
                                                    public void run() { 
                                                    lp.screenBrightness=bright;
                                                    getWindow().setAttributes(lp);
                                                    } 
                                            }); 
                                    } 
                            }, 2000); 

                        }
                    });
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }

        }

      }).start();  

    }

this activity is running inside a loop. And i want to stop this wen i click on the button. It seems that i cannot call the Thread.interrupt() method inside the button onclick event. How do i proceed with this?

4

4 に答える 4

1

参照を作成すると、クラスのどこからでもアクセスできます。したがって、クラス本体の広告では次のようになります

Thread thrd 

Button や TextView などで行うのと同じように。次に、 onCreate メソッドで次のような参照を追加します

thrd = new Thread(new Runnable(){

これで、クラスのどこでも使用できるスレッドへの参照ができました。

于 2013-02-22T12:05:30.817 に答える
1
  1. クラスで を宣言boolean mRunnningします。これはフラグのようなもので、実行中のカスタム スレッドはこれに依存して、キャンセルされたかどうかを判断します。

  2. いつものように、スレッドにはwhile(true)doの代わりにループがありますwhile(mRunning)

  3. また、スレッドをスリープ状態にしています。スリープ中は、スレッドはmRunning変数をチェックできませんinterrupt()。スレッドオブジェクトを呼び出して停止させる必要があります。

  4. 変数を使用してスレッド参照を保持することをお勧めしますThread t = new Thread(....

  5. したがって、スレッドを停止するには、呼び出しmRunning = falseてからt.interrupt().

于 2013-02-22T12:08:04.490 に答える
1

あなたの現在の状況に応じて、以前の回答コメントに基づいて. あなたは次のようにしようとしています:

myThread = new Thread(new Runnable

右?

.start();次に、以前のコーディングの一部も削除する必要があります。次に書く:

myThread.start();

myThreadクラス内のどこからでもこのスレッドにアクセスできるように、クラス内でグローバルとして宣言する必要があります。スレッドを停止できるようになりましたonClick

この回答と回答を組み合わせる必要がありUser117ます。

于 2013-02-22T13:15:45.893 に答える
0


while(true)の代わりに

run()メソッド内で、ブール変数をtrueに宣言し、onClickの後で変更します。例えば:

     private boolean shouldRun = true; 

次に、run()メソッドで:

      @Override
    public void run() {
        // TODO Auto-generated method stub
        while (shouldRun==true) {
                     .
                     . 
                     .

そしてあなたのonClickで:

        @override
          public void OnClick(View v){

              shouldRun=false;
                      .
                      .
                      .
于 2013-02-22T12:09:14.373 に答える