2010-10-21 75 views
0

Greg爲什麼我的代碼中忽略了scheduleAtFixedRate ..?

這是我的新代碼根據您的建議。它不起作用。我收到了一堆「剛剛發送了帶有座標的短信」。所以它仍然沒有睡15秒。

package com.droid.service; 

import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Timer; import java.util.TimerTask;

import android.app.Service; import android.content.Context; import android.content.Intent; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.os.Looper;

公共類DroidService擴展服務 {

private LocationManager lm; 
private LocationListener locationListener; 
private Location location = null; 

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

// @Override 
// public void onCreate() 
// { 
// super.onCreate(); 
// initService(); 
// } 

private static final int PERIOD = 15000; 
Handler mHandler; 
Runnable mRunnable; 

@Override 
public void onCreate() 
{ 
    super.onCreate(); 
    mHandler = new Handler(); 
    mRunnable = new Runnable() 
    { 
     public void run() 
     { 
      updateNotification();   
      mHandler.postDelayed(this, PERIOD); 
      System.out.println("PAUSE FOR 15 SECONDS..!!"); 
     } 
    }; 
} 

@Override 
public void onStart(final Intent intent, final int startId) 
{ 
    super.onStart(intent, startId); 
    mRunnable.run(); 
} 

//私人無效initService() // {// 的System.out.println(「在initService..Droid服務...! 「); // int initialDelay = 15000; // 15秒後開始 // // int period = 300000; //每5分鐘重複一次 // // int period = 1800000; //每30分鐘重複一次 // int period = 15000; //每15秒重複測試一次 // Timer timer = new Timer(); // TimerTask task = new TimerTask() // { // public void run() // { // Looper.prepare(); // updateNotification(); // Looper.loop(); //} //}; // timer.scheduleAtFixedRate(task,initialDelay,period); //}

protected void updateNotification() 
{ 
    lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationListener = new MyLocationlistener(); 
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
    location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
} 

public static class DateUtils 
{ 

    public static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss"; 

    public static String now() 
    { 
     Calendar cal = Calendar.getInstance(); 
     SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW); 
     return sdf.format(cal.getTime()); 
    } 
} 

private class MyLocationlistener implements LocationListener 
{ 
    public void onLocationChanged(Location loc) 
    { 
     double lat = loc.getLatitude(); 
     double lon = loc.getLongitude(); 

     String latitude = Double.toString(lat); 
     String longitude = Double.toString(lon); 

     String coords = latitude + longitude; 

     // comment out text message for debug mode 
     // SmsManager sm = SmsManager.getDefault(); 
     // sm.sendTextMessage("phoneNumber", null, coords, null, null); 

     System.out.println("Just sent a text message with coords"); 
    } 

    public void onProviderDisabled(String provider) 
    { 
    } 

    public void onProviderEnabled(String provider) 
    { 

    } 

    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
    } 
} 

}

這裏是我的代碼

我得到「剛剛發與COORDS短信」像曾經即使我scheduleAtFixedRate設置爲10秒,每秒發送的。

我得到這個在一個正常的程序工作,但在Android平臺上,他們希望你使用Looper ....並且它看起來我的代碼已經被捕獲在這個Looper中,所以它忽略了我的scheduleAtFixedRate( 10秒)

任何幫助,非常感謝。

package com.droid.service; 

import java.util.Timer; 
import java.util.TimerTask; 

import android.app.Service; 
import android.content.Context; 
import android.content.Intent; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.os.IBinder; 
import android.os.Looper; 

public class DroidService extends Service 
{ 

    private LocationManager lm; 
    private LocationListener locationListener; 
    private Location location = null; 

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

    @Override 
    public void onCreate() 
    { 
     super.onCreate(); 
     initService(); 
    } 

    private void initService() 
    { 
     System.out.println("In initService..!!"); 
     int initialDelay = 15000; // start after 15 seconds 
     // int period = 300000; // repeat every 5 minuets 
     // int period = 1800000; // repeat every 30 minuets 
     int period = 15000; // repeat every 15 seconds for testing 
     Timer timer = new Timer(); 
     TimerTask task = new TimerTask() 
     { 
      public void run() 
      { 
       Looper.prepare(); 
       updateNotification(); 
       Looper.loop(); 
       Looper.myLooper().quit(); 
      } 
     }; 
     timer.scheduleAtFixedRate(task, initialDelay, period); 
    } 

    protected void updateNotification() 
    { 
     lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationListener = new MyLocationlistener(); 
     lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
     location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    } 

    private class MyLocationlistener implements LocationListener 
    { 
     public void onLocationChanged(Location loc) 
     { 
      double lat = loc.getLatitude(); 
      double lon = loc.getLongitude(); 

      String latitude = Double.toString(lat); 
      String longitude = Double.toString(lon); 

      String coords = latitude + longitude; 

      // comment out text message for debug mode 
      // SmsManager sm = SmsManager.getDefault(); 
      // sm.sendTextMessage("phoneNumber", null, coords, null, null); 

      System.out.println("Just sent a text message with coords"); 
     } 

     public void onProviderDisabled(String provider) 
     { 
     } 

     public void onProviderEnabled(String provider) 
     { 

     } 

     public void onStatusChanged(String provider, int status, Bundle extras) 
     { 
     } 
    } 
} 

回答

0

對我來說基本上最好的選擇就是打電話給這個onCreate。現在它正在做我想做的事情。感謝您的所有輸入..!這幫助我解決了這個問題。

0

嘗試從onStart()調用initService。

+0

來自onStart()的調用給了我相同的結果。 – Biggs 2010-10-22 04:11:24

+0

在調試時,您是否可以在onStart方法中獲得一箇中斷點? – Vinay 2010-10-22 13:58:02

1

我認爲這裏的誤解並不是Android想要使用Looper,但是你不應該使用Timer來滿足你期望的行爲。而不是定時器,你必須操作一個Looper。我有一段時間沒有直接使用Looper,因爲還有其他組件會將它們的細微差別抽象出來。不過我認爲這是Looper。loop()不應該返回,直到外部實體要麼中斷你的線程,要麼退出你的循環。所以我不認爲這會起作用,但我可能是錯的。

如果你想創建一個脈衝,你可以使用一個Handler。 Handler允許你在Looper線程上工作。 所以你所擁有的例子,

private static final int PERIOD = 15000; 
Handler mHandler; 
Runnable mRunnable; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    mHandler = new Handler(); 
    mRunnable = new Runnable() { 
     public void run() { 
     updateNotication(); 
     mHandler.postDelayed(this, PERIOD); 
     } 
    }; 
} 

我承認,從這裏開始了,我只是猜測,我知道你的應用程序所期望的結果。我假設你不需要雙向溝通,因爲你的IBinder爲空。

@Override 
    public void onStart() { 
     mRunnable.run(); 
    } 

     // The rest of your code. 
     .. 
+0

Greg,這是正確的。我的資料夾爲空。這是一個後臺服務,每30分鐘發送一次我的GPS位置。它應該繼續運行。我會嘗試你的意見並使用處理程序。謝謝,布賴恩 – Biggs 2010-10-21 12:20:29

+0

格雷格......這沒有奏效。這是我得到的輸出: – Biggs 2010-10-22 05:04:44

相關問題