簡體   English   中英

廣播接收機中未調用的方法

[英]Method not called in broadcast receiver

我想更新地圖,並從短信中獲取坐標。 接收方被調用,但是void refreshMap沒有被調用。 這是代碼:

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.telephony.SmsMessage;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MainActivity extends FragmentActivity implements LocationListener {

    GoogleMap googleMap;
    private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        IntentFilter filter = new IntentFilter(Intent.ACTION_DEFAULT);  
        filter.addAction(SMS_RECEIVED);
        filter.setPriority(1000);
        this.registerReceiver(this.smsReceiver, filter);

        // Getting reference to the SupportMapFragment of activity_main.xml
        SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        // Getting GoogleMap object from the fragment
        googleMap = fm.getMap();

        // Enabling MyLocation Layer of Google Map
        googleMap.setMyLocationEnabled(true);               


         // Getting LocationManager object from System Service LOCATION_SERVICE
        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

        // Creating a criteria object to retrieve provider
        Criteria criteria = new Criteria();

        // Getting the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);

        // Getting Current Location
        Location location = locationManager.getLastKnownLocation(provider);

        if(location!=null){
                onLocationChanged(location);
        }

        locationManager.requestLocationUpdates(provider, 20000, 0, this);

    }


     private BroadcastReceiver smsReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) { 
                if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
                                {                                               
                                    refreshMap();  //with the parsed coordinates from sms
                                }                       
                            }
                        }catch(Exception e){
//                                  Log.d("Exception caught",e.getMessage());
                        }
                    }
                }

            }
          };


    @Override
    public void onLocationChanged(Location location) {

        TextView tvLocation = (TextView) findViewById(R.id.tv_location);

        // Getting latitude of the current location
        double latitude = location.getLatitude();

        // Getting longitude of the current location
        double longitude = location.getLongitude();     

        // Creating a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);

        // Showing the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        // Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));

        // Setting latitude and longitude in the TextView tv_location
        tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );      

    }




    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub      
    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub      
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub      
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void refreshMap() {
        // TODO Auto-generated method stub
        Log.e("MAP","IS refreshing");  //NEVER CAlled
    }
}

我嘗試了幾種方法,但是從未調用過refreshMap()。 任何幫助都會很棒。

編輯:這是LogCat

04-12 13:48:41.046: I/SELinux(17737): Function: selinux_android_load_priority [0], There is no sepolicy file.
04-12 13:48:41.046: I/SELinux(17737):  
04-12 13:48:41.066: I/SELinux(17737): Function: selinux_android_load_priority , spota verifySig and checkHash pass. priority version is VE=SEPF_SM-N9005_4.4.2_0040
04-12 13:48:41.066: I/SELinux(17737):  
04-12 13:48:41.066: I/SELinux(17737):  
04-12 13:48:41.066: I/SELinux(17737): selinux_android_seapp_context_reload: seapp_contexts file is loaded from /data/security/spota/seapp_contexts
04-12 13:48:41.066: E/dalvikvm(17737): >>>>> Normal User
04-12 13:48:41.066: E/dalvikvm(17737): >>>>> in.wptrafficanalyzer.locationingooglemapv2 [ userId:0 | appId:10623 ]
04-12 13:48:41.066: D/dalvikvm(17737): Late-enabling CheckJNI
04-12 13:48:41.296: W/ApplicationPackageManager(17737): getCSCPackageItemText()
04-12 13:48:41.296: I/PersonaManager(17737): getPersonaService() name persona_policy
04-12 13:48:41.356: E/MoreInfoHPW_ViewGroup(17737): Parent view is not a TextView
04-12 13:48:41.406: I/x(17737): Making Creator dynamically
04-12 13:48:41.446: I/Google Maps Android API(17737): Google Play services client version: 6587000
04-12 13:48:41.466: I/Google Maps Android API(17737): Google Play services package version: 7099038
04-12 13:48:42.076: D/dalvikvm(17737): GC_FOR_ALLOC freed 4205K, 24% free 20808K/27180K, paused 38ms, total 39ms
04-12 13:48:42.356: D/AbsListView(17737): Get MotionRecognitionManager
04-12 13:48:42.366: D/AbsListView(17737): onVisibilityChanged() is called, visibility : 8
04-12 13:48:42.366: D/AbsListView(17737): unregisterIRListener() is called 
04-12 13:48:42.376: D/AbsListView(17737): onVisibilityChanged() is called, visibility : 8
04-12 13:48:42.376: D/AbsListView(17737): unregisterIRListener() is called 
04-12 13:48:42.416: D/dalvikvm(17737): GC_FOR_ALLOC freed 1184K, 21% free 21645K/27180K, paused 26ms, total 26ms
04-12 13:48:42.456: W/ActivityThread(17737): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
04-12 13:48:42.526: I/System.out(17737): Thread-72732(HTTPLog):isShipBuild true
04-12 13:48:42.526: I/System.out(17737): Thread-72732(HTTPLog):SmartBonding Enabling is true, SHIP_BUILD is true, log to file is false, DBG is false
04-12 13:48:42.586: D/AbsListView(17737): onVisibilityChanged() is called, visibility : 0
04-12 13:48:42.586: D/AbsListView(17737): unregisterIRListener() is called 
04-12 13:48:42.616: E/Activity :(17737): resumed
04-12 13:48:42.626: I/dalvikvm(17737): Could not find method android.content.pm.PackageManager.getPackageInstaller, referenced from method com.google.android.gms.common.ff.a
04-12 13:48:42.626: W/dalvikvm(17737): VFY: unable to resolve virtual method 479: Landroid/content/pm/PackageManager;.getPackageInstaller ()Landroid/content/pm/PackageInstaller;
04-12 13:48:42.626: D/dalvikvm(17737): VFY: replacing opcode 0x6e at 0x000d
04-12 13:48:42.636: D/AbsListView(17737): onVisibilityChanged() is called, visibility : 4
04-12 13:48:42.636: D/AbsListView(17737): unregisterIRListener() is called 
04-12 13:48:42.646: D/AbsListView(17737): onVisibilityChanged() is called, visibility : 0
04-12 13:48:42.646: D/AbsListView(17737): unregisterIRListener() is called 
04-12 13:48:42.646: D/AbsListView(17737): unregisterIRListener() is called 
04-12 13:48:42.686: I/Adreno-EGL(17737): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build:  ()
04-12 13:48:42.686: I/Adreno-EGL(17737): OpenGL ES Shader Compiler Version: E031.24.00.08+13
04-12 13:48:42.686: I/Adreno-EGL(17737): Build Date: 03/28/14 Fri
04-12 13:48:42.686: I/Adreno-EGL(17737): Local Branch: 0328_AU200_patches
04-12 13:48:42.686: I/Adreno-EGL(17737): Remote Branch: 
04-12 13:48:42.686: I/Adreno-EGL(17737): Local Patches: 
04-12 13:48:42.686: I/Adreno-EGL(17737): Reconstruct Branch: 
04-12 13:48:42.786: D/OpenGLRenderer(17737): Enabling debug mode 0
04-12 13:48:42.796: D/AbsListView(17737): unregisterIRListener() is called 

我認為您需要在UI線程上調用refreshMap方法,如下所示:

     private BroadcastReceiver smsReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) { 
            if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
                            {    
                                MainActivity.this.runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {       
                                       refreshMap();  //with the parsed coordinates from sms
                                    }
                                });
                            }                       
                        }
                    }catch(Exception e){
                      Log.d("Exception caught",e.getMessage());
                    }
                }
            }

        }
      };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM