2012-05-08 98 views
7

我要保持我的應用程序在後臺運行,運行
我有將用戶的位置,我們的服務器應用程序
我有以下代碼:保持應用程序在後臺

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    LocationManager locationManager; 
    String context = Context.LOCATION_SERVICE; 
    locationManager = (LocationManager)getSystemService(context); 

    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    criteria.setAltitudeRequired(false); 
    criteria.setBearingRequired(false); 
    criteria.setCostAllowed(true); 
    criteria.setPowerRequirement(Criteria.POWER_LOW); 
    String provider = locationManager.getBestProvider(criteria, true); 

    updateWithNewLocation(null); 

    locationManager.requestLocationUpdates(provider, (10*60*1000), 10, 
              locationListener); 
} 
private final LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     updateWithNewLocation(location); 
    } 

    public void onProviderDisabled(String provider){ 
     updateWithNewLocation(null); 
    } 

    public void onProviderEnabled(String provider){ } 
    public void onStatusChanged(String provider, int status, 
           Bundle extras){ } 
    }; 
    public void updateWithNewLocation(Location location) { 


     if (location != null) { 
      Dbhelper helper = new Dbhelper(this); 
      final SQLiteDatabase db = helper.getWritableDatabase(); 
      long time = System.currentTimeMillis(); 
      SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss"); 
      final String curTime = df.format(time); 
      final double lat = location.getLatitude(); 
      final double lng = location.getLongitude(); 
      final double alt = location.getAltitude(); 
      System.out.println(lat); 
      System.out.println(lng); 
      System.out.println(alt); 
      db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " + 
      "('"+lng+"','"+lat+"','"+alt+"','"+curTime+"')"); 
      db.close(); 
      /*Timer timer = new Timer(); 
      timer.scheduleAtFixedRate(new TimerTask(){ 
       @Override 
       public void run(){ 
        db.execSQL("INSERT INTO location (longitude,latitude,altitude,tgl_buat) VALUES " + 
          "('121.2149012','-6.217837','0.0','2012-05-07 10:20:01')"); 
        db.close(); 
       } 
      }, 10*60*1000, 10*60*1000);*/ 

      } 
     } 

我想我applicatioin在後臺運行。我希望它在手機啓動時自動啓動

+0

在Android中有一個稱爲Service的組件。 Android documnetation說**服務是一個應用程序組件,可以在後臺執行長時間運行的操作,並且不提供用戶界面**。爲了自動啓動你的應用程序,你可以使用AlarmManager。 –

回答

13

對於您的問題,一個非常簡單的答案是使用Service。它將允許你在後臺執行各種任務,並且是你最好的選擇無聲地發送你的位置到服務器

閱讀this尋求幫助。

+0

你能給我一個簡單的示例代碼嗎?謝謝:)) – akubabas

+0

+1對於簡單和很好的答案:) – Lucifer

+1

看到我更新的答案或閱讀[this](http://stackoverflow.com/a/2779589/966550) – waqaslam

0

Service中運行您的後臺邏輯,並且如果您想提供良好的UX體驗(並且也具有更高的優先級),請將Notification發佈到狀態欄(使用NotificationManager)。

0

你也可以在後臺運行,使用Service

您的應用程序,我希望這link將幫助您

請閱讀documentation進一步的細節

0

你必須使用一個服務和廣播接收器

+1

你能給我一個簡單的代碼到這個??謝謝:) – akubabas

+0

@deadbabaz你可以很容易地從Android的開發者網站獲得服務和廣播接收器的代碼。 – Dharmendra

0

GrabLocationDetails.java

使用此代碼作爲您的GrabLocationDetails.java

public class GrabLocationDetails extends Service implements LocationListener { 

     double lat,lng; 
     private LocationManager locationManager; 
     private String provider; 
     boolean isGps; 
     private ArrayList<String> mList; 
     Context GLDContext; 

     public GrabLocationDetails(Context cont){ 
      this.GLDContext=cont; 
     } 
     public GrabLocationDetails(){} 
     @Override 
     public void onCreate() { 
      super.onCreate(); 
      mList = new ArrayList<String>(); 
      isGps = false; 
      lat=0.0; 
      lng=0.0; 
     } 

     @Override 
     public int onStartCommand(Intent intent, int flags, int startId) { 

      //super.onStart(intent, startId); 

      try { 
       locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
       Criteria criteria = new Criteria(); 
       provider = locationManager.getBestProvider(criteria, false); 
       LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); 
       boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER); 
       if (!enabled) { 
        isGps = false; 
        ListAddItem(isGps); 
        SendBroadcast(); 
       } else { 
        isGps = true; 
        Location location = locationManager.getLastKnownLocation(provider); 
        lat=(location.getLatitude()); 
        lng=(location.getLongitude()); 
        ListAddItem(true); 
        SendBroadcast(); 
        locationManager.requestLocationUpdates(provider, 400, 1, this); 
       } 

      } catch (Exception e) { 
       ListAddItem(isGps); 
       SendBroadcast(); 
      } 
      return super.onStartCommand(intent, flags, startId); 
     } 

     @Override 
     public void onDestroy() { 
      super.onDestroy(); 
      //locationManager.removeUpdates(this); 
     } 

     public void SendBroadcast(){ 
      Intent broadcastIntent = new Intent(); 
      broadcastIntent.setAction(CommandExecutionModule.LocationDetails); 
      broadcastIntent.putExtra("Data", mList); 
      sendBroadcast(broadcastIntent); 

     } 
     public void ListAddItem(boolean GPS) { 
      //if(!GPS) 
      //mList.add("0"); 
      //else 
      //mList.add("1"); 
      mList.add(Double.toString(lat)); 
      mList.add(Double.toString(lng)); 
     } 

     /**************************************************************************************************************/ 

     @Override 
     public void onLocationChanged(Location location){ 
      locationManager.requestLocationUpdates(provider, 400, 1, this); 
      mList.clear(); 
      lat = (location.getLatitude()); 
      lng = (location.getLongitude()); 
      ListAddItem(isGps); 
      SendBroadcast(); 
      locationManager.removeUpdates(this); 
      stopSelf(); 
     } 

     @Override 
     public void onStatusChanged(String provider, int status, Bundle extras) { 
      locationManager.requestLocationUpdates(provider, 400, 1, this); 
     } 

     @Override 
     public void onProviderEnabled(String provider){ 
      isGps=true; 
     } 
     @Override 
     public void onProviderDisabled(String provider){ 
      isGps=false; 
      lat=0.0; 
      lng=0.0; 
      mList.clear(); 
      ListAddItem(isGps); 
      //SendBroadcast(); 
     } 
+0

在您的通話類中啓動此服務並註冊一個廣播接收器 –

相關問題