代码之家  ›  专栏  ›  技术社区  ›  ahmetvefa53

android后台广播接收机

  •  0
  • ahmetvefa53  · 技术社区  · 7 年前

    广播接收器是否在静态定义(清单中)的后台运行。有人能解释一下吗?另外,如何在api 26(oreo)和更高版本的后台运行broadcastreceiver。

    <receiver android:name=".ScreenOnOffReceiver"
                  android:enabled="true"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MY_PACKAGE_REPLACED"/>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                <action android:name="myintent"/>
            </intent-filter>
        </receiver>
    
    public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    }
    

    public class ScreenOnOffReceiver extends BroadcastReceiver {
    
    public final static String SCREEN_TOGGLE_TAG = "SCREEN_TOGGLE_TAG";
    
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d(SCREEN_TOGGLE_TAG, " action name : " + action);
    }
    

    }

    非常感谢。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Durgesh    7 年前

    由于android O Forward后台限制已被强制执行,只有一些广播(如BOOT_COMPLETED等)才允许进行静态注册。当应用程序处于前台时,任何其他应用程序都应该动态注册/注销。 https://developer.android.com/about/versions/oreo/background#broadcasts