andorid sms yollama
19.05.2018 - 01:44
arkadaslar asagıkı kodlar ile sms yollaya mıyorum
durumText.setText dekı bılgılerı sendSMSMessage() içine çekemiyorum yardımcı olurmusunuz
public class konum extends FragmentActivity implements OnMapReadyCallback {
private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 0;
private GoogleMap mMap;
LocationManager locationManager;
LocationListener locationListener;
Button smsButton;
TextView durumText;
String telefonNo;
String message;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_konum);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
smsButton = findViewById(R.id.gonderButton);
durumText= findViewById(R.id.durum_text);
smsButton.setOnClickListener(new View.OnClickListener() {
// sms gonderme alanı
@Override
public void onClick(View v) {
sendSMSMessage();
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// final TextView durum= findViewById(R.id.durum_text);
// bir oncekı sayfa da bulunan lıstvıew bılısını cekme
Intent intent = getIntent();
final String name = intent.getStringExtra("name");
// ad soyad bılgısı alma
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
final String isim = preferences.getString("isim", "N/A");
locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
locationListener = new LocationListener() {
@SuppressLint("SetTextI18n")
@Override
public void onLocationChanged(Location location) {
mMap.clear();
LatLng userLocation = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(userLocation).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLocation,15));
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
try {
List<Address> adressList = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
if (adressList != null && adressList.size() > 0) {
System.out.println("address info" + adressList.get(0).toString());
durumText.setText(name +", Adres: "+ adressList.get(0).getAddressLine(0)+" Konum: "+ adressList.get(0).getLatitude() +" , " + adressList.get(0).getLongitude()+" ,"+ isim.toUpperCase());
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override public void onStatusChanged(String provider, int status, Bundle extras) { }
@Override public void onProviderEnabled(String provider) { }
@Override public void onProviderDisabled(String provider) { }
};
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION},1);
} else {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LatLng userLastLocation = new LatLng(lastLocation.getLatitude(), lastLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(userLastLocation).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLastLocation, 15));
}
}
protected void sendSMSMessage() {
telefonNo = "05555125556";
message = durumText.getText().toString(); // buraya yukarıdakı durumText.setText içeriğini alamıyorum.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.SEND_SMS)) ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (grantResults.length > 0) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,locationListener);
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_SEND_SMS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(telefonNo, null, message, null, null);
Toast.makeText(getApplicationContext(), "SMS sent.",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again.", Toast.LENGTH_LONG).show();
}
}
}
}
}
6
Görüntülenme
0 Beğeni