2012-10-09 41 views
1

我創建振動應用程序,併發出蜂鳴聲,每30秒,當我退出的振動和蜂鳴必須取消,當我登錄的振動和蜂鳴應該恢復。如何取消定時器並更新同一個定時器?

注:直到我登出

在我的應用我使用TimerTask此實現

這是利用振動和蜂鳴代碼必須振動和蜂鳴每30秒TimerTask

static TimerTask Task; 
final static Handler handler = new Handler(); 
static Timer t = new Timer(); 

public static void vib() { 

    Task = new TimerTask() { 
     public void run() { 
      handler.post(new Runnable() { 
       public void run() { 
        Vibrator vibrator = (Vibrator) ApplicationUtils.getContext().getSystemService(Context.VIBRATOR_SERVICE); 
        vibrator.vibrate(3000); 
        playSound(); 
        Log.d("TIMER", "Timer set on"); 
       } 
      }); 
     } 
    }; 
    t.schedule(Task, 0, 30000); 
} 

這是我使用的註銷部分代碼

public void stopvib() { 
    if (Task != null) { 
    // Log.d("TIMER", "timer canceled"); 
     t.cancel(); 
     Task.cancel(); 
    } 
} 

注:我也去掉了Task.cancel();但還是我收到同樣的錯誤

我的震動工作註銷之前罰款並重新登錄我歌廳錯誤

java.lang.IllegalStateException: Timer was cancelled 
    at java.util.Timer.scheduleImpl(Timer.java:562) 
    at java.util.Timer.schedule(Timer.java:481) 
    at com.vib(AlertListActivity.java:724) 

任何一個可以幫我這個編碼。我哪裏做錯了?

回答

3

我最近運行這段代碼並工作正常。這種使用廣播可以實現Receiver.You必須實現擴展TimerTask的獨立CustomTimer任務:

Activity mActivity=null; 
public MyCustomTimer(Activity mActivity) { 
    this.mActivity=mActivity; 
} 
    @Override 
    public void run() { 
     this.mActivity.runOnUiThread(new Runnable() { 

      @Override 
      public void run() { 
       Toast.makeText(mActivity, "Write you code here",Toast.LENGTH_LONG).show(); 
       Log.d("MyCustomTimer","Call"); 
      } 
     }); 

    } 

此之後,你必須實現廣播接收中要實現「VIB()」方法:類 讓說,在我的情況(只是舉例)是MainActivity:

public class MainActivity extends Activity { 
    private MyCustomTimer myCustomTimer = null; 
    BroadcastReceiver mBr_Start = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals("START_VIBRATION")) { 
       System.out.println("onreceive :START_VIBRATION"); 
       vib(); 
      } 
     } 
    }; 
    BroadcastReceiver mBr_Stop = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals("STOP_VIBRATION")) { 
       stopVibration(); 
      } 
     } 
    }; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     IntentFilter mIntentFilter = new IntentFilter(); 
     mIntentFilter.addAction("START_VIBRATION"); 
     registerReceiver(mBr_Start, mIntentFilter); 
     IntentFilter mIntentFilter2 = new IntentFilter(); 
     mIntentFilter2.addAction("STOP_VIBRATION"); 
     registerReceiver(mBr_Stop, mIntentFilter2); 

     Button b1 = (Button) findViewById(R.id.button1); 
     b1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent i = new Intent(MainActivity.this, MySecondActivity.class) 
         .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
       startActivity(i); 

      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    private void vib() { 
     myCustomTimer = new MyCustomTimer(MainActivity.this); 
     Timer timer = new Timer(); 
     timer.scheduleAtFixedRate(myCustomTimer, 0, 30000); 
    } 

    private void stopVibration() { 
     Log.d("MainActivity", "Before Cancel"); 
     if (null != myCustomTimer) 
      myCustomTimer.cancel(); 
     Log.d("MainActivity", "After Cancel"); 
    } 

} 

現在,你可以啓動或執行這行停振: 開始振動:

Intent i=new Intent("START_VIBRATION"); 
       mActivity.sendBroadcast(i); 

要停止:

Intent i=new Intent("STOP_VIBRATION"); 
       mActivity.sendBroadcast(i); 

注: MainActivity中的onDestroy()(在你的情況,如果您實現廣播接收器,註銷廣播接收器。)

+0

更新:在MyCustomTimer的run()方法中,你將調用playSound(); –

0

設置計時器實例爲空當您註銷,然後將其初始化每次用戶登錄的應用程序。這將解決「定時器被取消」的相關問題。

+0

其中一套計時器實例,u能幫助我BCZ我是新的android – Jagan

+0

我用t = null;但我正在逐漸顯示java.lang.NullPointerException錯誤,當我登錄PLZ幫我 – Jagan

0

爲什麼你需要一個靜態TimerTask.You可以給這樣這對我來說工作正常。

timer.schedule(new TimerTask() { 
     @Override 
     public void run() { 
     //your code 
     } 
     }, 0, 30000); 

雖然註銷使用,timer.cancel(). 在這裏你可以簡單地取消timer.No需要取消TimerTask的。

+0

我要撥打的VIB()方法將不同類 – Jagan

+0

,易建聯用靜態的TimerTask – Jagan

+0

然而,你都應該通過正確的應用程序只有一個定時器活躍?如果是這樣,你可以在應用程序中的任何地方調用靜態方法,每次最終都有一個新的TimerTask ..你可以將它保留爲靜態本身並在你的應用程序的任何地方使用timer.cancel()。 .. – hacker