我刚开始我的第一个主要项目 - 一个简单的防盗应用程序,当我的手机被文本编辑时,如果它包含某个字符串,它会将短信发送回原始地址,告诉它手机的位置和地址。然而,我设法将短信发回原始地址,说出我的经度,纬度和准确度,以及显示位置的谷歌地图的链接。但是,我尝试将应用程序的地址发送回原始地址,但是一旦我尝试添加此功能,我的Logcat就开始了,显示所有这些GC_CONCURRENT FREED和GC_CONCURRENT BLOCKED - 显示大约10个第二。这使我的手机锁定,任务管理员不得不强制停止应用程序。所以问题是,问题到底是什么,我已经查了一下,我认为这与地理编码器循环有关吗?
这是我的MainActivity,没有太多进展,只是一个按钮向自己发送带有特定字符串的消息 - 比继续发送消息更容易。
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);
}
}
现在这是我的SMSReceiver类 - 魔术发生的地方。好吧,所有的错误来自哪里。这个班级接收短信,检查字符串,然后找到我的电话。在添加地理编码器之前,这一切都很顺利。
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
}
}
我的清单 - 不要认为它与它有任何关系,但只是因为你需要它。
<?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>
最后 - 我的logcat错误。
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
答案 0 :(得分:1)
for (int i1=0; i1<addresses.get(0).getMaxAddressLineIndex(); i++)
你有一个永远不会结束的循环。 “i1”变量用作循环变量,但是您要更新“i”变量。