BroadcastReceiver 收不到广播事件消息
BroadcastReceiver doesn't receive the broadcast event message
我创建了我的广播接收器 AnprEventReciever,它应该在连接状态改变时被触发,但它并没有。
AnprEventReciever:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import bi.anpr.layouts.SplashActivity;
public class AnprEventReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("test","event recieved");
Toast.makeText(context, "event recieved" , Toast.LENGTH_LONG).show();
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sxx.vlctest">
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="bi.anpr.vlc.VLCApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_anpr_launcher_round"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name="bi.anpr.core.AnprEventReciever"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name="bi.anpr.layouts.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.VideoResourceActivity"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.ZoneActivity"
android:label="Zone Setter"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.SettingsActivity"
android:label="@string/title_activity_settings"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
请注意,我已经测试了几乎所有可能的操作都失败了 除了 BOOT_COMPLETED,我的 BroadcastReceiver 被成功触发。
感谢 @TheWanderer 通过他的评论帮助我找到答案。在 Android 8.0 Behavior Changes Note 开发人员中提到了以下内容:
Apps cannot use their manifests to register for most implicit
broadcasts (that is, broadcasts that are not targeted specifically at
the app).
所以我尝试像下面这样以编程方式注册我的 BroadcastReceiver,而不是为其创建清单条目:
private AnprEventReciever myReceiver;
private IntentFilter filter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_resource);
filter = new IntentFilter();
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
myReceiver = new AnprEventReciever();
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(myReceiver, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(myReceiver);
}
它工作得很好,但不幸的是,操作 "android.intent.action.CONNECTIVITY_CHANGE" 似乎已被弃用,我在 Intent class 中找不到它,我不知道它是否在其他地方找到。
注意: 在阅读过程中我发现当 activity 暂停或销毁时有必要注销广播接收器,否则你可能会得到尝试注册或重新注册时出错。
更新:
使用 ConnectivityManager.CONNECTIVITY_ACTION 而不是 "android.intent.action.CONNECTIVITY_CHANGE" 创建 intent 过滤器以接收连接改变的广播。
我创建了我的广播接收器 AnprEventReciever,它应该在连接状态改变时被触发,但它并没有。
AnprEventReciever:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
import bi.anpr.layouts.SplashActivity;
public class AnprEventReciever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("test","event recieved");
Toast.makeText(context, "event recieved" , Toast.LENGTH_LONG).show();
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sxx.vlctest">
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="bi.anpr.vlc.VLCApplication"
android:allowBackup="false"
android:icon="@mipmap/ic_anpr_launcher_round"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver
android:name="bi.anpr.core.AnprEventReciever"
android:enabled="true"
android:exported="true"
android:label="StartMyServiceAtBootReceiver">
<intent-filter>
<action android:name="android.intent.action.CONNECTIVITY_CHANGE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<activity
android:name="bi.anpr.layouts.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.VideoResourceActivity"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.ZoneActivity"
android:label="Zone Setter"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.SettingsActivity"
android:label="@string/title_activity_settings"
android:screenOrientation="portrait" />
<activity
android:name="bi.anpr.layouts.SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>
请注意,我已经测试了几乎所有可能的操作都失败了 除了 BOOT_COMPLETED,我的 BroadcastReceiver 被成功触发。
感谢 @TheWanderer 通过他的评论帮助我找到答案。在 Android 8.0 Behavior Changes Note 开发人员中提到了以下内容:
Apps cannot use their manifests to register for most implicit broadcasts (that is, broadcasts that are not targeted specifically at the app).
所以我尝试像下面这样以编程方式注册我的 BroadcastReceiver,而不是为其创建清单条目:
private AnprEventReciever myReceiver;
private IntentFilter filter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_resource);
filter = new IntentFilter();
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
myReceiver = new AnprEventReciever();
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(myReceiver, filter);
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(myReceiver);
}
它工作得很好,但不幸的是,操作 "android.intent.action.CONNECTIVITY_CHANGE" 似乎已被弃用,我在 Intent class 中找不到它,我不知道它是否在其他地方找到。
注意: 在阅读过程中我发现当 activity 暂停或销毁时有必要注销广播接收器,否则你可能会得到尝试注册或重新注册时出错。
更新:
使用 ConnectivityManager.CONNECTIVITY_ACTION 而不是 "android.intent.action.CONNECTIVITY_CHANGE" 创建 intent 过滤器以接收连接改变的广播。