android oreo

In this tutorial, we’ll discuss the changes in Broadcast Receiver since Android Oreo. We’ll see why the restrictions have been put on Background Operations in Android.

在本教程中,我们将讨论自Android Oreo以来Broadcast Receiver中的更改。 我们将了解为什么对Android的后台操作设置了限制。

安德罗广播接收机 (Androd Broadcast Receivers)

Broadcast Receivers are like Antennas. Just like Antennas can be tuned to catch certain frequencies, broadcast receivers can be registered to certain intent actions. When that action event triggers, the BroadcastReceiver gets called.

广播接收器就像天线。 就像可以调整天线以捕获某些频率一样,可以将广播接收器注册为某些意图动作。 当该动作事件触发时,将调用BroadcastReceiver。

隐式与显式广播接收器 (Implicit vs Explicit BroadcastReceivers)

Before we differentiate, let’s know the difference between Implicit and Explicit Intents.

在进行区分之前,让我们知道隐式意图和显式意图之间的区别。

Explicit Intents are used to call a particular component that you know of.
Implicit Intents are used when you don’t know the exact component to invoke.

显式意图用于调用您知道的特定组件。
当您不知道要调用的确切组件时,将使用隐式意图

Take the example of capturing a photo from camera or gallery. It’s an implicit intent, since calling the intent shows you a dialog of different applications to choose from. Same goes for email/browser intents (if they show you a dialog chooser).

以从相机或画廊捕获照片为例。 这是一个隐式意图,因为调用该意图会显示一个对话框,供您选择不同的应用程序。 电子邮件/浏览器的意图也是如此(如果它们向您显示对话框选择器)。

Implicit Broadcast Receivers aren’t exclusive to your application.
Actions such as ACTION_BOOT_COMPLETED or CONNECTIVITY_CHANGE are categorised in implicit broadcast receivers. This is because when these events happen, all the applications registered with the event will get the information.

隐式广播接收器并非仅适用于您的应用程序。
ACTION_BOOT_COMPLETEDCONNECTIVITY_CHANGE类的操作在隐式广播接收器中分类。 这是因为当这些事件发生时,向该事件注册的所有应用程序都将获取该信息。

Explicit Broadcast Receivers are exclusive to your application. Only your application’s broadcast receiver will get triggered when the custom intent action you define, gets called.

显式广播接收器是您的应用程序专有的。 调用您定义的自定义意图操作时,只会触发应用程序的广播接收器。

处理Android Oreo广播接收器 (Handling Android Oreo Broadcast Receivers)

Now, broadcast receivers run in the background. Hence it drains the battery. Imagine every application has registered android.net.conn.CONNECTIVITY_CHANGE in the receiver which is implicit.

现在,广播接收器在后台运行。 因此,它会耗尽电池电量。 想象每个应用程序都在接收者中隐式注册了android.net.conn.CONNECTIVITY_CHANGE

Now, whenever the wifi/mobile internet connection toggles, all the applications, with the receiver would get triggered. Imagine what happens when your wifi router is unstable. It can cause battery issues.

现在,每当wifi /移动互联网连接切换时,所有应用程序以及接收器都将被触发。 想象一下,当您的wifi路由器不稳定时会发生什么。 可能会导致电池问题。

AndroidManifest.xmlAndroidManifest.xml注册后,隐式广播接收器将无法工作

How to handle Implicit Receivers in Android Oreo?

如何在Android Oreo中处理隐式接收器?

To use Implicit Receivers in your application, you need to define them programmatically in your code, using registerReceiver().

要在应用程序中使用隐式接收器,您需要使用registerReceiver()在代码中以编程方式定义它们。

Using registerReceiver() we can programmatically register and unregisterReceiver() during the lifecycle of the activity. This way implicit receivers would only be called when our activity/application is alive and not at other times.

使用registerReceiver()我们可以在活动的生命周期中以编程方式注册和unregisterReceiver() 。 这样,隐式接收器仅在我们的活动/应用程序处于活动状态时才被调用,而在其他时间则不会被调用。

Besides registering receivers programmatically, we can use JobSchedulers too.

除了以编程方式注册接收者之外,我们还可以使用JobSchedulers

Several Implicit Broadcasts are exempted and can be declared in the Manifest:

某些隐式广播被豁免,可以在清单中声明:

Note: Beginning with Android Pie, The NETWORK_STATE_CHANGED_ACTION broadcast doesn’t receive information about the user’s location or personally identifiable data.

注意 :从Android Pie开始, NETWORK_STATE_CHANGED_ACTION广播不会接收到有关用户位置或个人身份数据的信息。

In the next section, we’ll be creating an application which implements Broadcast Receivers keeping the Android Oreo limitations.

在下一节中,我们将创建一个应用程序,该应用程序实现广播接收器,并保持Android Oreo的限制。

项目结构 (Project Structure)

码 (Code)

The AndroidManifest.xml file looks like the following:

AndroidManifest.xml文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"xmlns:tools="https://schemas.android.com/tools"package="com.journaldev.androidoreobroadcastreceiver"><applicationandroid: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"tools:ignore="GoogleAppIndexingWarning"><receiver android:name="com.journaldev.androidoreobroadcastreceiver.MyReceiver"android:enabled="true"><intent-filter><action android:name="com.journaldev.AN_INTENT" /></intent-filter></receiver><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>

We’ve defined an intent filter with an action which is explicit.

我们用一个明确的动作定义了一个意图过滤器。

Implicit ones will be defined in the code only:

隐式代码仅在代码中定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"android:gravity="center"tools:context=".MainActivity"><Buttonandroid:id="@+id/btnExplicitBroadcast"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Explicit Broadcast" /></LinearLayout>

The code for the MainActivity.java class is given below:

MainActivity.java类的代码如下:

package com.journaldev.androidoreobroadcastreceiver;import android.content.ComponentName;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {Button btnExplicitBroadcast;MyReceiver myReceiver;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);btnExplicitBroadcast = findViewById(R.id.btnExplicitBroadcast);btnExplicitBroadcast.setOnClickListener(this);myReceiver= new MyReceiver();}@Overrideprotected void onResume() {super.onResume();IntentFilter filter = new IntentFilter();filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");registerReceiver(myReceiver, filter);}public void broadcastIntent() {Intent intent = new Intent();intent.setAction("com.journaldev.AN_INTENT");intent.setComponent(new ComponentName(getPackageName(),"com.journaldev.androidoreobroadcastreceiver.MyReceiver"));getApplicationContext().sendBroadcast(intent);}@Overrideprotected void onStop() {super.onStop();unregisterReceiver(myReceiver);}@Overridepublic void onClick(View view) {switch (view.getId()) {case R.id.btnExplicitBroadcast:broadcastIntent();break;}}
}

On button click sendBroadcast() is called to send the explicit broadcast.

单击按钮后,将调用sendBroadcast()以发送显式广播。

Note: sendStickyBroadcasts() were used to send broadcast intents that stayed around for other receivers to access them at a later point. The method was deprecated in API 21.

注意: sendStickyBroadcasts()用于发送广播意图,这些意图sendStickyBroadcasts()其他接收者在以后使用。 该方法已在API 21中弃用。

The code for the MyReceiver.java BroadcastReceiver class is given below:

下面给出了MyReceiver.java BroadcastReceiver类的代码:

package com.journaldev.androidoreobroadcastreceiver;import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;public class MyReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();if (action.equals("com.journaldev.AN_INTENT")) {Toast.makeText(context, "Explicit Broadcast was triggered", Toast.LENGTH_SHORT).show();}if (("android.net.conn.CONNECTIVITY_CHANGE").equals(action)) {Toast.makeText(context, "Implicit Broadcast was triggered using registerReceiver", Toast.LENGTH_SHORT).show();}}
}

The output of the above application in action is given below:

上面应用程序的输出如下:

LocalBroadcastManager (LocalBroadcastManager)

A LocalBroadcastManager is used to send or receive events locally within the current application only.

LocalBroadcastManager仅用于在当前应用程序中本地发送或接收事件。

LocalBroadcastManager does not listen to system-wide broadcasts and is more secure than BroadcastReceiver.
Plus it has a less overhead hence communication speed is faster.

LocalBroadcastManager不收听系统范围的广播,并且比BroadcastReceiver更安全。
加上它具有较少的开销,因此通信速度更快。

It can be initialised as:

可以初始化为:

LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);localBroadcastManager.sendBroadcast(intent);

LocalBroadcastManager would fail if you make events that are system wide.

如果您进行系统范围的事件,LocalBroadcastManager将失败。

<receiver android:name=".MyReciever"android:permission="android.permission.SEND_SMS"><intent-filter><action android:name="MY_ACTION"/></intent-filter>
</receiver>
sendBroadcast(new Intent("MY_ACTION"), Manifest.permission.SEND_SMS);

Set the android:exported attribute to “false” in the receiver tag in the manifest restricts the application from receiving broadcasts from other applications.

在清单的接收器标签中将android:exported属性设置为“ false”,以限制该应用程序接收来自其他应用程序的广播。

This brings an end to this tutorial. You can download the project from the below link:

本教程到此结束。 您可以从以下链接下载项目:

AndroidOreoBroadcastReceiverAndroidOreoBroadcastReceiver
Github Project LinkGithub项目链接

翻译自: https://www.journaldev.com/23653/android-oreo-implicit-and-explicit-broadcast-receiver

android oreo

android oreo_Android Oreo隐式和显式广播接收器相关推荐

  1. 直播预告 | 斯坦福助理教授马腾宇:深度学习中的隐式和显式正则化

    人工智能作为科技领域最具代表性的技术,日益成为国际竞争的新焦点.当下,我国正逐步开展全民智能教育普及,设置人工智能相关课程,致力于建设人工智能的人才高地. 在此背景下,中关村海华信息技术前沿研究院立足 ...

  2. 如何在 C# 中使用隐式和显式操作符

    C# 有一个鲜为人知的特性是通过定义 显式和隐式操作符 实现类型之间的转换,这篇文章我们将会讨论如何使用这些 显式 和 隐式 操作符. 什么是显式,什么是隐式 隐式类型转换 它是运行时自动帮你完成的, ...

  3. intent隐式和显式_Neo4j:使隐式关系成为显式和双向关系

    intent隐式和显式 我最近阅读了Michal Bachman关于 Neo4j中双向关系的文章 ,他建议对于某些关系类型,我们对关系的方向不那么感兴趣,因此可以在查询时忽略它. 他使用以下示例显示N ...

  4. 5-Selenium WebDriver三种等待--隐式等待-显式等待和流畅等待

    在selenium中,Waits在执行测试中扮演重要角色.在本教程中,您将学习Selenium中"隐式"和"显式"等待的各个方面. 在Selenium中为什么需 ...

  5. 【两种解法】Quadtrees UVA - 297(隐式建树+显式建树)

    立志用最少的代码做最高效的表达 A quadtree is a representation format used to encode images. The fundamental idea be ...

  6. C++ dll的隐式与显式调用

     转载自:http://blog.sina.com.cn/s/blog_53004b4901009h3b.html 应用程序使用DLL可以采用两种方式:一种是隐式链接,另一种是显式链接.在使用D ...

  7. 接口的隐式和显式实现

    1:当类实现一个接口是,通常使用隐式接口实现,这样可以方便的访问接口方法和类自身具有的方法和属性 2:当类实现多个接口且接口包含相同的方法签名,此时使用显式接口实现.(标示出哪个接口属于哪个方法) 3 ...

  8. CAE 分析中 隐式和显式时间积分算法的python程序实现

    前两天,同事研究Dyna的显/隐式时间积分的差异和基本原理.想来自己也有三.四年没做这方面的编程了,对同事问的一些问题也一时犯迷瞪,索性就又看了一遍书,网上找了些资料,写了点代码,理了理思路,以备不时 ...

  9. js类型转换(隐式类型转换显式类型转换)

    我们需要了解任何计算都只能在相同的数据类型之间执行.如果我们强制JavaScript执行执行一些操作,例如在字符串中添加一个数字,在这种情况下,js编译器会默认将数字更改为字符串类型,然后将两者连接起 ...

最新文章

  1. 御水.20180506
  2. apache tomcat 整合
  3. 玩互联‮的网‬核心秘诀‮什是‬么? ​‎
  4. java如何使用while_java中的while(true)语句的用法是什么
  5. oracle怎么使表更工整,Oracle 表分区介绍与使用
  6. scala函数进阶篇
  7. 计算机网络2021题库
  8. photoshop图片显示为索引,解决办法
  9. 使用 CSS Color-Mix() 简化你的调色板
  10. pr制作节奏感抖动(变换)
  11. Win10照片应用的小bug——自动模糊图片(有过渡色)
  12. pandas 取excel 中的某一列_干货Python Pandas 做数据分析之玩转 Excel 报表分析
  13. 如何访问同一局域网内的其他电脑文件
  14. Flutter 保护你的APP数据安全
  15. rust编程-Druid UI框架-Widget trait
  16. HTML5七夕情人节表白网页_圣诞节3d相册(含音乐)_HTML+CSS+JS 求婚 html生日快乐祝福代码网页 520情人节告白代码 程序员表白源码 抖音3D旋转相册 js烟花代码
  17. 数控计算机软件的确认表怎么做,数控仿真系统操作(广数GSK980T)
  18. SSM实现的酒店管理系统
  19. Docker: 容器与镜像
  20. GRE动词同义词汇介绍:刺 尖锐

热门文章

  1. [转载] python实现三角形面积计算
  2. SPI通信实验---verilog(FPGA作为从机,使用可读可写)
  3. IDEA创建xml文件
  4. list排序成员函数对string对象与char*对象排序的差别
  5. 使用jsPlumb插件实现动态连线功能
  6. from import
  7. RedHat命令笔记
  8. Druid数据库连接池配置
  9. jdbcdbcpc3p0
  10. .NET日志工具介绍