2011-12-30 35 views
10

Tôi đang cố gắng hiển thị một msg mỗi 1 phút !! không ngừng! tôi thấy exemple hiển thị msg chỉ một lần sau một sự chậm trễ cố định !! bạn có thể giúp làm thế nào có thể thiết lập nó ?? hoặc nếu sử dụng bộ đếm thời gian thì tốt hơn nó hoạt động như thế nào tôi cần một ví dụ !!Trình xử lý hoặc hẹn giờ android

public class TimertestActivity extends Activity { 
    /** Called when the activity is first created. */ 

     @Override 
     public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     Handler handler = new Handler(); 
     handler.postDelayed(
      new Runnable() { 
       public void run() { 
        afficher(); 
       } 
      }, 1000L); 

     } 

     public void afficher() 
     { 
      Toast.makeText(getBaseContext(), 
        "test", 
        Toast.LENGTH_SHORT).show(); 
     } 
} 

Cảm ơn!

+0

Nếu bạn tìm thấy một giải pháp phù hợp với mình, xin vui lòng chấp nhận câu trả lời. –

Trả lời

24

Hãy thử mã này -

public class TimertestActivity extends Activity { 
    Handler handler = new Handler(); 
    Runnable runnable = new Runnable() { 
     public void run() { 
      afficher(); 
     } 
    }; 

    /** Called when the activity is first created. */ 

     @Override 
     public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 
     runnable.run(); 
     } 

     public void afficher() 
     { 
      Toast.makeText(getBaseContext(), 
        "test", 
        Toast.LENGTH_SHORT).show(); 
      handler.postDelayed(runnable, 1000); 
     } 
} 
+1

yaaaaaaah tuyệt vời nó hoạt động chính xác !! cảm ơn rất nhiều :) –

+0

Tuyệt vời là nó giúp bạn. Xin vui lòng cũng chấp nhận câu trả lời nếu nó giúp. – anujprashar

+0

làm cách nào tôi có thể thực hiện việc này ??? –

1

Bạn có thể sử dụng TimerTask cho this.But khi điện thoại của bạn đi ngủ nó sẽ không làm việc vì vậy tôi nghĩ rằng bạn có thể sử dụng AlarmManager cho việc này. Dù sao tham khảo this link cho TimerTask,

AlarmManager mã,

AlarmManager am = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE); 
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
       SystemClock.elapsedRealtime(), interval, pendingIntent); 
+0

Nhưng trong quản lý báo thức bạn không thể hiển thị tin nhắn bánh mì nướng. Nó sẽ hiển thị hoạt động mới. nhưng điều này sẽ hữu ích cho bạn. –

2
// Timer using Handler 

private final int SPLASH_TIME = 3000; 

// Handling splash timer. 
private void startSplashTimer() { 
    new Handler().postDelayed(
    new Runnable() { 
    @Override 
    public void run() { 
     startActivity(new Intent(SplashScreen.this,MainActivity.class)); 
    } 
}, SPLASH_TIME); 

}

Các vấn đề liên quan