无法从SMSReceiver获取Intent

时间:2015-01-12 08:17:39

标签: android

我是Android的新手,我正在编写一个应用程序,它会发送一些带有短信的地理位置并接收它,并将其显示在地图上。 它在SMSReceiver中获取短信并将部分sms放入bundle中并调用MapActivity。 问题是MapActivity不显示任何地理位置。 我在SMSReceiver中获得了祝酒。所以我认为没有任何问题,MapActivity运行但我没有得到任何标记......有没有人知道问题出在哪里???

这是我的SMSReceiver:

public class SMSReceiver extends BroadcastReceiver {

private static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";;
private static final String GEOPINTS = "geoPoints";
Long user_id;

@Override
public void onReceive(Context context, Intent intent) {
    if (intent.getAction().equals(ACTION)) {
        Bundle bundle = intent.getExtras();
        android.telephony.SmsMessage[] msg = null;
        long sender = -1;
        String geoPoints = null;
        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            msg = new android.telephony.SmsMessage[pdus.length];
            int i;
            for (i = 0; i < msg.length; i++) {
                msg[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                if (msg[i].getMessageBody().startsWith(GEOPINTS)) {
                    Toast.makeText(context, "geoPoints received.", Toast.LENGTH_SHORT).show();
                    // abortBroadcast();
                    String number = msg[i].getOriginatingAddress();
                    number = number.substring(3);
                    sender = Long.parseLong(number);
                    geoPoints = msg[i].getMessageBody();
                    geoPoints = geoPoints.substring(9);
                    Intent mapActivityIntent = new Intent(context, MapActivity.class);
                    mapActivityIntent.putExtra("GEOPOINTS", geoPoints);
                    mapActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(mapActivityIntent);
                } 
            }
        }

    }
}}

这是我的MapActivity:

public class MapActivity extends FragmentActivity implements ConnectionCallbacks, OnConnectionFailedListener,
    LocationListener {

private static final long ONE_MIN = 1000 * 60;
private static final long TWO_MIN = ONE_MIN * 2;
private static final long FIVE_MIN = ONE_MIN * 5;
private static final long MEASURE_TIME = 1000 * 30;
private static final long POLLING_FREQ = 1000 * 10;
private static final long FASTES_UPDATE_FREQ = 1000 * 2;
private static final float MIN_ACCURACY = 25.0f;
private static final float MIN_LAST_READ_ACCURACY = 500.0f;

private Location mBestReading;
private Location mCurrentLocation;
LocationRequest mLocationRequest;
private LocationClient mLocationClient;

private final String TAG = "MapActivity";
private GoogleMap googleMap;
Long user_id;
String user_name;
String friend_name;
ImageView imageView;
private static String USER_ID = "USER_ID";
ArrayList<Person> friends = new ArrayList<Person>();
ArrayList<LatLng> points = new ArrayList<LatLng>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    Intent i = new Intent();
    Bundle bundle = i.getExtras();
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    user_id = Long.valueOf(prefs.getLong(USER_ID, 0L));
    imageView = (ImageView) findViewById(R.id.tinyImage);
    TextView userNameText = (TextView) findViewById(R.id.tinyUserName);
    DataBaseAdapter db = new DataBaseAdapter(this);
    User user = new User();
    user = db.getUserId(user_id);
    user_name = user.getName();
    userNameText.setText(user_name);
    byte[] image = null;
    Bitmap bitmap = null;
    image = user.getImage();
    if (image != null) {
        ByteArrayInputStream imageStream = new ByteArrayInputStream(image);
        bitmap = BitmapFactory.decodeStream(imageStream);
        imageView.setImageBitmap(bitmap);
    } else {
        Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image_default);
        imageView = (ImageView) findViewById(R.id.tinyImage);
        imageView.setImageBitmap(bm);
    }
    if (bundle != null && bundle.containsKey("GEOPOINTS")) {
        String geoPoints = bundle.getString("GEOPOINTS");
        if (geoPoints != null) {
            String latlng = "";
            Double lat = null;
            Double lng = null;
            LatLng point = null;
            do {
                latlng = geoPoints.substring(0, geoPoints.indexOf("\n"));
                lat = Double.parseDouble(latlng.substring(0, latlng.indexOf(" ")));
                lng = Double.parseDouble(latlng.substring(latlng.indexOf(" ") + 1));
                Toast.makeText(getApplicationContext(), "lat = " + lat + " lng = " + lng, Toast.LENGTH_SHORT)
                        .show();
                point = new LatLng(lat, lng);
                points.add(point);
                if (geoPoints.indexOf("\n") != geoPoints.indexOf(geoPoints.length())) {
                    geoPoints = geoPoints.substring(geoPoints.indexOf("\n") + 1);
                } else {
                    geoPoints = "";
                }
            } while (!geoPoints.equals(""));

        }
    }
    // Check if google play service , GPS & network is enabled
    if (!servicesAvailable())
        finish();

    // Create new Location Client. This class will handle call backs
    mLocationClient = new LocationClient(this, this, this);

    // Create and define the LocationRequest
    mLocationRequest = LocationRequest.create();

    // Use high accuracy
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    // Update every 10 seconds
    mLocationRequest.setInterval(POLLING_FREQ);

    // Receive updates no more often than every 2 seconds
    mLocationRequest.setFastestInterval(FASTES_UPDATE_FREQ);

}

private void addDirectionMarker() {
    if (points != null) {
        for (LatLng point : points) {
            googleMap.addMarker(new MarkerOptions().position(point).icon(
                    BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN)));
        }
    }

}

private void initilizeMap() {
    if (googleMap == null) {
        googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    }
    if (googleMap == null) {
        Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show();
    }
    googleMap.setMyLocationEnabled(true);
    googleMap.setOnInfoWindowClickListener(new OnInfoWindowClickListener() {

        @Override
        public void onInfoWindowClick(Marker mark) {
            if (!mark.getTitle().equals("You are here!") && !mark.getTitle().equals(null)) {
                friend_name = mark.getTitle();
                Intent i = new Intent(getApplicationContext(), ChoiceActivity.class);
                i.putExtra("FRIEND_NAME", friend_name);
                startActivity(i);
            }
        }
    });

}

@Override
protected void onStart() {
    super.onStart();

    // Connect to LocationServices
    mLocationClient.connect();
}

@Override
protected void onStop() {

    // Stop updates
    mLocationClient.removeLocationUpdates(this);

    // Disconnect from LocationServices
    mLocationClient.disconnect();

    super.onStop();
}

// Get the last known location from all providers
// return best reading that is as accurate as minAccuracy and
// was taken no longer then minAge milliseconds ago. If none,
// return null.

private Location bestLastKnownLocation(float minAccuracy, long maxAge) {

    Location bestResult = null;
    float bestAccuracy = Float.MAX_VALUE;
    long bestTime = Long.MIN_VALUE;

    // Get the best most recent location currently available
    mCurrentLocation = mLocationClient.getLastLocation();

    if (mCurrentLocation != null) {

        float accuracy = mCurrentLocation.getAccuracy();
        long time = mCurrentLocation.getTime();

        if (accuracy < bestAccuracy) {

            bestResult = mCurrentLocation;
            bestAccuracy = accuracy;
            bestTime = time;

        }
    }

    // Return best reading or null
    if (bestAccuracy > minAccuracy || (System.currentTimeMillis() - bestTime) > maxAge) {
        return null;
    } else {
        return bestResult;
    }
}

private boolean servicesAvailable() {

    // Check that Google Play Services are available
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    // If Google Play services is available

    return (ConnectionResult.SUCCESS == resultCode);

}

@Override
protected void onResume() {
    super.onResume();
    initilizeMap();
    addFriendsMarker();
    addDirectionMarker();
}

public void setUserLocation(Location location) {

    DataBaseAdapter db = new DataBaseAdapter(getApplicationContext());
    User user = new User();
    user.setId(user_id);
    com.bhproj.test.gpstrackertest.model.Location loc = new com.bhproj.test.gpstrackertest.model.Location(
            location.getLatitude(), location.getLongitude());
    user.setLocation(loc);
    db.updateLocUser(user);
    MarkerOptions marker = new MarkerOptions()
            .position(new LatLng(location.getLatitude(), location.getLongitude())).title("You are here!")
            .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_CYAN));
    googleMap.addMarker(marker);
    googleMap.animateCamera(
            CameraUpdateFactory.newCameraPosition(CameraPosition.builder()
                    .target(new LatLng(location.getLatitude(), location.getLongitude())).build()), 2000, null);

}

public void addFriendsMarker() {
    DataBaseAdapter db = new DataBaseAdapter(getApplicationContext());
    List<Person> persons = db.getFriends(user_id);
    if (persons != null) {
        for (Person friend : persons) {
            friends.add(friend);
        }
        if (friends != null) {
            for (Person i : friends) {
                googleMap.addMarker(new MarkerOptions()
                        .position(new LatLng(i.getLocation().getLatitude(), i.getLocation().getLongitude()))
                        .title(i.getName())
                        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE)));
            }
        }
    }
}

public void goToUserPageOnClickListener(View v) {
    Intent i = new Intent(getApplicationContext(), UserPageActivity.class);
    startActivity(i);
}

@Override
public void onLocationChanged(Location location) {
    // Determine whether new location is better than current best
    // estimate

    if (null == mBestReading || location.getAccuracy() < mBestReading.getAccuracy()) {

        // Update best estimate
        mBestReading = location;

        // Update display
        googleMap.clear();
        setUserLocation(location);
        addFriendsMarker();

        if (mBestReading.getAccuracy() < MIN_ACCURACY)
            mLocationClient.removeLocationUpdates(this);

    }

}

@Override
public void onConnectionFailed(ConnectionResult arg0) {
    Log.i(TAG, "Connection Failed. Try again later.");

}

@Override
public void onConnected(Bundle arg0) {
    // Get first reading. Get additional location updates if necessary

    if (servicesAvailable()) {
        // Get best last location measurement meeting criteria
        mBestReading = bestLastKnownLocation(MIN_LAST_READ_ACCURACY, FIVE_MIN);

        // Display last reading information
        if (null != mBestReading) {

            googleMap.clear();
            setUserLocation(mBestReading);
            addFriendsMarker();

            // } else {
            //
            // Toast.makeText(getApplicationContext(),
            // "No Initial Reading Available", Toast.LENGTH_LONG).show();

        }

        if (null == mBestReading || mBestReading.getAccuracy() > MIN_LAST_READ_ACCURACY
                || mBestReading.getTime() < System.currentTimeMillis() - TWO_MIN) {

            mLocationClient.requestLocationUpdates(mLocationRequest, this);

            // Schedule a runnable to unregister location listeners
            Executors.newScheduledThreadPool(1).schedule(new Runnable() {

                @Override
                public void run() {

                    mLocationClient.removeLocationUpdates(MapActivity.this);

                }
            }, MEASURE_TIME, TimeUnit.MILLISECONDS);
        }

    }
}

@Override
public void onDisconnected() {
    Log.i(TAG, "Disconnected. Try again later.");

}}

2 个答案:

答案 0 :(得分:0)

使用

Intent i = getIntent();

相反,如果Intent i = new Intent();startActivity

获取您在BroadcastReceiver传递的Intent

答案 1 :(得分:0)

在你的broadcasereceiver中做这样的事情:(使用那个意图)

public class SMSReceiver extends BroadcastReceiver {

public static final String NOTIFICATION = "receiver_sms";
@Override
public void onReceive(Context context, Intent intent) {

    Log.i("onReceive methode", "new SMS Comming");

    Bundle myBundle = intent.getExtras();

    SmsMessage[] messages = null;
    String strMessage = "", address = "";
    abortBroadcast();
    if (myBundle != null) {
        // get message in pdus format(protocol description unit)
        Object[] pdus = (Object[]) myBundle.get("pdus");
        // create an array of messages
        messages = new SmsMessage[pdus.length];
        Log.i("onReceive methode", "new SMS Comming");
        for (int i = 0; i < messages.length; i++) {
            // Create an SmsMessage from a raw PDU.
            messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            // get the originating address (sender) of this SMS message in
            // String form or null if unavailable
            address = messages[i].getOriginatingAddress();

            // get the message body as a String, if it exists and is text
            // based.
            strMessage += messages[i].getMessageBody();
            strMessage += "\n";
        }

        // show message in a Toast
    }
        // this is what you need
        Intent broadcast = new Intent(NOTIFICATION);
        broadcast.putExtra("data", strMessage);
        LocalBroadcastManager.getInstance(context).sendBroadcast(broadcast);

}

然后在你的活动中注册你的接收者

public BroadcastReceiver receiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e(tag, "SMS Received.");            
        Bundle b = intent.getBundleExtra("SMS");
        String str = intent.getStringExtra("data");
        parseSMSData(str);
    }
};

然后在onResume():

@Override
protected void onResume() {
    super.onResume();
    LocalBroadcastManager.getInstance(this).registerReceiver(receiver,
            new IntentFilter(SMSReceiver.NOTIFICATION));
}

并且在onDestroy()中你必须注销这个接收器:

@Override
protected void onDestroy() {
    // Unregister since the activity is about to be closed.
    LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
    super.onDestroy();
}

并且也不要忘记在应用程序标记中的清单文件中添加它:

 <receiver android:name=".SMSBroadcastReceiver">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
        </intent-filter>
    </receiver>