gpt4 book ai didi

android - 为什么我的 BroadCast 接收器不工作?

转载 作者:行者123 更新时间:2023-11-29 14:44:38 24 4
gpt4 key购买 nike

我正在学习 BroadCastReceiver。我试图通过以下代码实现的是,当我切换到飞行模式时,我希望看到一个 Toast,无论应用程序是否打开。我没有做什么/做错了什么?请帮忙。谢谢

ConnectivityChangedReceiver.java类

public class ConnectivityChangedReceiver extends BroadcastReceiver {

@Override
public void onReceive( Context context, Intent intent ){

Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();

}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.kirathe.mos.c_max">
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<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=".ConnectivityChangedReceiver">
<intent-filter>
<action android:name="android.intent.action.AIRPLANE_MODE">
</action>
</intent-filter>

</receiver>

</application>

MainActivity.java`

public class MainActivity extends AppCompatActivity {

private TextView switchStatus;
private Switch mySwitch;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}

最佳答案

来自android文档:

https://developer.android.com/guide/components/broadcast-exceptions

As part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. However, several broadcasts are currently exempted from these limitations. Apps can continue to register listeners for the following broadcasts, no matter what API level the apps target.

https://developer.android.com/distribute/best-practices/develop/target-sdk

Google Play will require that new apps target at least Android 8.0 (API level 26) from August 1, 2018, and that app updates target Android 8.0 from November 1, 2018.

“android.intent.action.AIRPLANE_MODE” 不再在豁免广播列表中。因此,在 Activity 中而不是在 AndroidManifest 中注册您的广播接收器。

关于android - 为什么我的 BroadCast 接收器不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44007421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com