Android 只发送一次GPS定位短信

Android 只发送一次GPS定位短信,android,gps,coordinates,Android,Gps,Coordinates,我正在做一个应用程序,它在手机的后台运行。当我用HDPK GPS发送短信时,应用程序应该以固定的数字发送小区的GPS坐标。我收到了数字上的GPS坐标,但连续不断。我用RemoveUpdate尝试了一切,但都是徒劳的!请帮我解决这个问题。此外,当应用程序在后台侦听SMS时,GPS和应用程序在收到消息时也会崩溃,尽管在崩溃之前我只收到了祝酒词。但如果应用程序在屏幕上运行,它将不会崩溃并持续发送坐标信息 public class RecActivity extends Activity { doubl

我正在做一个应用程序,它在手机的后台运行。当我用HDPK GPS发送短信时,应用程序应该以固定的数字发送小区的GPS坐标。我收到了数字上的GPS坐标,但连续不断。我用RemoveUpdate尝试了一切,但都是徒劳的!请帮我解决这个问题。此外,当应用程序在后台侦听SMS时,GPS和应用程序在收到消息时也会崩溃,尽管在崩溃之前我只收到了祝酒词。但如果应用程序在屏幕上运行,它将不会崩溃并持续发送坐标信息

public class RecActivity extends Activity {
double current_lat, current_lng;
boolean flag=true;
// String provider=LocationManager.GPS_PROVIDER;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
BroadcastReceiver SMSbr = new BroadcastReceiver() {

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

Bundle bundle = intent.getExtras();
if (bundle != null) {

Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (messages.length > -1) {
String messagebody=messages[0].getMessageBody();
if(messagebody.toString().matches("HDPK GPS"))
{

LocationManager mlocManager = (LocationManager)getSystemService(RecActivity.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();                   

Toast.makeText(RecActivity.this,"GPS STARTED", Toast.LENGTH_LONG)
                                        .show();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,1, mlocListener);                                 

}

                                                                                           }

}
}

};

IntentFilter SMSfilter = new IntentFilter(SMS_RECEIVED);
this.registerReceiver(SMSbr, SMSfilter);
}

public class MyLocationListener implements LocationListener
{

public void onLocationChanged(Location loc) {
Toast.makeText(RecActivity.this,"GPS WORKING", Toast.LENGTH_LONG).show();
current_lat=loc.getLatitude();
current_lng=loc.getLongitude();
String Text = "My location is: " +

"Latitude = " + current_lat +

"Longitude = " + current_lng;

SmsManager sender=SmsManager.getDefault();
sender.sendTextMessage("9762281814",null,Text , null, null);
Toast.makeText(RecActivity.this, "SMS SENT", Toast.LENGTH_LONG).show();



}



public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
SmsManager sender=SmsManager.getDefault();
sender.sendTextMessage("9762281814",null,"GPS Disabled" , null, null);

}

public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
SmsManager sender=SmsManager.getDefault();
sender.sendTextMessage("9762281814",null,"GPS Enabled" , null, null);

}

 public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub

}

}

}
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".RecActivity"
android:label="@string/app_name" >
<intent-filter android:priority="100">
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
public class RecActivity扩展活动{
双电流水平,电流水平;
布尔标志=真;
//字符串提供程序=LocationManager.GPS\U提供程序;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
最后一个字符串SMS_RECEIVED=“android.provider.Telephony.SMS_RECEIVED”;
BroadcastReceiver SMSbr=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
Bundle=intent.getExtras();
if(bundle!=null){
Object[]pdus=(Object[])bundle.get(“pdus”);
最终SmsMessage[]消息=新SmsMessage[pdus.length];
对于(int i=0;i-1){
字符串messagebody=消息[0]。getMessageBody();
if(messagebody.toString()匹配(“HDPK GPS”))
{
LocationManager mlocManager=(LocationManager)getSystemService(RecActivity.LOCATION\u服务);
LocationListener mlocListener=新建MyLocationListener();
Toast.makeText(RecActivity.this,“GPS开始”,Toast.LENGTH\u LONG)
.show();
mlocManager.RequestLocationUpdate(LocationManager.GPS_提供程序,1000,1,mlocListener);
}
}
}
}
};
IntentFilter SMSfilter=新的IntentFilter(收到SMS_);
该注册接收服务器(SMSbr、SMSfilter);
}
公共类MyLocationListener实现LocationListener
{
位置更改后的公共无效(位置loc){
Toast.makeText(RecActivity.this,“GPS工作”,Toast.LENGTH_LONG.show();
当前纬度=loc.getLatitude();
当前_lng=loc.getLongitude();
String Text=“我的位置是:”+
“纬度=”+当前纬度+
“经度=”+当前液化天然气;
SmsManager sender=smsmsmanager.getDefault();
sendTextMessage(“9762281814”,null,Text,null,null);
Toast.makeText(RecActivity.this,“SMS已发送”,Toast.LENGTH_LONG.show();
}
公共无效onProviderDisabled(字符串arg0){
//TODO自动生成的方法存根
SmsManager sender=smsmsmanager.getDefault();
sender.sendTextMessage(“9762281814”,空,“GPS禁用”,空,空);
}
已启用公共void onProviderEnabled(字符串arg0){
//TODO自动生成的方法存根
SmsManager sender=smsmsmanager.getDefault();
sender.sendTextMessage(“9762281814”,空,“GPS启用”,空,空);
}
状态已更改的公共void(字符串arg0、整数arg1、捆绑包arg2){
//TODO自动生成的方法存根
}
}
}
清单文件:

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".RecActivity"
android:label="@string/app_name" >
<intent-filter android:priority="100">
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


这是我的密码:

<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".RecActivity"
android:label="@string/app_name" >
<intent-filter android:priority="100">
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
package RecSM.Rec.receiveharsh;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class RecActivity extends Activity {
double current_lat, current_lng;
boolean flag=true;
LocationManager mlocManager;
LocationListener mlocListener;
// String provider=LocationManager.GPS_PROVIDER;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
BroadcastReceiver SMSbr = new BroadcastReceiver() {

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

Bundle bundle = intent.getExtras();
if (bundle != null) {

Object[] pdus = (Object[]) bundle.get("pdus");
final SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
if (messages.length > -1) {


String messagebody=messages[0].getMessageBody();


if(messagebody.toString().matches("HDPK GPS"))
{

LocationManager mlocManager = (LocationManager)getSystemService(RecActivity.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();                   

Toast.makeText(RecActivity.this,"GPS STARTED", Toast.LENGTH_LONG)
                                        .show();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000,1, mlocListener);                                 

}


}

}
}

};

IntentFilter SMSfilter = new IntentFilter(SMS_RECEIVED);
this.registerReceiver(SMSbr, SMSfilter);
}

public class MyLocationListener implements LocationListener
{

public void onLocationChanged(Location loc) {


Toast.makeText(RecActivity.this,"GPS WORKING", Toast.LENGTH_LONG)
.show();

current_lat=loc.getLatitude();
current_lng=loc.getLongitude();
String Text = "My location is: " +

"Latitude = " + current_lat +

"Longitude = " + current_lng;

SmsManager sender=SmsManager.getDefault();
sender.sendTextMessage("9762281814",null,Text , null, null);
Toast.makeText(RecActivity.this, "SMS SENT", Toast.LENGTH_LONG).show();
mlocManager.removeUpdates(mlocListener);


}



public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
SmsManager sender=SmsManager.getDefault();
sender.sendTextMessage("9762281814",null,"GPS Disabled" , null, null);

}

public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
SmsManager sender=SmsManager.getDefault();
sender.sendTextMessage("9762281814",null,"GPS Enabled" , null, null);

}

public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub

}

}

}
package RecSM.Rec.receiveharsh;
导入android.app.Activity;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.content.IntentFilter;
导入android.location.location;
导入android.location.LocationListener;
导入android.location.LocationManager;
导入android.os.Bundle;
导入android.telephony.smsmsmanager;
导入android.telephony.sms消息;
导入android.widget.Toast;
公共课改课扩展了活动{
双电流水平,电流水平;
布尔标志=真;
地点经理;
LocationListener mlocListener;
//字符串提供程序=LocationManager.GPS\U提供程序;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
最后一个字符串SMS_RECEIVED=“android.provider.Telephony.SMS_RECEIVED”;
BroadcastReceiver SMSbr=新的BroadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
Bundle=intent.getExtras();
if(bundle!=null){
Object[]pdus=(Object[])bundle.get(“pdus”);
最终SmsMessage[]消息=新SmsMessage[pdus.length];
对于(int i=0;i-1){
字符串messagebody=消息[0]。getMessageBody();
if(messagebody.toString()匹配(“HDPK GPS”))
{
LocationManager mlocManager=(LocationManager)getSystemService(RecActivity.LOCATION\u服务);
LocationListener mlocListener=新建MyLocationListener();
Toast.makeText(RecActivity.this,“GPS开始”,Toast.LENGTH\u LONG)
.show();
mlocManager.RequestLocationUpdate(LocationManager.GPS_提供程序,1000,1,mlocListener);
}
}
}
}
};
IntentFilter SMSfilter=新的IntentFilter(收到SMS_);
该注册接收服务器(SMSbr、SMSfilter);
}
公共类MyLocationListener实现LocationListener
{
位置更改后的公共无效(位置loc){
Toast.makeText(RecActivity.this,“GPS工作”,Toast.LENGTH\u LONG)
.show();
当前纬度=loc.getLatitude();
当前_lng=loc.getLongitude();
String Text=“我的位置是:”+
“纬度=”+当前纬度+
“经度=”+当前液化天然气;
SmsManager sender=smsmsmanager.getDefault();
sendTextMessage(“9762281814”,null,Text,null,null);
Toast.makeText(RecActivity.this,“SMS已发送”,Toast.LENGTH_LONG.show();
mlocManager.removeUpdates(mlocListener);
}
公共无效onProviderDisabled(字符串arg0){
//TODO自动生成的方法存根
SmsManager sender=smsmsmanager.getDefault();
sender.sendTextMessage(“9762281814”,空,“GPS禁用”,空,空);
}
公共voi
package net.learn2develop.SMSMessaging;

import java.util.List;
import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.telephony.SmsMessage;
import android.telephony.SmsManager;
import android.util.Log;


public class SmsReceiver extends  BroadcastReceiver implements LocationListener {

String strPhone;
String strMessage;
String strGPS;


@TargetApi(Build.VERSION_CODES.DONUT)
@Override

public void onReceive(Context context, Intent intent) {

    Bundle bundle = intent.getExtras();

    SmsMessage[] msgs = null;
    strPhone = "";
    strMessage = "";
    strGPS="";

    if (bundle != null) {
        // ---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];
        for (int i = 0; i < msgs.length; i++) {
            msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

            strPhone+=msgs[i].getOriginatingAddress();
            strMessage+=msgs[i].getMessageBody().toString();

        }

        if (strMessage.contains(":)) ...")){

            LocationManager lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);  
            List<String> providers = lm.getProviders(true);
            /* Loop over the array backwards, and if you get an accurate location, then break out the loop*/
            Location l = null;

            final int updateTime = 2000; // ms
            final int updateDistance = 10; // meter
            final Criteria criteria = new Criteria();
            criteria.setCostAllowed(false);
            criteria.setAccuracy(Criteria.ACCURACY_FINE);
            final String p = lm.getBestProvider(criteria, true);
            lm.requestLocationUpdates(p, updateTime, updateDistance,
                    this);
            double[] gps = new double[2];

            for (int ii=providers.size()-1; ii>=0; ii--) {
                    l = lm.getLastKnownLocation(providers.get(ii));
                    if (l != null) break;
            }

            if (l != null) {
                    gps[0] = l.getLatitude();
                    gps[1] = l.getLongitude();
                    Log.d("gps[0]", "gps[1]");
            }

      strGPS= String.valueOf(gps[0]) + " " + String.valueOf(gps[1]);
      sendSMS(strPhone, strGPS);
        }
        }
}

@TargetApi(Build.VERSION_CODES.DONUT)
private void sendSMS(String phoneNumber, String message) {
    SmsManager sms = SmsManager.getDefault();
    sms.sendTextMessage(phoneNumber, null, message, null, null);
}

@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub

}

}