搜尋此網誌

2018年3月11日 星期日

【Android O】about between implicit and explicit intents (Broadcast Receiver) (Notification)

因為Android 8.0(O)的關係,讓原先的Notification上的custom button失效。
原因為Implicit Broadcast Exceptions這篇文章。
擷取開頭描述
As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. However, several broadcasts are currently exempted from these limitations. 
....

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) 
不能在manifest中broadcast receivers註冊broadcasts為implicit 。這段話浪費我好久久。


原來intent 有分為 implicit 和 explicit。
  1. Explicit Intent: Explicit intent names the component.
  2. Implicit Intent: Implicit Intents have not specified a component.
光從字面上,根本搞不懂發生什麼。還是從範例來了解:

Implicit activity call

With an intent filter you create action for your activity so other apps can call your activity via an action:
<activity android:name=".BrowserActivitiy" android:label="@string/app_name">
  <intent-filter>
     <action android:name="android.intent.action.VIEW" />
     <category android:name="android.intent.category.DEFAULT" />
     <data android:scheme="http"/> 
  </intent-filter>
</activity>
.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
startActivity(intent);

Explicit activity call

You make a call that indicates exactly which activity class to use:
Intent intent = new Intent(this, ActivityABC.class);
startActivity(intent);
解讀後,從上面例子同理反推,



Build Version >Android 8.0,在manifest.xml <receiver>時自己定義<intent-filiter> action(Implicit Receiver),更改成Explicit Receiver

若要合法使用<intent-filiter> action,只能參考Implicit Broadcast Exceptions文章下面列的這些反應動作。

github找到的例子
 manifest.xml中修改註冊為Explicit Receiver

在activity中把intent改為explicit intent


補充 下面連結有提到 為什麼要限制Implicit Broadcast的解釋
JobIntentService for background processing on Android O

再補上簡體中文的機制說明和解決辦法
从源码角度看Android_O_AMS新特性

Android O 8.0 BroadcastReceiver 注册工具


【Android】How to increase the height of remoteview in android notification

ref:
https://stackoverflow.com/a/36891181

Notification height is limited to 256dp on Android. Views will be clipped at the bottom that exceed this height.

原來Notification高度是有限制的,不像是bottomsheet可以填充整個畫面。須注意。

【Android】理解RemoteViews

ref:
《Android 开发艺术探索》 05-理解RemoteViews 抄书系列

這篇文章是對解釋remoteview超詳細,比官網還有用,先記錄一下。

如果早先看到這,懂裡面的原理,可以少走冤枉路。