我剛剛開始了我的第一個主要項目 - 一個簡單的防盜應用程序,當我的手機被文本編輯時,如果它包含某個字符串,它會發送一個短信回到原始地址,告訴它手機的位置和地址。不過,我設法將短信發回原始地址,並說明了我的經度,緯度和準確性,這也是顯示位置的谷歌地圖的鏈接。然而,我嘗試應用將手機地址發送回原始地址的能力,但只要我嘗試添加該地址,我的Logcat就心存疑慮,顯示所有這些GC_CONCURRENT FREED和GC_CONCURRENT BLOCKED - 顯示約10第二。這使我的手機鎖定,任務管理器不得不強制停止應用程序。所以問題是,究竟是什麼問題,我查了它,我認爲這是與地理編碼器循環?Geocoder - GC_CONCURRENT FREED
這是我的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