广播接收器无法正常工作-为什么如此? [英] Broadcast receiver is not working - why so?
问题描述
我正在学习Android。
我尝试实现自定义静态广播接收器,但无法正常工作。
我从Google搜索了一些问题,但是找不到解决该问题的方法。
我在Android 7.0最低级别24目标级别28上工作
I'm studying Android. I try to implement a Custom static Broadcast Receiver but it is not working. I search for some issue from Google but I can't find something to solve this. I work on Android 7.0 Min Level 24 Target Level 28
事实上,当我启动活动时,MyStaticReceiver没有启动(无日志)
MyDynamicReceiver完美运行
In fact, MyStaticReceiver isn't launching when I start the Activity (no log) MyDynamicReceiver work perfectly
您有解决方案吗?
AndroidManifest.xml:
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="test.receiver">
<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">
<activity android:name="test.receiver.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".MyStaticReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="@string/StaticAction" />
</intent-filter>
</receiver>
</application>
</manifest>
MainActivity.java:
MainActivity.java :
public class MainActivity extends Activity {
public final static boolean Debug = true;
public final static String TAG = "TagDebug";
private MyDynamicReceiver dynamicReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ComponentName receiver = new ComponentName(this, MyStaticReceiver.class);
PackageManager pm = this.getPackageManager();
pm.setComponentEnabledSetting(receiver,
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP);
}
@Override
protected void onResume() {
super.onResume();
if (Debug) Log.i(TAG, "MainActivity:onResume");
configureDynamicReceiver();
}
@Override
protected void onDestroy(){
super.onDestroy();
if (Debug) Log.i(TAG, "MainActivity:onDestroy");
unregisterReceiver(dynamicReceiver);
}
public void onClickButton(View v) {
if (Debug) Log.i(TAG, "MainActivity:onClickButton");
Intent staticIntent = new Intent();
staticIntent.setAction(getString(R.string.StaticAction));
sendBroadcast(staticIntent);
Intent dynamicIntent = new Intent();
dynamicIntent.setAction(getString(R.string.DynamicAction));
sendBroadcast(dynamicIntent);
}
public void configureDynamicReceiver() {
if( dynamicReceiver == null ) {
dynamicReceiver = new MyDynamicReceiver();
}
IntentFilter filter = new IntentFilter(getString(R.string.DynamicAction));
registerReceiver(dynamicReceiver, filter);
}
}
MyStaticReceiver.java(MyDynamicReceiver是相同的... )
MyStaticReceiver.java (MyDynamicReceiver is the same ...)
public class MyStaticReceiver extends BroadcastReceiver {
public final static boolean Debug = true;
public final static String TAG = "TagDebug";
public MyStaticReceiver() {
if (Debug) Log.i(TAG, "MyStaticReceiver");
}
@Override
public void onReceive(Context context, Intent intent) {
if (Debug) Log.i(TAG, "MyStaticReceiver:onReceive");
}
}
推荐答案
您实际上正在发送隐式广播,因此清单中声明的接收方将无法使用。
You are actually sending an implicit broadcast, therefore the receiver declared in the manifest will not work.
如果您的应用是定位到Android 8.0(API级别26)或更高版本,您不能使用清单
为大多数隐式广播(
并非专门针对您的应用的广播)声明接收方。当用户正在积极使用您的应用时,您仍然可以使用
上下文注册的接收器。 链接
MyDynamicReceiver
不会遇到任何问题,因为它是上下文注册的接收器。
You don't face any issue with MyDynamicReceiver
because it is context-registered receiver.
但是要使其适用于 MyStaticReceiver
,您可以尝试发送显式广播通过在 Intent的构造函数中传递组件名称。
But to make it work for MyStaticReceiver
, you can try sending an explicit broadcast by passing the component name in the constructor of the Intent
.
Intent staticIntent = new Intent(this, MyStaticReceiver.class);
staticIntent.setAction(getString(R.string.StaticAction));
sendBroadcast(staticIntent);
这篇关于广播接收器无法正常工作-为什么如此?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!