【问题标题】:My Custom BroadcastReceiver is not working above API level 16我的自定义 BroadcastReceiver 无法在 API 级别 16 以上工作
【发布时间】:2019-02-16 18:51:30
【问题描述】:

我正在做我的大学项目,它是家长控制应用程序。我正在创建一个自定义接收器。我在 jelly bean、kitkat、lollipop、marshmallow、naughat 和 oreo 中测试了该应用程序。但它只适用于果冻豆和奇巧。我尝试阅读stackoverflow中的所有解决方案。请给我解决方案!

manifest.xml

<?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.nizam.training.parentalcontrol">
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />


<application
    android:fullBackupContent="false"
    android:allowBackup="true"
    android:icon="@mipmap/m"
    android:label="@string/app_name"
    android:logo="@mipmap/ico"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <service
        android:name=".BlockService"
        android:enabled="true"
        android:exported="true"/>

    <activity android:name=".AppSettingActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
    </activity>
    <activity
        android:name=".BlockActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
    <activity
        android:name=".MainActivity"
        android:theme="@style/Theme.AppCompat.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".LoginActivity"
        android:label="Login"
        android:theme="@style/MyTheme" />
    <activity
        android:name=".SignupActivity"
        android:label="Create Pin"
        android:theme="@style/MyTheme" />
    <activity
        android:name=".TaskList"
        android:label="Tasks"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="true"
        android:permission=""
        tools:ignore="ExportedReceiver">
        <intent-filter>
            <action android:name="StartupReceiver_Manual_Start" />
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </receiver>
    <receiver android:name=".CheckRunningApplicationReceiver" />
</application></manifest>

在TaskListActivity.java中调用广播

getApplicationContext.sendBroadcast(new Intent("StartupReceiver_Manual_Start"))

LogCat

http://mnktalktech.blogspot.com/2019/02/logcat-for-parentalcontrol.html

【问题讨论】:

  • 如果你在模拟器上运行,请检查这个,stackoverflow.com/questions/28966496
  • @amitava 我使用 apk 文件安装了这个应用程序,在他的设备中我的自定义广播接收器也无法正常工作。
  • 请使 android:exported="false" 并删除 tools:ignore="ExportedReceiver" 并尝试。
  • @amitava 我也试过了。而且我尝试了很多方法,但没有找到解决方案。
  • 我使用 Nougat 和 Oreo(Go),并且使用了 BroadcastReceiver。我的代码有点不同。我可以将其发布为答案吗?

标签: android


【解决方案1】:

我找到了这个;这可能是问题的一部分。你有什么错误吗?

GET_TASKS Permission Deprecated

【讨论】:

  • 我已删除 GET_TASKS 并运行应用程序,但存在同样的问题。
  • API 级别 16 未发生错误。但是在以后的 API 级别中,我可以在 Logcat 中看到太多的红线,但我无法理解。
  • 你能把那个 Logcat 消息放在你问题的代码中吗?
  • @amitava Kk 我将包括 logcat。
【解决方案2】:

请尝试一下。

<receiver
        android:name=".MyReceiver"
        android:enabled="true"
        android:exported="false"
        >
        <intent-filter>
            <action android:name="StartupReceiver_Manual_Start"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </receiver>

然后告诉我。

【讨论】:

    【解决方案3】:

    在这个问题上花了一周的时间后,我找到了解决方案。 我之前放的东西(不起作用)...

    getApplicationContext.sendBroadcast(new Intent("StartupReceiver_Manual_Start"))
    

    现在我改变了。

    MyReceiver mr=new MyReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction("StarctupReceiver_Manual_Start");
    registerReceiver(mr,intentFilter);
    getBaseContext().getApplicationContext().sendBroadcast(new Intent("StarctupReceiver_Manual_Start"));
    

    我不知道它为什么起作用,但它起作用了。如果有人知道原因,请发表评论。感谢所有试图解决此问题的人。

    【讨论】:

      猜你喜欢
      • 2014-01-01
      • 1970-01-01
      • 2016-05-17
      • 2019-03-08
      • 1970-01-01
      • 2017-06-22
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多