0

我刚刚开始了我的第一个主要项目 - 一个简单的防盗应用程序,当我的手机被发短信时,如果它包含某个字符串,它会向原始地址发送一条短信,告诉它位置和电话的地址。但是,我设法向原始地址发送了一条短信,说明了我的经度、纬度和准确度,还有一个指向显示位置的谷歌地图的链接。但是,我尝试应用将手机地址发送回原始地址的功能,但是当我尝试添加此功能时,我的 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
4

1 回答 1

1
for (int i1=0; i1<addresses.get(0).getMaxAddressLineIndex(); i++)

你有一个永远不会结束的循环。“i1”变量用作循环变量,但您正在更新“i”变量。

于 2013-05-13T16:50:42.203 回答