当gps位置可用时自动发送短信

时间:2014-10-17 06:48:16

标签: android gps smsmanager

我正在开发一个应用程序,用于在固定时间后通过短信发送用户位置,如果手机重新启动,应用程序应在后台自动启动而不启动任何启动器。我安静地完成了我的任务我只是在获取位置方面遇到问题,因为gps需要一些时间,而且当应用程序找不到任何位置时会崩溃。  这是我的主要活动代码

public class MainActivity extends Activity {

 @Override
    public void onCreate(Bundle savedInstanceState) 
     {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

    startLocationTracking();

}

private void startLocationTracking()
{
    AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
    Intent alarmintent1 = new Intent(MainActivity.this, AlarmReceiver.class);
    PendingIntent sender1=PendingIntent.getBroadcast(MainActivity.this, 100, alarmintent1, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);

    try {
        am.cancel(sender1);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("exjfkd"+e);
    }

    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.MINUTE,10);

    am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 1000*600, sender1);
    System.out.println("set timer");
}

}

public class AlarmReceiver extends BroadcastReceiver{


long time = 600* 1000; 
long distance = 10; 

@SuppressLint("NewApi") 

@Override
public void onReceive(final Context context, Intent intent) {

    System.out.println("alarm receiver....");
    Intent service = new Intent(context, MyService.class);
    context.startService(service);

    //Start App On Boot Start Up
    if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction())) {
    Intent App = new Intent(context, MainActivity.class);
    App.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(App);

 }
try{
    LocationManager locationManager = (LocationManager)context
            .getSystemService(Context.LOCATION_SERVICE);


    Criteria criteria = new Criteria();

    String provider = locationManager.getBestProvider(criteria, true);

    locationManager.requestLocationUpdates(provider, time,
            distance, locationListener);


    Location location = locationManager.getLastKnownLocation(provider);

    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    String device_id = tm.getDeviceId();  // returns IMEI number 


    String phoneNo = "+923362243969";

    String Text = "Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " Device Id: " + device_id;


    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(phoneNo, null, Text, null, null);
    Log.i("Send SMS", "");

 } 
    catch (Exception e) {
    e.printStackTrace();
 } 
    this.abortBroadcast();

}


LocationListener locationListener = new LocationListener() {

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }

    @Override
    public void onLocationChanged(Location location) {

    }
};

}

我在逻辑上有点混淆我希望我的应用程序在gps坐标可用时发送短信。如果在手机上禁用gps,如果手机上有网络,它应该通过网络获取位置并通过网络发送。

2 个答案:

答案 0 :(得分:1)

修改

你的应用程序崩溃了,因为重启后第一次GPS /网络将不会有自上次知识位置

Location location = locationManager.getLastKnownLocation(provider);

在此行中,您将收到位置为空。

String Text = "Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " Device Id: " + device_id;

并在此行中,您将获得空指针异常,因为location.getLatitude()不可用。

所以在此代码尝试检查位置之前!= null。

if(location!=null){
     String Text = "Latitude = " + location.getLatitude() +" Longitude = " + location.getLongitude() + " Device Id: " + device_id;
}

使用此方法了解GPS可用性的状态

public boolean isgpsavailable(Activity activity) {
        LocationManager locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
        boolean result = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        return result;
}

然后将您的代码放入if(isgpsavailable(this)){...}声明

答案 1 :(得分:0)

您可以使用android.telephony包发送短信。您可以在此处查看如何添加到项目中:How do I add ITelephony.aidl to eclipse?

这样的事情允许你发送textSMS:

SmsManager sms = SmsManager.getDefault();

sms.sendTextMessage(phoneNumber, scAddress, texto, null, null);

希望它可以帮到你!!