Program Tip

매니페스트 대 활동에 브로드 캐스트 수신기 등록

programtip 2020. 11. 11. 20:32
반응형

매니페스트 대 활동에 브로드 캐스트 수신기 등록


실행중인 활동 또는 서비스에서 등록해야하는 것과 비교하여 방금 매니페스트에 등록했을 때 브로드 캐스트 수신기가 작동 할 것으로 예상 할 수있는시기를 이해하려면 도움이 필요합니다.

예를 들어 다음 인 텐트 필터를 사용하여 독립형 수신기를 등록하면 서비스 / 활동 참조없이 작동합니다.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.blk_burn.standalonereceiver"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >

        <receiver android:name="TestReceiver">
            <intent-filter>
                <action android:name="android.media.AUDIO_BECOMING_NOISY"/>
            </intent-filter>
        </receiver>

    </application>

</manifest>

내가 대체 할 경우 android.media.AUDIO_BECOMING_NOISYandroid.intent.action.HEADSET_PLUG수신기 트리거되지 않습니다 ( 안드로이드 문서 )

이 사이트에서 찾은 내용에서 이미 실행중인 활동 또는 서비스에서이 수신기를 등록해야합니다 ( Post ).

  • 누구든지 매니페스트에서 의도 필터를 조정할 때 이것이 작동하지 않는 이유와 수신자를 참조 / 등록하는 백그라운드에서 실행되는 서비스가 필요한 이유를 말해 줄 수 있습니까?

  • 인 텐트 필터를 사용하여 내 앱의 매니페스트에 수신기를 등록 할 수 있도록 해결 방법이 android.intent.action.HEADSET_PLUG있습니까?

  • 매니페스트에 올바른 필터를 사용하는 것과 비교하여 서비스 또는 활동을 등록하는 데 필요한 Android 문서의 브로드 캐스트 작업을 어떻게 식별 할 수 있습니까?


수신기가 매니페스트에 등록되어 있고 앱이 실행되고 있지 않으면 브로드 캐스트를 처리하기위한 새 프로세스가 생성됩니다. 코드에 등록하면 등록한 활동 / 서비스의 수명과 관련이 있습니다. 일부 브로드 캐스트의 경우 새 앱 프로세스가 존재하지 않거나 일부가있는 경우 생성하는 것이 실제로 의미가 없습니다. 보안, 성능 등의 영향을 미치므로 수신기를 코드로만 등록 할 수 있습니다.

에 관해서는 HEADSET_PLUG방송, 아이디어가 앱이, 당신이 정말로 헤드폰이 분리되지 신경 안 실행하지 않는 경우 이미 실행중인 응용 프로그램이 등 UI, 볼륨에 응용 프로그램 고유의 조정을 얻을 수있는 것 같다.

AFAIK,이 정보가 모든 방송에 대해 요약되는 단일 장소는 없지만 각 Intent에는 등록 및 사용 방법에 대한 JavaDoc에 대한 주석이 있어야하지만 분명히 장소가 부족합니다. Intent.FLAG_RECEIVER_REGISTERED_ONLY에 대한 Android 소스 트리를 grep하면 목록을 컴파일 할 수 있습니다 .


일반적인 브로드 캐스트 수신기는 매니페스트 파일 AndroidManifest.xml에서 구성 할 수 있습니다. 이러한 방식으로 구성된 BroadcastReceiver를 정적으로 등록이라고합니다.

다음 요소를 사용하여 매니페스트 파일에 수신자를 등록 할 수 있습니다.

<receiver
   android:name=".ConnectivityChangeReceiver">
   <intent-filter>
      <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
   </intent-filter>
</receiver>

중첩 된 요소는 수신자가 반응해야하는 이벤트를 지정하는 데 사용됩니다.

다이내믹 브로드 캐스트 수신기

As an alternative you can register your BroadcastReceiver implementation dynamically in your code. You just need to call the registerReceiver() method on your Context object.

The registerReceiver() method takes two parameters:

The arguments of the registerReceiver() method

  • receiver : The BroadcastReceiver you want to register
  • filter : The IntentFilter object that specifies which event your receiver should listen to.

When you register your receiver in this way, it lives for as long as the component livesand Android sends events to this receiver until the creating component itself gets destroyed.

It’s your task to handle the lifecycle correctly. Thus when you add a receiver dynamically, take care to unregister the same receiver in the onPause() method of your Activity!

I suggest to register the receiver in the onResume() method of your Activity and to unregister it in your onPause() method:

@Override
protected void onPause() {
   unregisterReceiver(mReceiver);
   super.onPause();
}

@Override
protected void onResume() {
   this.mReceiver = new ConnectivityChangeReceiver();
   registerReceiver(
         this.mReceiver, 
         new IntentFilter(
               ConnectivityManager.CONNECTIVITY_ACTION));
   super.onResume();
}

When to use which method to register

Which method to use for registering your BroadcastReceiver depends on what your app does with the system event. I think there are basically two reasons why your app wants to know about system-wide events:

  • Your app offers some kind of service around these events
  • Your app wants to react graciously to state changes

Examples for the first category are apps that need to work as soon as the device is booted or that must start some kind of work whenever an app is installed. Battery Widget Pro or App2SD are good examples for these kinds of apps. For this type you must register the BroadcastReceiver in the Manifest file.

Examples for the second category are events that signal a change to circumstances your app might rely on. Say your app depends on an established Bluetooth connection. You have to react to a state change – but only when your app is active. In this case there is no need for a statically registered broadcast receiver. A dynamically registered one would be more reasonable.

There are also a few events that you are not even allowed to statically register for. An example for this is the Intent.ACTION_TIME_TICK event which is broadcast every minute. Which is a wise decision because a static receiver would unnecessarily drain the battery.

참고URL : https://stackoverflow.com/questions/10876015/broadcast-receiver-register-in-manifest-vs-activity

반응형