How to active in a class to another class passing with String value?

I want to pass a String variable to another class and run a method again using that String.

However I can not pass the value and run the method again.

My application was getting the latitude and longitude from a message and I want to pass that latitude and longitude to my map class and show that location in my map view. Here is my code.

 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
     }

    }

This is the code of my other class:

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));
    }
}

I am receiving the following errors:

error: no suitable constructor found for

Intent(SMSRecevir,Class) constructor Intent.Intent(String,Uri) is not applicable

(argument mismatch; SMSRecevir cannot be converted to String)

constructor Intent.Intent(Context,Class) is not applicable

(argument mismatch; SMSRecevir cannot be converted to Context)

error: method startActivity in class ContextCompat cannot be applied to given types;

required: Context,Intent,Bundle

found: Intent

reason: actual and formal argument lists differ in length

Please help me..

1 answer

  • answered 2018-05-16 05:42 Jyoti JK

    You need to use context

    try

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

    And in your MapActivity,

    Replace it

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

    As

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