如何在一个类中激活另一个传递String值的类?

时间:2018-05-16 05:37:14

标签: android android-intent

我想将String变量传递给另一个类并使用该String再次运行一个方法。

但是我无法传递该值并再次运行该方法。

我的应用程序从消息中获取纬度和经度,我想将该纬度和经度传递给我的地图类,并在地图视图中显示该位置。这是我的代码。

 public class SMSRecevir extends BroadcastReceiver
    {

    String phonenumb="********";

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

    if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
        {
            // Retrieves a map of extended data from the intent.
            final Bundle bundle = intent.getExtras();
            SmsMessage[] msgs = null;
            String str = "";

            try
            {
                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";
                    }
                    String replyPhone = msgs[0].getOriginatingAddress();
                    String request = msgs[0].getMessageBody().toString();

                    if(request.equals("lost")&& replyPhone.equals(phonenumb))
                    {


                        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);

                        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
                        ringtone.play();


                        for (int a=0;a<10;a++)
                            Toast.makeText(context, "!!!...Vechile Was Lost....!!!", Toast.LENGTH_SHORT).show();

                    }
                    else if (replyPhone.equals(phonenumb)){
                        Intent my =new Intent(SMSRecevir.this,MapsActivity.class);
                       my.putExtra("key",request);

                       startActivity(my);

                    }
                }
            }
            catch (Exception e)
            {
                Log.e("MyReceiver", "Exception smsReceiver" +e);

            }
        }//close if
     }

    }

这是我其他班级的代码:

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback{
String phonenumber="********";
public double a=36.36548,b=81.66542;
private MapView mapView;
public GoogleMap gmap;
double lati;
double longi;
double aaa,bbb;
int v,bb;
char n= 'N';
char ar2[] = new char[30];
char ar3[] = new char[30];
String mg= "a";

private static final String MAP_VIEW_BUNDLE_KEY = "###########";

Button b1;
Button b2;
Button b3;
Button b4;
Button b5;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setLogo(R.drawable.ic_action_name);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
    setContentView(R.layout.activity_main);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        String value3 = extras.getString("key");
       mg = value3;
    }

   TextView t ;
   t=(TextView) findViewById(R.id.textView);
   t.setText(mg);
    b1 = (Button) findViewById(R.id.find);
    b2 = (Button) findViewById(R.id.out);
    b3 = (Button) findViewById(R.id.in);
    b4 = (Button) findViewById(R.id.off);
    b5 = (Button) findViewById(R.id.call);
    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phonenumber, null, "F", null, null);


        }
    });
    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phonenumber, null, "O", null, null);


        }
    });
    b3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phonenumber, null, "I", null, null);


        }
    });
    b4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


        }
    });
    b5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phonenumber, null, "Call", null, null);


        }
    });

        Bundle mapViewBundle = null;
        if (savedInstanceState != null) {
            mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
        }

        mapView = findViewById(R.id.map);
        mapView.onCreate(mapViewBundle);
        mapView.getMapAsync(this);
    }
    @Override
    public void onSaveInstanceState (Bundle outState){
        super.onSaveInstanceState(outState);

        Bundle mapViewBundle = outState.getBundle(MAP_VIEW_BUNDLE_KEY);
        if (mapViewBundle == null) {
            mapViewBundle = new Bundle();
            outState.putBundle(MAP_VIEW_BUNDLE_KEY, mapViewBundle);
        }

        mapView.onSaveInstanceState(mapViewBundle);
    }
    @Override
    protected void onResume () {
        super.onResume();
        mapView.onResume();
    }

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

    @Override
    protected void onStop () {
        super.onStop();
        mapView.onStop();
    }
    @Override
    protected void onPause () {
        mapView.onPause();
        super.onPause();
    }
    @Override
    protected void onDestroy () {
        mapView.onDestroy();
        super.onDestroy();
    }
    @Override
    public void onLowMemory () {
        super.onLowMemory();
        mapView.onLowMemory();
    }

    @Override
    public void onMapReady (GoogleMap googleMap){
        gmap = googleMap;
        gmap.setMinZoomPreference(12);
        gmap.setIndoorEnabled(true);
        UiSettings uiSettings = gmap.getUiSettings();
        uiSettings.setIndoorLevelPickerEnabled(true);
        uiSettings.setMyLocationButtonEnabled(true);
        uiSettings.setMapToolbarEnabled(true);
        uiSettings.setCompassEnabled(true);
        uiSettings.setZoomControlsEnabled(true);

  if(mg != "a") {
     char[] ar = mg.toCharArray();
     int v;
     for (v = 1; v < 11; v++) {

    ar2[v] = ar[v];
  }
     for (v = 14; v < 25; v++) {
      bb = 0;
      ar3[v] = ar[v];
      bb++;
}
   String lat = new String(ar2);
   String lon = new String(ar3);
   lati = Double.valueOf(lat.trim()).doubleValue();
   longi = Double.valueOf(lon.trim()).doubleValue();
   lati = lati / 100;
   longi = longi / 100;
   a = lati;
   b = longi;

      LatLng ny = new LatLng(a,b);

      MarkerOptions markerOptions = new MarkerOptions();
      markerOptions.position(ny);
      gmap.addMarker(markerOptions);

      gmap.moveCamera(CameraUpdateFactory.newLatLng(ny));

}

        LatLng ny = new LatLng(a,b);

        MarkerOptions markerOptions = new MarkerOptions();
        markerOptions.position(ny);
        gmap.addMarker(markerOptions);

        gmap.moveCamera(CameraUpdateFactory.newLatLng(ny));
    }
}

我收到以下错误:

  

错误:

找不到合适的构造函数      

意图(SMSRecevir,班)   构造函数Intent.Intent(String,Uri)不适用

     

(参数不匹配; SMSRecevir无法转换为String)

     

构造函数Intent.Intent(Context,Class)不适用

     

(参数不匹配; SMSRecevir无法转换为上下文)

错误:类ContextCompat中的方法startActivity不能应用于给定类型;

  

必需:Context,Intent,Bundle

     

发现:意图

     

原因:实际和正式的参数列表长度不同

请帮帮我..

1 个答案:

答案 0 :(得分:0)

您需要使用 context

Intent my =new Intent(context,MapsActivity.class);
 my.putExtra("key",request);
 context.startActivity(my);

在MapActivity中,

替换它

Bundle extras = getIntent().getExtras();
if (extras != null) {
    String value3 = extras.getString("key");
   mg = value3;
}

作为

String value3 =  getIntent().getStringExtra("key");