2012-03-03 44 views
0

我建立了一項服務,其中使用GPS和wifi(我自己寫的)計算用戶的位置。計算完成後,SMS會發回給用戶。請注意,此服務是由SMS啓動的。 WIFI和GPS可以正常工作。但是當組合在一起時會出錯。位置和定時器和nullpointerexception

其實我得到了兩個錯誤,第一個是:java.lang.NullPointerException。

然後我的應用程序再次崩潰,我得到這個:了java.lang.RuntimeException:無法啓動服務[email protected]與空:顯示java.lang.NullPointerException

好像GPS正在嘗試在第一個服務完成後啓動另一個服務。我嘗試了很多方法,但無法解決這個問題。有人請舉手。

下面是代碼

public class ScanService extends Service { 
    WifiManager wifi; 
    TimerTask timerTask1; 
    private static Timer timer = new Timer(); 
    String receipt; 
    String message = ""; 
    int count = 0; 
    Location location; 
    String gpsInfo = ""; 
    LocationManager locationManager; 

@Override 
public void onCreate() { 
    wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
    timer = new Timer(); 
    timerTask1 = new TimerTask() { 
     @Override 
     public void run() { 
      // TODO Auto-generated method stub 

      Log.d("timer", "timer alright"); 
      if (count >=0 && count <= 24) { 
       count++; 
       scan();//do the scanning 
      } 
      else if (count == 25) { 
       locating();//do the calculation 
       sendSMS(); 

      } 
     } 
    }; 
    Criteria criteria = new Criteria(); 
    criteria.setAccuracy(Criteria.ACCURACY_FINE); 
    criteria.setCostAllowed(true); 
    criteria.setPowerRequirement(Criteria.POWER_LOW); 
    String provider = locationManager.getBestProvider(criteria, true); 
    location = locationManager.getLastKnownLocation(provider); 
    if (location != null) { 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
     message += " http://ditu.google.cn/?q=" + lat + "," + lng; 
    } 
    else { 
     message += "unable to get gps info"; 
    } 
} 

@Override 
public void onStart(Intent intent, int startId) { 
    super.onStart(intent, startId); 
    Toast.makeText(this, "service onStart", 0).show(); 
    wifi.setWifiEnabled(false); 
    wifi.setWifiEnabled(true); 
    timer.scheduleAtFixedRate(timerTask1, 30000, 500); 

} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    timer.cancel(); 
    Toast.makeText(this, "service onDestroy", Toast.LENGTH_LONG).show(); 
} 
public void sendSMS() { 
    String SENT = "SMS_SENT"; 
    String DELIVERED = "SMS_DELIVERED"; 
    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); 
    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, new Intent(DELIVERED), 0); 
    Log.d("sendSMS", "in sndSMS"); 
    //Log.d("message", message); 

    BroadcastReceiver sentReceiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch(getResultCode()) { 
      case Activity.RESULT_OK: 
       Toast.makeText(getBaseContext(), "SMS sent", 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
       Toast.makeText(getBaseContext(), "Generic failure", 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NO_SERVICE: 
       Toast.makeText(getBaseContext(), "No service", 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NULL_PDU: 
       Toast.makeText(getBaseContext(), "Null PDU", 
         Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_RADIO_OFF: 
       Toast.makeText(getBaseContext(), "Radio off", 
         Toast.LENGTH_SHORT).show(); 
       break; 
      } 
     } 

    }; 

    BroadcastReceiver deliveredReceiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      switch(getResultCode()) { 
      case Activity.RESULT_OK: 
       Toast.makeText(getBaseContext(), "SMS delivered", 
         Toast.LENGTH_SHORT).show(); 
       stopSelf(); 
       break; 
      case Activity.RESULT_CANCELED: 
       Toast.makeText(getBaseContext(), "SMS not delivered", 
         Toast.LENGTH_SHORT).show(); 
       stopSelf(); 
       break; 
      } 

     } 

    }; 
    registerReceiver(sentReceiver, new IntentFilter(SENT)); 
    registerReceiver(deliveredReceiver, new IntentFilter(DELIVERED)); 
    SmsManager sms = SmsManager.getDefault(); 
    sms.sendTextMessage(receipt, null, message, sentPI, deliveredPI); 

    timer.cancel(); 
    Log.d("timer", "timer cancelled"); 
} 

}

回答

0

數字出來了!短信發送太久了!添加下面的代碼後,問題就解決了!

PendingIntent pi = PendingIntent.getActivity(this, 0, new Intent(this, ScanService.class), 0);     
    SmsManager sms = SmsManager.getDefault(); 
    List<String> texts = sms.divideMessage(message); 
    for (String text:texts) { 

     sms.sendTextMessage(receipt, null, text, pi, null); 
    }