सिस्टम रिंगटोन पिकर से नोटिफिकेशन साउंड चुनना
यह आर्टिकल पिछले (ID 1005061590) के समान है — RingtoneManager.ACTION_RINGTONE_PICKER से सिस्टम के उपलब्ध नोटिफिकेशन साउंड की लिस्ट दिखाना।
चरण 1-2: प्रोजेक्ट और लेआउट
दो बटन वाला लेआउट: "Set Ringtone" और "Create Notification"।
चरण 3: MainActivity — वही कोड
package app.tutorialspoint.com.notifyme;
import android.app.Activity;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public static final String NOTIFICATION_CHANNEL_ID = "10001";
private final static String default_notification_channel_id = "default";
String chosenRingtone;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onNewIntent(getIntent());
}
public void setRingtone(View view) {
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Tone");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
this.startActivityForResult(intent, 5);
}
public void createNotification(View view) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this, default_notification_channel_id);
mBuilder.setContentTitle("Notify Me");
mBuilder.setContentText("Something important!");
mBuilder.setSmallIcon(R.drawable.ic_launcher_foreground);
mBuilder.setAutoCancel(true);
if (chosenRingtone != null) {
mBuilder.setSound(Uri.parse(chosenRingtone));
}
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
if (chosenRingtone != null) {
notificationChannel.setSound(Uri.parse(chosenRingtone), null);
}
mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
assert mNotificationManager != null;
mNotificationManager.createNotificationChannel(notificationChannel);
}
assert mNotificationManager != null;
mNotificationManager.notify((int) System.currentTimeMillis(), mBuilder.build());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == 5) {
Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
if (uri != null) {
this.chosenRingtone = uri.toString();
} else {
this.chosenRingtone = null;
}
}
}
}
चरण 4: मैनिफेस्ट
मानक।
RingtoneManager के EXTRA_RINGTONE_TYPE विकल्प
| कॉन्स्टेंट | विवरण |
|---|---|
TYPE_RINGTONE | कॉल रिंगटोन |
TYPE_NOTIFICATION | नोटिफिकेशन साउंड (छोटे) |
TYPE_ALARM | अलार्म टोन |
TYPE_ALL | सभी प्रकार |
प्रोग्रामेटिकली लिस्ट प्राप्त करना (पिकर के बिना)
अगर खुद UI बनाना हो:
RingtoneManager manager = new RingtoneManager(this);
manager.setType(RingtoneManager.TYPE_NOTIFICATION);
Cursor cursor = manager.getCursor();
while (cursor.moveToNext()) {
String title = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
Uri uri = manager.getRingtoneUri(cursor.getPosition());
// title और uri से अपनी लिस्ट बनाएं
}
cursor.close();
नोटिफिकेशन चैनल पर डिफॉल्ट साउंड
चैनल बनाते समय setSound(uri, audioAttributes) से डिफॉल्ट सेट करें। बाद में बदल नहीं सकते — नया चैनल बनाना होगा।
सेटिंग्स में यूजर कंट्रोल
यूजर सेटिंग्स → ऐप्स → आपका ऐप → नोटिफिकेशन → चैनल → "साउंड" से बदल सकता है। ऐप का सेट किया साउंड केवल डिफॉल्ट है।