2012-03-03 12 views
0

GPSとWi-Fiを使ってユーザの位置を計算するサービスを作成しました。計算が完了すると、SMSがユーザーに送り返されます。このサービスはSMSによって開始されることに注意してください。 Wi-FiとGPSはそれ自身でうまく動作します。しかし、一緒に組み合わされるとエラーになる。位置とタイマーとnullpointerexception

実際には2つのエラーがあります。最初は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

フィギュアそれをです! SMSは送信するには長すぎます!次のコードを追加した後、問題は解決されます!

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); 
    }