Question: Can you update my code? It says getdefault is depreciated in API 3 4 : package com.example.dariusquickeventtrackingapp; import android.Manifest; import android.content.pm . PackageManager; import android.os

Can you update my code? It says getdefault is depreciated in API 34:
package com.example.dariusquickeventtrackingapp;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
public class EventActivity extends AppCompatActivity {
private static final int SMS_PERMISSION_CODE =100;
private EditText titleEditText, dateEditText, descriptionEditText;
private Button addButton;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_event);
titleEditText = findViewById(R.id.edit_event_title);
dateEditText = findViewById(R.id.edit_event_date);
descriptionEditText = findViewById(R.id.edit_event_description);
addButton = findViewById(R.id.button_add_event);
addButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
addEvent();
}
});
}
private void addEvent(){
String title = titleEditText.getText().toString();
String date = dateEditText.getText().toString();
String description = descriptionEditText.getText().toString();
// Add event to database or perform other necessary actions
// Check for SMS permission before sending notification
if (ContextCompat.checkSelfPermission(this, Manifest.permission.SEND_SMS)
== PackageManager.PERMISSION_GRANTED){
sendSmsNotification(title, date);
} else {
requestSmsPermission();
}
// Clear input fields
titleEditText.setText("");
dateEditText.setText("");
descriptionEditText.setText("");
Toast.makeText(this, "Event added successfully", Toast.LENGTH_SHORT).show();
}
private void requestSmsPermission(){
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS)){
// Explain to the user why we need the permission
Toast.makeText(this,"SMS permission is required to send event notifications", Toast.LENGTH_LONG).show();
}
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, SMS_PERMISSION_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults){
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == SMS_PERMISSION_CODE){
if (grantResults.length >0 && grantResults[0]== PackageManager.PERMISSION_GRANTED){
// Permission granted, send SMS
sendSmsNotification(titleEditText.getText().toString(), dateEditText.getText().toString());
} else {
// Permission denied, show a message to the user
Toast.makeText(this,"SMS notifications will not be sent", Toast.LENGTH_SHORT).show();
}
}
}
private void sendSmsNotification(String title, String date){
// In a real app, you would get the user's phone number from their profile or settings
String phoneNumber ="1234567890"; // Replace with actual phone number retrieval logic
String message = "New event added: "+ title +" on "+ date;
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);
Toast.makeText(this,"SMS notification sent", Toast.LENGTH_SHORT).show();
} catch (Exception e){
Toast.makeText(this, "Failed to send SMS notification", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!