24
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Android AlarmManagerを使ったステータスバー通知

Last updated at Posted at 2013-11-18

#AlarmManagerを使ったステータスバー通知の方法

AndroidManifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<!-- :remoteさせる必要はないみたいだが、googleのサンプルコードにこうなっていたので:remoteしている -->
<receiver
    android:name=".receiver.AlarmReceiver"
    android:process=":remote" >
</receiver>
<receiver android:name=".receiver.StateAlarmReceiver" >
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <action android:name="android.intent.action.DATE_CHANGED" />
        <action android:name="android.intent.action.TIMEZONE_CHANGED" />
        <action android:name="android.intent.action.TIME_SET" />
        <action android:name="android.intent.action.PACKAGE_REPLACED" />
    </intent-filter>
</receiver>

###セットの仕方

MainActivity.java
// AlarmManagerから通知を受け取るレシーバーを定義する
Intent intent = new Intent(context, AlarmReceiver.class);
// レシーバーで判断するため 適当でOK
intent.putExtra("intent_alarm_id_key", 1000);

PendingIntent sender = PendingIntent.getBroadcast(context, INTENT_ALARM_ID, intent,
PendingIntent.FLAG_UPDATE_CURRENT);

// 通知させたい時間をCalendarを使って定義する
Calendar calSet = Calendar.getInstance();
calSet.setTimeInMillis(System.currentTimeMillis());
calSet.setTimeZone(TimeZone.getDefault());

// 毎日17:30に通知を表示させる
calSet.set(Calendar.HOUR_OF_DAY, 17);
calSet.set(Calendar.MINUTE, 30);


AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
// AlarmManager.RTC_WAKEUPで端末スリープ時に起動させるようにする
// 1回だけ通知の場合はalarmManager.set()を使う
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),
// 一日毎にアラームを呼び出す
AlarmManager.INTERVAL_DAY, sender);

###アラーム通知を受け取るレシーバー

AlarmReceiver.java
    @Override
    public void onReceive(Context context, Intent intent) {
        messageId = intent.getIntExtra("intent_alarm_id_key", 0);

        // 通知をタップした時にMainActivityを立ち上げる
        Intent intent = new Intent(context, MainActivity.class);
        intent.setAction("intent_action");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(mContext, id, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        // ステータスバー通知の大きなアイコン画像
        Bitmap largeIcon = BitmapFactory.decodeResource(mContext.getResources(),
                R.drawable.ic_launcher);
        Builder builder = new Builder(mContext);
        // ステータスバー通知タイトル
        builder.setContentTitle(mContext.getString(R.string.msg_message_alarm_title));
        // ステータスバー通知テキスト
        builder.setContentText(mContext.getString(R.string.msg_message_alarm_content));
        // ステータスバー通知アイコン
        builder.setSmallIcon(R.drawable.icon_stat_notify);
        builder.setLargeIcon(largeIcon);
        // ステータスバーに通知される文字
        builder.setTicker(mContext.getString(R.string.msg_message_alarm_ticker));
        builder.setAutoCancel(true);
        builder.setDefaults(Notification.DEFAULT_ALL);
        // 音・LED・バイブレーション設定を端末にゆだねる
        builder.setContentIntent(pendingIntent);

        NotificationManager manager = (NotificationManager) mContext
                .getSystemService(Context.NOTIFICATION_SERVICE);
        manager.notify(0, builder.build());
    }


24
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
24
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?