我正在学习Android。 我尝试实现自定义静态广播接收器,但无法正常工作。 我从Google搜索一些问题,但是找不到解决该问题的方法。 我在Android 7.0最低级别24目标级别28上工作
实际上,当我启动Activity时,MyStaticReceiver没有启动(无日志) MyDynamicReceiver完美运行
您有解决方案吗?
AndroidManifest.xml:
if (this.WindowState == FormWindowState.Normal)
{
ucChat.FlowChat.Size = new Size(323, 276);
ucChat.FlowChat.Left = (ucChat.ClientSize.Width - ucChat.FlowChat.Width) / 2;
ucChat.FlowChat.Anchor = (AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left);
}
MainActivity.java:
<?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>
MyStaticReceiver.java(MyDynamicReceiver是相同的...)
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);
}
}
答案 0 :(得分:1)
您实际上是在发送隐式广播,因此清单中声明的接收器将不起作用。
如果您的应用定位到Android 8.0(API级别26)或更高版本,则不能使用清单 声明大多数隐式广播的接收方( (不专门针对您的应用)。您仍然可以使用 用户正在积极使用您的应用程序时,在上下文中注册的接收者。 Link
MyDynamicReceiver
不会遇到任何问题,因为它是上下文注册的接收者。
但是要使其适用于MyStaticReceiver
,您可以尝试通过在Intent
的构造函数中传递组件名称来发送显式广播。
Intent staticIntent = new Intent(this, MyStaticReceiver.class);
staticIntent.setAction(getString(R.string.StaticAction));
sendBroadcast(staticIntent);