2017-02-24 105 views
0

因此,我发现这个后台服务代码不断弹出,每10秒敬酒一次。我想要问的是翻译此代码弹出分钟间隔而不是10秒的时间间隔,而不是弹出的祝酒,我希望它在间隔结束时弹出一个活动。从服务启动一项活动(更改时间间隔) - Android

public class TimeService extends Service { 
    // constant 
    public static final long NOTIFY_INTERVAL = 10 * 1000; // 10 seconds 

    // run on another Thread to avoid crash 
    private Handler mHandler = new Handler(); 
    // timer handling 
    private Timer mTimer = null; 

    @Override 
    public IBinder onBind(Intent intent) { 
     return null; 
    } 

    @Override 
    public void onCreate() { 
     // cancel if already existed 
     if(mTimer != null) { 
      mTimer.cancel(); 
     } else { 
      // recreate new 
      mTimer = new Timer(); 
     } 
     // schedule task 
     mTimer.scheduleAtFixedRate(new TimeDisplayTimerTask(), 0, NOTIFY_INTERVAL); 
    } 

    class TimeDisplayTimerTask extends TimerTask { 

     @Override 
     public void run() { 
      // run on another thread 
      mHandler.post(new Runnable() { 

       @Override 
       public void run() { 

        // display toast 
        Toast.makeText(getApplicationContext(), getDateTime(), 
          Toast.LENGTH_SHORT).show(); 
       } 

      }); 
     } 

     private String getDateTime() { 
      // get date time in custom format 
      SimpleDateFormat sdf = new SimpleDateFormat("[yyyy/MM/dd - HH:mm:ss]"); 
      return sdf.format(new Date()); 
     } 


    } 
... 
} 
+0

弹出的活动?我想你需要'popupwindow'或'alertdialog' – Redman

+0

可能重复的[android服务启动活动](http://stackoverflow.com/questions/3606596/android-start-activity-from-service) – Skynet

回答

0
public static final long NOTIFY_INTERVAL = 10 * 1000 * 60; 
0

10 * 1000是10 seconds.Time总是算作毫秒。所以 变化10 * 1000 10 * 1000 * 60

public static final long NOTIFY_INTERVAL = 10 * 1000 * 60;