Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड पर उपलब्ध नोटिफिकेशन साउंड की लिस्ट कैसे दिखाएं?

सिस्टम रिंगटोन पिकर से नोटिफिकेशन साउंड चुनना

यह आर्टिकल पिछले (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) से डिफॉल्ट सेट करें। बाद में बदल नहीं सकते — नया चैनल बनाना होगा।

सेटिंग्स में यूजर कंट्रोल

यूजर सेटिंग्स → ऐप्स → आपका ऐप → नोटिफिकेशन → चैनल → "साउंड" से बदल सकता है। ऐप का सेट किया साउंड केवल डिफॉल्ट है।

  1. Android में नोटिफिकेशन से सर्विस कैसे शुरू करें? Android में नोटिफिकेशन से सर्विस कैसे शुरू करें?

    Android में नोटिफिकेशन से सर्विस शुरू करना यह उदाहरण दर्शाता है कि Android में नोटिफिकेशन से सर्विस कैसे शुरू की जाती है। चरण 1 - नया प्रोजेक्ट बनाएं Android Studio में File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें। चरण 2 - res/layout/activity_main.xml में कोड जोड़

  1. एंड्रॉइड में SearchView का उपयोग कैसे करें? एंड्रॉइड में SearchView का उपयोग कैसे करें?

    एंड्रॉइड में SearchView के साथ लिस्ट फिल्टरिंग यह ट्यूटोरियल SearchView को ListView और ArrayAdapter के साथ इंटीग्रेट करके रियल-टाइम फिल्टरिंग लागू करने का तरीका दिखाता है। लेआउट (activity_main.xml) <RelativeLayout xmlns:android=https://schemas.android.com/apk/res/android android:layout_width

  1. Android में इंस्टॉल ऐप्स की सूची कैसे प्राप्त करें? Android में इंस्टॉल ऐप्स की सूची कैसे प्राप्त करें?

    इंस्टॉल ऐप्स की सूची प्राप्त करना यह उदाहरण दर्शाता है कि PackageManager का उपयोग करके डिवाइस पर इंस्टॉल सभी थर्ड-पार्टी ऐप्स की सूची कैसे प्राप्त करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट में ListView (activity_main.xml) <?xml version=1.0 encodi