Android 静态广播接收器不工作
我正在尝试创建一个静态广播接收器,但它不工作。 我使用的是API级别25 雄激素单Android 静态广播接收器不工作,android,android-studio,kotlin,broadcastreceiver,Android,Android Studio,Kotlin,Broadcastreceiver,我正在尝试创建一个静态广播接收器,但它不工作。 我使用的是API级别25 雄激素单 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.bro
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.broadcastreceivertry">
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<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"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name=".ScreenReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON"/>
<action android:name="android.intent.action.SCREEN_OFF"/>
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.kt
package com.example.broadcastreceivertry
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.telephony.SmsManager
import android.widget.Button
class MainActivity : AppCompatActivity() {
var SCREEN_INTENT = "Screen.Intent"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
代码运行后,无法接收android系统的广播
如果我在MainActivity中动态注册这些接收器,那么就没有问题了。但在这个静态注册接收机中,我无法获得广播
注意:如果您的应用程序目标API级别为26或更高,则无法使用
声明隐式广播(广播)接收器的清单
不专门针对你的应用),除了一些
不受该限制的广播。在大多数情况下,你
可以改为使用计划作业
这意味着,若您真的需要广播接收器,那个么您应该通过代码动态地注册和注销它
正如@MD提到的,URL说明了为什么它不被允许,以及如何处理这个问题
安卓7.0
Android 7.0(API级别24)及更高版本不会发送以下系统广播:
ACTION_NEW_PICTURE
ACTION_NEW_VIDEO
此外,针对Android 7.0及更高版本的应用程序必须使用
registerReceiver(广播接收器、意图过滤器)
。在清单中声明接收者不起作用
将描述广播接收器的所有变化检查我在API 25而不是26上使用它。所以这可能不是问题。@MeetMaheshwari检查我的答案then@MD,我目前使用的是Android 7.1.1,即API 25,但仍然遇到同样的问题。@Jassubek,不,你的回答没有帮助。我在android模拟器上使用的是stock android。
ACTION_NEW_PICTURE
ACTION_NEW_VIDEO