Doflamingo
Doflamingo

Reputation: 3

Can't Send message in Android Studio

I am developing woman safety app.I want to send the user location via message using SmsManager when the user shake their phone. I have two mobile phone operators in my country,

Orredo

and

Telenor

for sending message.When I use

Orredo,I can send message but when I use Telenor, I can't send message.I want to know what cause the problem is.Because of my code or my mobile phone operator??.

Here is my code. My code may be a little messy and make no sense because I am beginner and I developed this app from scratch.

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

        if (broadcastReceiver == null) {
            broadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {

                        tv1.append(""+intent.getExtras().get("Coordinate"));
                        Intent intent1 = new Intent(getApplicationContext(),GPS_Service.class);
                        stopService(intent1);
                        Log.d(TAG, "onReceive: "+tv1.getText());



                }
            };
        }
        registerReceiver(broadcastReceiver, new IntentFilter("location_update"));
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (broadcastReceiver != null) {
            unregisterReceiver(broadcastReceiver);
        }
    }


    private void enable_buttons() {
        mRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, Register.class);
                startActivity(intent);
            }
        });


    }

    private final SensorEventListener sensorListener = new SensorEventListener() {

        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            float x = sensorEvent.values[0];
            float y = sensorEvent.values[1];
            float z = sensorEvent.values[2];

            acelLast = acelVal;
            acelVal = (float) Math.sqrt((double) (x * x + y * y + z * z));
            float delta = acelVal - acelLast;
            shake = shake * 0.9f + delta;


            if (shake > 12) {
                mDatbaseHelper =new DatabaseHelper(MainActivity.this);
                Cursor data = mDatbaseHelper.getData();
                while (data.moveToNext()) {
                    phone1 = data.getString(1);
                    phone2 = data.getString(2);
                }

                @SuppressLint("MissingPermission")

                String s = "tel:" + phone1;
                Intent intent = new Intent(Intent.ACTION_CALL);
                intent.setData(Uri.parse(s));
                if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {

                    return;
                }
                startActivity(intent);
                ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}, SEND_SMS_PERMISSION_CODE);




                if (checkPermission(Manifest.permission.SEND_SMS)) {
                    SmsManager smsManager = SmsManager.getDefault();
                   smsManager.sendTextMessage(phone1, null,"google.com/maps/place/"+tv1.getText().toString(), null, null);
                   Log.d(TAG, "onSensorChange: "+tv1.getText().toString());
                    Toast.makeText(MainActivity.this, "Message Sent!", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
                }

            }
        }

And here is my code for GPS Service.

public class GPS_Service extends Service {
    private LocationListener listener;
    private LocationManager locationManager;
    public static final String TAG ="GPS_Service";


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onCreate() {
        super.onCreate();
        listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Intent i = new Intent("location_update");
                i.putExtra("Coordinate", location.getLatitude() + "," + location.getLongitude());
//                i.putExtra("Latitude", location.getLatitude());
//                i.putExtra("Longitude", location.getLongitude());
                sendBroadcast(i);

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

            @Override
            public void onProviderEnabled(String provider) {

            }

            @Override
            public void onProviderDisabled(String provider) {
                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(i);
            }


        };
        locationManager =(LocationManager)getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,3000,0,listener);
    }
    @Override
    public void onDestroy() {
        if(locationManager!=null)
        {
            locationManager.removeUpdates(listener);
        }
        super.onDestroy();
    }
}

Upvotes: 0

Views: 167

Answers (0)

Related Questions