0

У меня есть два приемникаAndroid: Как сделать один приемник и переключиться между намерениями?

<receiver 
     android:name=".BootReceiver" 
     android:enabled="true" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
    </receiver> 
    <receiver 
     android:name=".DateReceiver" 
     android:enabled="true" > 
     <intent-filter> 
      <action android:name="android.intent.action.DATE_CHANGED" /> 
     </intent-filter> 
    </receiver> 

Это является их ява реализаций

public class BootReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     Log.i("BootReceiver", "ok"); 
    } 
} 

public class DateReceiver extends BroadcastReceiver { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
    Log.i("DateReceiver", "new date"); 
    } 
} 

Я хотел бы сделать один приемник со всеми intent-filters и сделать переключатель, когда он получает намерение.

ответ

1

Добавьте его в манифесте как этот

<receiver android:name=".EventHandler" > 
      <intent-filter> 
       <action android:name="android.bluetooth.headset.action.VENDOR_SPECIFIC_HEADSET_EVENT" /> 
       <action android:name="android.bluetooth.headset.profile.action.AUDIO_STATE_CHANGED" /> 
       <action android:name="android.bluetooth.headset.profile.action.CONNECTION_STATE_CHANGED" /> 
       <action android:name="android.bluetooth.adapter.action.STATE_CHANGED" /> 
       <action android:name="android.net.wifi.STATE_CHANGE" /> 
       <action android:name="android.intent.action.HEADSET_PLUG"/> 
       <action android:name="android.hardware.action.NEW_PICTURE" /> 
       <action android:name="android.hardware.action.NEW_VIDEO" /> 
       <action android:name="android.intent.action.SCREEN_OFF" /> 
       <action android:name="android.intent.action.SCREEN_ON" /> 
       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" /> 
      </intent-filter> 
     </receiver> 

и получить их что-то подобное ....

@Override 
    public void onReceive(Context context, Intent intent) { 
     Log.d(TAG, "---------------------------------------"); 
     if(intent.getAction().equalsIgnoreCase(Intent.ACTION_AIRPLANE_MODE_CHANGED)){ 
      // Do Some Thing here... 
     } 
} 
+0

Я принял этот ответ, потому что он не использует жестко закодированные строки, но ссылки – user3290180

2
<receiver 
     android:name=".DataAndBootReceiver" 
     android:enabled="true" > 
     <intent-filter> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
     </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.DATE_CHANGED" /> 
     </intent-filter> 
    </receiver> 



    public class DataAndBootReceiver extends BroadcastReceiver { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction() 
        .equals("android.intent.action.DATE_CHANGED")) { 
       Log.i("Receiver", "Data"); 
      } else { 
       Log.i("Receiver", "Boot"); 
      } 
     } 
    }