2012-07-17 18 views
0

我正在從事沒有任何活動的服務。它只在後臺工作。它接收經緯度並通過郵件發送。問題是程序沒有發送郵件,我正在使用Java Mail API。下面是代碼:從gps獲取位置併發送至郵件服務無活動

它的主要活動

public class MainActivity extends Service { 

    LocationManager lm; 
    LocationListener lis; 
    Location loc; 
    Double lat; 
    Double lan; 
    Location location; 
    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1; // in Meters 
    private static final long MINIMUM_TIME_BETWEEN_UPDATES = 10000; // in Milliseconds 
LocationManager locationManager; 



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

    @Override 
    public void onStart(Intent intent, int startId) { 
     // TODO Auto-generated method stub 
     super.onStart(intent, startId); 

     locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

     locationManager.requestLocationUpdates(
            LocationManager.GPS_PROVIDER, 
            MINIMUM_TIME_BETWEEN_UPDATES, 
            MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, 
           new MyLocationListener() 
         ); 



     sendMail(showcurrentlocation()); 



    } 


    private String showcurrentlocation() { 
     // TODO Auto-generated method stub 
     Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 

     String message = null; 
     if (location != null) { 
message = String.format(
           "Current Location \n Longitude: %1$s \n Latitude: %2$s", 
           location.getLongitude(), location.getLatitude() 
          ); 

        } 


return message; 

    } 

    public void sendMail(String msg) { 
     try { 
      GMailSender sender = new GMailSender("user", 
        "password"); 
      sender.sendMail("subject",body, "sender", 
        "recepients"); 
     } catch (Exception e) { 
      Log.e("SendMail", e.getMessage(), e); 
     } 

    } 

    private class MyLocationListener implements LocationListener { 

       public void onLocationChanged(Location location) { 
        String message = String.format(
         "New Location \n Longitude: %1$s \n Latitude: %2$s", 
          location.getLongitude(), location.getLatitude() 

          ); 

        Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show(); 
       } 

       public void onStatusChanged(String s, int i, Bundle b) { 
        Toast.makeText(MainActivity.this, "Provider status changed", 
         Toast.LENGTH_LONG).show(); 
        } 

       public void onProviderDisabled(String s) { 
       Toast.makeText(MainActivity.this, 
           "Provider disabled by the user. GPS turned off", 
         Toast.LENGTH_LONG).show(); 
        } 

        public void onProviderEnabled(String s) { 
        Toast.makeText(MainActivity.this, 
          "Provider enabled by the user. GPS turned on", 
           Toast.LENGTH_LONG).show(); 
       } 

     } 




} 

另一個類廣播接收機

public class MyBroadcastReceiver extends BroadcastReceiver{ 

    public static final String TAG = "LocationLoggerServiceManager"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     Intent service = new Intent(context, MainActivity.class); 
     context.startService(service); 

     } 

}public class MyBroadcastReceiver extends BroadcastReceiver{ 

    public static final String TAG = "LocationLoggerServiceManager"; 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     // TODO Auto-generated method stub 

     Intent service = new Intent(context, MainActivity.class); 
     context.startService(service); 

     } 

} 
+0

有什麼問題?你從哪裏得到錯誤?它是編譯錯誤還是運行時錯誤?提供LOGCAT! – theAlse 2012-07-17 11:30:52

回答

0

有沒有必要寫this.Android廣播接收器,如果你提提供了更新的位置您想要更新位置的距離/時間。並且請在您的位置更改後,檢查您的sendMail()是否被調用。

+0

它不工作,問題是sendMail(showcurrentlocation)不工作proporly任何人都幫助我。或發送郵件給我發送郵件樣本 – 2012-07-20 08:09:06

+0

@PiyushSengar發送郵件,你可以參考這個鏈接「http://www.mkyong.com/android/how-to-send-email-in-android/」,因爲你是使用Java郵件API PLZ閱讀鏈接「http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a」 – Akshay 2012-07-21 14:03:33