我正在制作一个在手机后台运行的应用程序。当我使用 HDPK GPS 发送短信时,应用程序应该以固定数字发送小区的 GPS 坐标。我收到了号码上的 GPS 坐标,但持续不断。我已经用 removeupdates 尝试了一切,但都是徒劳的!请帮我解决这个问题。此外,当应用程序在后台侦听 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>
这是我的代码:
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
}
}
}