Android intent 如何使用意图将数据从活动传递到广播接收器

Android intent 如何使用意图将数据从活动传递到广播接收器,android-intent,broadcastreceiver,locationmanager,locationlistener,smsmanager,Android Intent,Broadcastreceiver,Locationmanager,Locationlistener,Smsmanager,我刚刚开始了我的第一个主要项目——一个简单的防盗应用程序,当我的手机被发送文本信息时,如果它包含某个字符串,它会向原始地址发送短信,告诉它手机的位置。然而,我目前只想让手机接收短信,检查它是否包含字符串,获取位置,然后在手机上祝酒。我的问题是,由于我对android编程/Java(仅15岁)非常陌生,我在将我的位置从MainActivity传递到广播接收器时遇到了麻烦 这是我的主要活动-正如您所看到的,我的LocationListener所在的位置,我已经在onLocationChanged上注

我刚刚开始了我的第一个主要项目——一个简单的防盗应用程序,当我的手机被发送文本信息时,如果它包含某个字符串,它会向原始地址发送短信,告诉它手机的位置。然而,我目前只想让手机接收短信,检查它是否包含字符串,获取位置,然后在手机上祝酒。我的问题是,由于我对android编程/Java(仅15岁)非常陌生,我在将我的位置从MainActivity传递到广播接收器时遇到了麻烦

这是我的主要活动-正如您所看到的,我的LocationListener所在的位置,我已经在onLocationChanged上注释掉了Toast.makeText(),如果我取消注释,我会得到正确的坐标

package com.SMS;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;

public class MainActivity extends Activity 
{

    private LocationManager lm;
    private LocationListener locationListener;


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

        lm = (LocationManager)
            getSystemService(Context.LOCATION_SERVICE);

        locationListener = new MyLocationListener();

        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);    
    }

 public class MyLocationListener implements LocationListener
 {
    @Override
    public void onLocationChanged(Location loc) {
            if (loc != null) 
            {
                int Longitude = (int) (loc.getLongitude() * 1E6);
                int Latitude = (int) (loc.getLatitude() * 1E6);
                // Toast.makeText(getBaseContext(), "Location Changed : Lat: " + Latitude + " Longitude : " + Longitude, Toast.LENGTH_LONG).show();                 sendBroadcast(intent);

                Intent intent = new Intent(MainActivity.this, SMSReceiver.class); 
                intent.putExtra("Longitude", Longitude);
                intent.putExtra("Latitude", Latitude);
                sendBroadcast(intent);
            }
    }

    @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

    }
  }
}
这是我的SmsReceiver类,在这里检索sms以检查包含的字符串。我已经尝试了我在这类事情上看到的一切(我的历史上充满了这些东西),但无论我做什么,纬度和经度仍然返回300——我设置的默认值。我确信这与我的意图设置和接收意图有关,只是不知道是什么错了。所以,请通读一遍,并就您发现的任何问题向我提供反馈

package com.SMS;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) 
    {

        //--get the sms message passed in--
        Bundle bundle = intent.getExtras();
        SmsMessage[] msgs = null;
        String str = "";
        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"))
                {
                    int longitude = intent.getIntExtra("Longitude", 300);
                    int latitude = intent.getIntExtra("Latitude", 300);
                    //--display the new sms message
                    Toast.makeText(context, "Location Changed : Lat: " + latitude + " Longitude : " + longitude, Toast.LENGTH_LONG).show();
                }
            }
        }
    }
}
package com.SMS;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.sms消息;
导入android.widget.Toast;
公共类SMSReceiver扩展了BroadcastReceiver
{
@凌驾
公共void onReceive(上下文、意图)
{
//--获取传入的sms消息--
Bundle=intent.getExtras();
SmsMessage[]msgs=null;
字符串str=“”;
if(bundle!=null)
{
//---检索收到的sms消息
Object[]pdus=(Object[])bundle.get(“pdus”);
msgs=新SMS消息[PDU.length];

对于(inti=0;i代码中的一个基本错误在这里

Intent intent = new Intent(MainActivity.this, SMSReceiver.class); 
                intent.putExtra("Longitude", Longitude);
                intent.putExtra("Latitude", Latitude);
                sendBroadcast(intent);
您正在发送带有经度和纬度的意向,在接收器中,您正在尝试获取带有意向的sms数据。 如果同一个接收器响应接收sms,则在这种情况下,意图仅包含sms数据,而不包含经度和纬度数据。 您不能从不同来源接收来自同一目的的两个数据。 为两者使用不同的广播接收器或通过不同的方法连接。 我希望天气转晴

Intent intent = new Intent(MainActivity.this, SMSReceiver.class); 
                intent.putExtra("Longitude", Longitude);
                intent.putExtra("Latitude", Latitude);
                sendBroadcast(intent);