Reputation: 21
I have just started my first main project - a simple anti-theft application, that when my the phone is been text-ed, if it contains a certain string, it will send a sms back to the originating address, telling it the location and address of the phone. However, I managed to send a sms back to the originating address, saying my longitude, latitude and accuracy, also a link to google maps showing the location. However, I tried to app the ability to send the address of the phone back to the originating address, but as soon as I tried to add this, my Logcat went mental, showing all of these GC_CONCURRENT FREED and GC_CONCURRENT BLOCKED - showing about 10 per second. This made my phone lock up and the task manager having to force stop the app. So the question is, what the hell is the problem, I have looked it up and I think it is something to do with the geocoder loop?
This is my MainActivity, not much going on, just a button sending myself a message with the certain string in - more easier than going on messages and doing it.
package com.SMS;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
Button btnSendSMS;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_Black_NoTitleBar);
setContentView(R.layout.activity_main);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendSMS("07923368279", "hi");
}
});
}
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
Now this is my SMSReceiver class - where the magic happens. Well, where all the errors come from. This class, receives the sms's, check for the string, then locates my phone. Which was all going well until I added the geocoder.
package com.SMS;
import java.io.IOException;
import java.util.List;
import java.util.Locale;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
public class SMSReceiver extends BroadcastReceiver {
IntentFilter intentFilter;
LocationManager locationManager;
LocationListener locationListener;
@Override
public void onReceive(Context context, Intent intent)
{
//--get the sms message passed in--
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
String add = "";
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]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " : ";
str += msgs[i ].getMessageBody().toString();
str += "\n";
if(str.contains("hi"))
{
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Location loc = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double Longitude = (double) loc.getLongitude();
double Latitude = (double) loc.getLatitude();
int Accuracy = (int) loc.getAccuracy();
Geocoder geoCoder = new Geocoder(context, Locale.getDefault());
try
{
List <Address> addresses = geoCoder.getFromLocation(Latitude, Longitude, 1);
if (addresses.size() > 0)
{
for (int i1=0; i1<addresses.get(0).getMaxAddressLineIndex(); i++)
add = addresses.get(0).getAddressLine(i1) + "\n";
}
}
catch (IOException e)
{
e.printStackTrace();
}
//--display the new sms message
sendSMS(msgs[i].getOriginatingAddress(), "Location Found! \nLatitude : " + Latitude + "\nLongitude : " + Longitude + "\nAccuracy : " + Accuracy + "m" + add + "\nhttp://maps.google.com/maps?q=loc:" + Latitude + "," + Longitude);
}
}
}
}
private void sendSMS(String phoneNumber, String message) {
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, null, null);
}
}
class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc) {
loc.getLatitude();
loc.getLongitude();
loc.getAccuracy();
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
}
My Manifest - don't think it's anything to do with it, but just incase you need it.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.SMS"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.SMS.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SMSReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COURSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.LOCATION" />
</manifest>
And finally - my logcat errors.
04-02 20:42:37.655: D/dalvikvm(19046): GC_CONCURRENT freed 511K, 9% free 8771K/9580K, paused 1ms+2ms, total 16ms
04-02 20:42:37.655: D/dalvikvm(19046): WAIT_FOR_CONCURRENT_GC blocked 11ms
04-02 20:42:37.695: D/dalvikvm(19046): GC_CONCURRENT freed 511K, 9% free 8772K/9580K, paused 2ms+3ms, total 24ms
04-02 20:42:37.695: D/dalvikvm(19046): WAIT_FOR_CONCURRENT_GC blocked 18ms
04-02 20:42:37.745: D/dalvikvm(19046): GC_CONCURRENT freed 512K, 9% free 8771K/9580K, paused 2ms+4ms, total 34ms
04-02 20:42:37.745: D/dalvikvm(19046): WAIT_FOR_CONCURRENT_GC blocked 27ms
04-02 20:42:37.755: D/overlay(158): Unset pipe=VG0 dpy=0; Unset pipe=VG1 dpy=0; Unset pipe=RGB1 dpy=0;
04-02 20:42:37.795: D/dalvikvm(19046): GC_CONCURRENT freed 511K, 9% free 8771K/9580K, paused 3ms+4ms, total 34ms
04-02 20:42:37.795: D/dalvikvm(19046): WAIT_FOR_CONCURRENT_GC blocked 27ms
04-02 20:42:37.845: D/dalvikvm(19046): GC_CONCURRENT freed 512K, 9% free 8771K/9580K, paused 3ms+3ms, total 34ms
04-02 20:42:37.845: D/dalvikvm(19046): WAIT_FOR_CONCURRENT_GC blocked 27ms
04-02 20:42:37.905: D/dalvikvm(19046): GC_CONCURRENT freed 512K, 9% free 8771K/9580K, paused 3ms+2ms, total 31ms
04-02 20:42:37.905: D/dalvikvm(19046): WAIT_FOR_CONCURRENT_GC blocked 20ms
04-02 20:42:37.945: D/dalvikvm(19046): GC_CONCURRENT freed 511K, 9% free 8771K/9580K, paused 2ms+2ms, total 19ms
04-02 20:42:37.945: D/dalvikvm(19046): WAIT_FOR_CONCURRENT_GC blocked 6ms
04-02 20:42:38.005: D/dalvikvm(19046): GC_CONCURRENT freed 512K, 9% free 8771K/9580K, paused 4ms+1ms, total 34ms
Upvotes: 0
Views: 299
Reputation: 10823
for (int i1=0; i1<addresses.get(0).getMaxAddressLineIndex(); i++)
You have a loop that'll never end. The "i1" variable is used as the loop variable, but you're updating the "i" variable.
Upvotes: 1