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

问题描述 投票:0回答:3

我正在开发一个应用程序,在固定时间后通过短信发送用户位置,如果手机重新启动,该应用程序应在后台自动启动,而无需任何启动器活动。我已经安静地完成了我的任务,我只面临获取位置的问题,因为 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,如果手机上有可用网络,则应该通过网络获取位置并通过网络发送。

android gps smsmanager
3个回答
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)){...}
语句


0
投票

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

类似这样的东西允许您发送短信:

SmsManager sms = SmsManager.getDefault();

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

希望对你有帮助!!


0
投票

7AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE); 意图alarmintent1 = new Intent(MainActivity.this, AlarmReceiver.class); PendingIntent sender1=PendingIntent.getBroadcast(MainActivity.this, 100, Alarmintent1, PendingIntent.FLAG_UPDATE_CURRENT | Intent.FILL_IN_DATA);

© www.soinside.com 2019 - 2024. All rights reserved.