GPS 및 Wi-Fi를 사용하여 사용자 위치를 계산하는 서비스를 만들었습니다 (직접 작성). 계산이 완료되면 SMS가 다시 사용자에게 전송됩니다. 이 서비스는 SMS에 의해 시작됩니다. Wi-Fi와 GPS는 자체적으로 잘 작동합니다. 그러나 함께 결합하면 오류가 발생합니다.위치 및 타이머 및 nullpointerexception
실제로 두 개의 오류가 있습니다. 첫 번째는 java.lang.NullPointerException입니다.
그리고 다음 다시 내 응용 프로그램 충돌과 나는이있어 : java.lang.RuntimeException가이 : 널 (null)로 서비스 [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");
}
}