LoginSignup
18
19

More than 5 years have passed since last update.

Android N で 注意すること (その1 : 暗黙的なintentの挙動変更)

Last updated at Posted at 2016-03-10

Overview

3/9(US時間) に、Android N Developer Previewが出ました。

と、同時に developer.android.com において、behavior change が出ましたので、少しずつ読み解いていきたいと思います。

backgroundの最適化

Android N removes three implicit broadcasts in order to help optimize both memory use and power consumption. This change is necessary because implicit broadcasts frequently start apps that have registered to listen for them in the background. Removing these broadcasts can substantially benefit device performance and user experience.

抄訳:

Android Nでは、3つの暗黙的なbroadcast intentを、メモリー使用と、消費電力を最適化するために削除します。というのは、backgroundで、暗黙的なbroadcast intentをlistenしているアプリが、頻繁に起動するためです。これらを削除することによって、deviceのperformanceとユーザー体験に貢献しえます。

じゃぁ、何を削除するのか?ということですが、

  • CONNECTIVITY_ACTION
  • ACTION_NEW_PICTURE
  • ACTION_NEW_VIDEO

の3つです。

  • CONNECTIVITY_ACTIONは、targetSdkVersion=Nで影響します。で、AndroidManifest.xmlに、書いていても、受け取れなくなります。ただし、BroadcastReceiverをContext.registerReceiver()していた場合は、foregroundでアプリが動作しているときは、受け取ることができます。 http://developer.android.com/preview/features/background-optimization.html によると、
  • ACTION_NEW_PICTURE / ACTION_NEW_VIDEOは、targetSdkVersionにはよらないです。

なお、今後、この3つに限らず、もっと増やすかもとのことです。

代替案は?

CONNECTIVITY_ACTION

CONNECTIVITY_ACTIONは、前述のとおり、foregroundでだけでよければ、動的に
登録を行えば、大丈夫です。

や、

を使えとのことです。

JobSchedulerに関してはおなじみなので、むしろ、connectivity changeのbroadcast intentを使うのはどちらかというと、お作法的によろしくないので、特に目新しくはない感じです。

NEW_PICTURE / NEW_VIDEO

これらの対策は完全に新規です。

JobInfo.TriggerContentUri()
Encapsulates parameters required to trigger a job on content URI changes.

抄訳:

content URIの変化でjobを起動させるのに必要なパラメータを封入します。

JobInfo.Builder.addTriggerContentUri()
Passes a TriggerContentUri object to JobInfo. A ContentObserver monitors the encapsulated content URI. If there are multiple TriggerContentUri objects associated with a job, the system provides a callback even if it reports a change in only one of the content URIs.
Add the TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTS flag to trigger the job if any descendants of the given URI change. This flag corresponds to the notifyForDescendants parameter passed to registerContentObserver().

抄訳:

TriggerContentUri objectをJobInfoに渡します。ContentObserverは、封入されたcontent URIを監視し、システムがcallbackを提供します。これは、たとえ、複数のcontent URIのうち1つでも変化したらreportします。与えられたURIに由来するどんな変化も必要なときは、TriggerContentUri.FLAG_NOTIFY_FOR_DESCENDANTSのflagをJobをtriggerするために追加してください。このflagは、registerObserverに渡すnotifyForDescendantsのparameterに対応します。

が追加になっています(ちなみに、定期実行はできない)。

ようは、JobSchedulerで、contentの変化をtrigger条件にして、Jobを起動できるようになったということです。

確認のためのコマンドも追加されているとのことです。

$ adb shell cmd appops set RUN_IN_BACKGROUND ignore
18
19
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
18
19