Androidで利用可能な通知音のリストを表示する方法を徹底解説
この記事では、Androidアプリで利用可能な通知音のリストを表示し、ユーザーにサウンドを選択させる方法を解説します。Android標準のRingtoneManagerを活用することで、端末に登録されている通知音を簡単に一覧表示できます。
実装の全体像
今回作成するサンプルアプリは、以下の2つの機能を持っています。
- 「set ringtone」ボタン:システムの通知音選択画面(ピッカー)を起動する
- 「Create notification」ボタン:選択した設定で通知を発行する
それでは、手順に沿って実装していきましょう。
ステップ1:新規プロジェクトを作成する
まず、Android Studioで新しいプロジェクトを作成します。メニューから File → New Project を選択し、必要な項目を入力してプロジェクトを作成してください。
ステップ2:レイアウトファイル(activity_main.xml)を編集する
次に、res/layout/activity_main.xml に以下のコードを追加します。ボタンを2つ配置したシンプルなレイアウトです。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:onClick="setRingtone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="set ringtone" />
<Button
android:onClick="createNotification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="16dp"
android:text="Create notification" />
</RelativeLayout>ステップ3:MainActivityに処理を実装する
src/MainActivity.java に以下のコードを記述します。ポイントは RingtoneManager.ACTION_RINGTONE_PICKER を使ってインテントを発行し、通知音タイプ(TYPE_NOTIFICATION)を指定してピッカーを起動する部分です。選択結果は onActivityResult() で受け取ります。
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 (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);
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;
}
}
}
}コードのポイント
- EXTRA_RINGTONE_TYPE:
TYPE_NOTIFICATIONを指定することで、通知音のみが候補として表示されます。着信音ならTYPE_RINGTONE、アラーム音ならTYPE_ALARMを指定します。 - EXTRA_RINGTONE_TITLE:ピッカー画面のタイトルを自由に設定できます。
- onActivityResult():ユーザーが選択した音のURIを
EXTRA_RINGTONE_PICKED_URIから取得し、変数に保存しています。 - 通知チャンネル:Android 8.0(API レベル26)以降では通知チャンネルの作成が必須のため、バージョン判定を行って対応しています。
ステップ4:AndroidManifest.xmlを編集する
最後に、AndroidManifest.xml に以下のコードを追加します。振動を使用するため VIBRATE パーミッションを宣言しておきます。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.tutorialspoint.com.notifyme">
<uses-permission android:name="android.permission.VIBRATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>アプリを実行して動作を確認する
それでは、実際にアプリを起動してみましょう。Android端末をPCに接続していることを前提に説明します。Android Studioでプロジェクト内のアクティビティファイルを開き、ツールバーの実行(Run)アイコンをクリックしてください。接続したモバイルデバイスを選択すると、端末にアプリがインストールされ、デフォルト画面が表示されます。
「set ringtone」ボタンをタップすると通知音の一覧が表示され、任意の音を選択できます。「Create notification」ボタンをタップすれば、実際に通知が発行されることも確認できます。
-
【Android開発】SearchViewの使い方を解説!ListViewの検索・絞り込み機能を実装する手順
この記事では、AndroidアプリでSearchViewを使用して、ListViewに表示された項目をリアルタイムに検索・絞り込む方法を解説します。SearchViewを活用すれば、少ないコード量で直感的な検索機能をアプリに追加できます。ステップ1:新規プロジェクトの作成Android Studioを起動し、「File」→「New Project」を選択して新しいプロジェクトを作成します。必要な項目をすべて入力してプロジェクトをセットアップしてください。ステップ2:レイアウトファイル(activity_main.xml)の編集res/layout/activity_main.xmlに以下のコー
-
【Android開発】インストール済みアプリの一覧を取得して表示する方法
この記事では、Android端末にインストールされているアプリケーションの一覧を取得し、ListViewで画面に表示する方法を解説します。PackageManagerクラスを利用すれば、端末内のパッケージ情報に簡単にアクセスできるようになります。 手順1:新規プロジェクトを作成する Android Studioを起動し、メニューから「File」→「New Project」を選択します。必要な項目を入力して、新しいプロジェクトを作成してください。 手順2:レイアウトファイル(activity_main.xml)を編集する res/layout/activity_main.xml に以下のコードを