Home > front end >  Is it posible to change Android's call ring in an app? If yes, how to achieve it?
Is it posible to change Android's call ring in an app? If yes, how to achieve it?

Time:12-11

I want to make an app that can set up several songs and play them randomly or repeatly when I got a call. Like I sat up song A and song B. When I got a call, the phone rang song A. Then I got another call, the phone rang song B. Is it posible? How to achieve this goal?

CodePudding user response:

You can't set the sound from an app directly as a global ringtone. But you can let the user select the ringtone from it's storage on his device. The code shows you how does it work in practice.

Let's start with your MainActivity.java that execute the RingtoneManager get pressed by a Button:

public class MainActivity extends AppCompatActivity {
   Button btn;
   TextView txtView;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      btn = findViewById(R.id.btnSelRingtone);
      txtView = findViewById(R.id.tvRingtone);
      btn.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View view) {
            //Intent to select Ringtone.
            final Uri currentTone=
               RingtoneManager.getActualDefaultRingtoneUri(MainActivity.this,
               RingtoneManager.TYPE_ALARM);
            Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentTone);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
            startActivityForResult(intent, 999);
         }
      });
   }
   @SuppressLint("SetTextI18n")
   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if(requestCode == 999 && resultCode == RESULT_OK){
         Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
         txtView.setText("From :"   uri.getPath());
      }
   }
}

Add the following code to activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".MainActivity">
   <Button
      android:id="@ id/btnSelRingtone"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:text="Select Ringtone"/>
   <TextView
      android:id="@ id/tvRingtone"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:textStyle="bold|italic"
      android:layout_below="@id/btnSelRingtone"
      android:text="Ringtone Path"/>
</RelativeLayout>

Don't forget to add <user-permission/> to your AndroidManifest.xml:

   <uses-permission
      android:name="android.permission.WRITE_SETTINGS"
      tools:ignore="ProtectedPermissions" />

RESULT:

Select ringtone

PS: I think that's a point where you can start with. Now you know all technologies to continue with your project. Cheers!

  • Related