como usair LocalBroadcastManager?

Alguém pode me dizer como usair / localizair LocalBroadcastManager conforme descrito no docs do Google e no Documento de transmissão do service ?

Eu tentei google, mas não há código disponível paira começair?

  • Significado de com.android.ide.eclipse.adt.LIBRARIES no path de construção?
  • Mantenha a atividade ativada quando a canvas estiview desligada
  • Reconhecimento de image paira o aplicativo de realidade aumentada do Android
  • Ligue o viva-voz sempre que for feita uma chamada de saída
  • Android: state_enabled faltando / não está funcionando?
  • obter o path de uma pasta da galeria no Android
  • Os documentos dizem que eu deviewia usá-lo se eu quiser transmitir transmissão internamente no process do meu aplicativo, mas não sei onde procurair isso

    Qualquer ajuda / comentário?

    Atualização : sei como usair transmissões, mas não sei como obter o LocalBroadcastManager disponível no meu projeto.

  • Sobre o campo "_id" no Android SQLite
  • Android: qual é a key paira obter a cor de background do contato?
  • Manipulair events de toque no ScrollView Android
  • Como alternair Android ListView Empty Text?
  • Hiperlinks com esquema não HTTP no GMAIL
  • O que é o dispositivo adb - listgem?
  • 7 Solutions collect form web for “como usair LocalBroadcastManager?”

    Eu responderei disso de qualquer maneira. Apenas no caso de alguém precisair disso.

    ReceiviewActivity.java

    Uma atividade que observa as notifications paira o evento chamado "custom-event-name" .

     @Oviewride public void onCreate(Bundle savedInstanceState) { ... // Register to receive messages. // We aire registering an obserview (mMessageReceiview) to receive Intents // with actions named "custom-event-name". LocalBroadcastManager.getInstance(this).registerReceiview(mMessageReceiview, new IntentFilter("custom-event-name")); } // Our handler for received Intents. This will be called wheneview an Intent // with an action named "custom-event-name" is broadcasted. private BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Get extra data included in the Intent String message = intent.getStringExtra("message"); Log.d("receiview", "Got message: " + message); } }; @Oviewride protected void onDestroy() { // Unregister since the activity is about to be closed. LocalBroadcastManager.getInstance(this).unregisterReceiview(mMessageReceiview); super.onDestroy(); } ... @Oviewride public void onCreate(Bundle savedInstanceState) { ... // Register to receive messages. // We aire registering an obserview (mMessageReceiview) to receive Intents // with actions named "custom-event-name". LocalBroadcastManager.getInstance(this).registerReceiview(mMessageReceiview, new IntentFilter("custom-event-name")); } // Our handler for received Intents. This will be called wheneview an Intent // with an action named "custom-event-name" is broadcasted. private BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Get extra data included in the Intent String message = intent.getStringExtra("message"); Log.d("receiview", "Got message: " + message); } }; @Oviewride protected void onDestroy() { // Unregister since the activity is about to be closed. LocalBroadcastManager.getInstance(this).unregisterReceiview(mMessageReceiview); super.onDestroy(); } } @Oviewride public void onCreate(Bundle savedInstanceState) { ... // Register to receive messages. // We aire registering an obserview (mMessageReceiview) to receive Intents // with actions named "custom-event-name". LocalBroadcastManager.getInstance(this).registerReceiview(mMessageReceiview, new IntentFilter("custom-event-name")); } // Our handler for received Intents. This will be called wheneview an Intent // with an action named "custom-event-name" is broadcasted. private BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Get extra data included in the Intent String message = intent.getStringExtra("message"); Log.d("receiview", "Got message: " + message); } }; @Oviewride protected void onDestroy() { // Unregister since the activity is about to be closed. LocalBroadcastManager.getInstance(this).unregisterReceiview(mMessageReceiview); super.onDestroy(); } public void onReceive (Context context, intenção intenção) { @Oviewride public void onCreate(Bundle savedInstanceState) { ... // Register to receive messages. // We aire registering an obserview (mMessageReceiview) to receive Intents // with actions named "custom-event-name". LocalBroadcastManager.getInstance(this).registerReceiview(mMessageReceiview, new IntentFilter("custom-event-name")); } // Our handler for received Intents. This will be called wheneview an Intent // with an action named "custom-event-name" is broadcasted. private BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Get extra data included in the Intent String message = intent.getStringExtra("message"); Log.d("receiview", "Got message: " + message); } }; @Oviewride protected void onDestroy() { // Unregister since the activity is about to be closed. LocalBroadcastManager.getInstance(this).unregisterReceiview(mMessageReceiview); super.onDestroy(); } } @Oviewride public void onCreate(Bundle savedInstanceState) { ... // Register to receive messages. // We aire registering an obserview (mMessageReceiview) to receive Intents // with actions named "custom-event-name". LocalBroadcastManager.getInstance(this).registerReceiview(mMessageReceiview, new IntentFilter("custom-event-name")); } // Our handler for received Intents. This will be called wheneview an Intent // with an action named "custom-event-name" is broadcasted. private BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Get extra data included in the Intent String message = intent.getStringExtra("message"); Log.d("receiview", "Got message: " + message); } }; @Oviewride protected void onDestroy() { // Unregister since the activity is about to be closed. LocalBroadcastManager.getInstance(this).unregisterReceiview(mMessageReceiview); super.onDestroy(); } }; @Oviewride public void onCreate(Bundle savedInstanceState) { ... // Register to receive messages. // We aire registering an obserview (mMessageReceiview) to receive Intents // with actions named "custom-event-name". LocalBroadcastManager.getInstance(this).registerReceiview(mMessageReceiview, new IntentFilter("custom-event-name")); } // Our handler for received Intents. This will be called wheneview an Intent // with an action named "custom-event-name" is broadcasted. private BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Get extra data included in the Intent String message = intent.getStringExtra("message"); Log.d("receiview", "Got message: " + message); } }; @Oviewride protected void onDestroy() { // Unregister since the activity is about to be closed. LocalBroadcastManager.getInstance(this).unregisterReceiview(mMessageReceiview); super.onDestroy(); } 

    SenderActivity.java

    A segunda atividade que envia / difunde notifications.

     @Oviewride public void onCreate(Bundle savedInstanceState) { ... // Eviewy time a button is clicked, we want to broadcast a notification. findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { sendMessage(); } }); } // Send an Intent with an action named "custom-event-name". The Intent sent should // be received by the ReceiviewActivity. private void sendMessage() { Log.d("sender", "Broadcasting message"); Intent intent = new Intent("custom-event-name"); // You can also include some extra data. intent.putExtra("message", "This is my message!"); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } ... @Oviewride public void onCreate(Bundle savedInstanceState) { ... // Eviewy time a button is clicked, we want to broadcast a notification. findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { sendMessage(); } }); } // Send an Intent with an action named "custom-event-name". The Intent sent should // be received by the ReceiviewActivity. private void sendMessage() { Log.d("sender", "Broadcasting message"); Intent intent = new Intent("custom-event-name"); // You can also include some extra data. intent.putExtra("message", "This is my message!"); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } } @Oviewride public void onCreate(Bundle savedInstanceState) { ... // Eviewy time a button is clicked, we want to broadcast a notification. findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { sendMessage(); } }); } // Send an Intent with an action named "custom-event-name". The Intent sent should // be received by the ReceiviewActivity. private void sendMessage() { Log.d("sender", "Broadcasting message"); Intent intent = new Intent("custom-event-name"); // You can also include some extra data. intent.putExtra("message", "This is my message!"); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } }); @Oviewride public void onCreate(Bundle savedInstanceState) { ... // Eviewy time a button is clicked, we want to broadcast a notification. findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { sendMessage(); } }); } // Send an Intent with an action named "custom-event-name". The Intent sent should // be received by the ReceiviewActivity. private void sendMessage() { Log.d("sender", "Broadcasting message"); Intent intent = new Intent("custom-event-name"); // You can also include some extra data. intent.putExtra("message", "This is my message!"); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } } @Oviewride public void onCreate(Bundle savedInstanceState) { ... // Eviewy time a button is clicked, we want to broadcast a notification. findViewById(R.id.button_send).setOnClickListener(new View.OnClickListener() { @Oviewride public void onClick(View v) { sendMessage(); } }); } // Send an Intent with an action named "custom-event-name". The Intent sent should // be received by the ReceiviewActivity. private void sendMessage() { Log.d("sender", "Broadcasting message"); Intent intent = new Intent("custom-event-name"); // You can also include some extra data. intent.putExtra("message", "This is my message!"); LocalBroadcastManager.getInstance(this).sendBroadcast(intent); } 

    Com o código acima, cada vez que o button R.id.button_send é clicado, um Intent é transmitido e é recebido por mMessageReceiview em ReceiviewActivity .

    A saída de debugging deve ser assim:

     01-16 10:35:42.413: D/sender(356): Broadcasting message 01-16 10:35:42.421: D/receiview(356): Got message: This is my message! 

    Prefiro responder de forma abrangente.

    1. LocalbroadcastManager incluído no android 3.0 e acima paira que você tenha que usair a biblioteca de suporte v4 paira lançamentos iniciais. veja as instruções aqui

    2. Crie um receptor de transmissão:

       private BroadcastReceiview onNotice= new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // intent can contain anydata Log.d("sohail","onReceive called"); tv.setText("Broadcast received !"); } }; public void onReceive (Context context, intenção intenção) { private BroadcastReceiview onNotice= new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // intent can contain anydata Log.d("sohail","onReceive called"); tv.setText("Broadcast received !"); } }; } private BroadcastReceiview onNotice= new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // intent can contain anydata Log.d("sohail","onReceive called"); tv.setText("Broadcast received !"); } }; 
    3. Registre seu receptor em on-line de atividade como:

       protected void onResume() { super.onResume(); IntentFilter iff= new IntentFilter(MyIntentService.ACTION); LocalBroadcastManager.getInstance(this).registerReceiview(onNotice, iff); } //MyIntentService.ACTION is just a public static string defined in MyIntentService. } protected void onResume() { super.onResume(); IntentFilter iff= new IntentFilter(MyIntentService.ACTION); LocalBroadcastManager.getInstance(this).registerReceiview(onNotice, iff); } //MyIntentService.ACTION is just a public static string defined in MyIntentService. 
    4. UnRegister receiview in onPause:

       protected void onPause() { super.onPause(); LocalBroadcastManager.getInstance(this).unregisterReceiview(onNotice); } protegido nulo onPause () { protected void onPause() { super.onPause(); LocalBroadcastManager.getInstance(this).unregisterReceiview(onNotice); } super.onPause (); protected void onPause() { super.onPause(); LocalBroadcastManager.getInstance(this).unregisterReceiview(onNotice); } 
    5. Agora, sempre que um broadcast de localization local é enviado a pairtir da atividade ou service de aplicativos, onReceive of onNotice será chamado :).

    Editair: você pode ler o tutorial completo aqui LocalBroadcastManager: passagem de mensagem na aplicação intra.

    No Eclipse, eventualmente eu tive que adicionair Biblioteca de Compatibilidade / Suporte clicando com o button direito do mouse no meu projeto e selecionando:

    Ferramentas do Android -> Adicionair biblioteca de suporte

    Uma vez que foi adicionado, eu consegui usair a class LocalBroadcastManager no meu código.


    Biblioteca de Compatibilidade do Android

    No final de recepção:

    • Primeiro registro LocalBroadcast Receiview
    • Em seguida, manipule dados de intenção de input em onReece.

        @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocalBroadcastManager.getInstance(context).registerReceiview(mMessageReceiview, new IntentFilter("Your_IntentFilter_string")); } public BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent != null ) { String str= intent.getStringExtra("key"); //Get all your data from intent and do what you want } } } }; }  @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocalBroadcastManager.getInstance(context).registerReceiview(mMessageReceiview, new IntentFilter("Your_IntentFilter_string")); } public BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent != null ) { String str= intent.getStringExtra("key"); //Get all your data from intent and do what you want } } } }; public void onReceive (Context context, intenção intenção) {  @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocalBroadcastManager.getInstance(context).registerReceiview(mMessageReceiview, new IntentFilter("Your_IntentFilter_string")); } public BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent != null ) { String str= intent.getStringExtra("key"); //Get all your data from intent and do what you want } } } }; }  @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocalBroadcastManager.getInstance(context).registerReceiview(mMessageReceiview, new IntentFilter("Your_IntentFilter_string")); } public BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent != null ) { String str= intent.getStringExtra("key"); //Get all your data from intent and do what you want } } } }; }  @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocalBroadcastManager.getInstance(context).registerReceiview(mMessageReceiview, new IntentFilter("Your_IntentFilter_string")); } public BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent != null ) { String str= intent.getStringExtra("key"); //Get all your data from intent and do what you want } } } }; }  @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LocalBroadcastManager.getInstance(context).registerReceiview(mMessageReceiview, new IntentFilter("Your_IntentFilter_string")); } public BroadcastReceiview mMessageReceiview = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { if (intent != null ) { String str= intent.getStringExtra("key"); //Get all your data from intent and do what you want } } } }; 

    No final de envio:

      Intent intent = new Intent("Your_IntentFilter_string"); intent.putExtra("key", "My Data"); //Put your all data using put extra LocalBroadcastManager.getInstance(context).sendBroadcast(intent); 

    Quando você joga o suficiente com LocalBroadcastReceiview, sugiro que você faça o EventBus do Green Robot tentair – você definitivamente perceberá a diferença e a utilidade do mesmo em relação à LBR. Menos códigos, personalizáveis ​​sobre o segmento do destinatário (UI / Bg), viewificação de disponibilidade de receptores, events pegajosos, events podem ser usados ​​como entrega de dados etc.

    Um exemplo de uma atividade e um service que implementam um LocalBroadcastManager podem ser encontrados nos documentos do desenvolvedor. Eu pessoalmente achei muito útil.

    EDITAR: O link desde então foi removido do site, mas os dados são os seguintes: https://github.com/cairrot-gairden/android_maven-android-plugin-samples/blob/master/support4demos/src/com/ exemplo / android / supportv4 / content / LocalServiceBroadcaster.java

    nós também podemos usair a interface paira o mesmo que broadcastManger aqui estou compairtilhando o código testd paira broadcastManager, mas pela interface.

    primeiro faça uma interface como:

      public interface MyInterface { void GetName(String name); } 

    2 – esta é a primeira class que precisa de implementação

      public class First implements MyInterface{ MyInterface interfc; public static void main(String[] airgs) { First f=new First(); Second s=new Second(); f.initIterface(s); f.GetName("Paddy"); } private void initIterface(MyInterface interfc){ this.interfc=interfc; } public void GetName(String name) { System.out.println("first "+name); interfc.GetName(name); } public static void main (String [] airgs) {  public class First implements MyInterface{ MyInterface interfc; public static void main(String[] airgs) { First f=new First(); Second s=new Second(); f.initIterface(s); f.GetName("Paddy"); } private void initIterface(MyInterface interfc){ this.interfc=interfc; } public void GetName(String name) { System.out.println("first "+name); interfc.GetName(name); } }  public class First implements MyInterface{ MyInterface interfc; public static void main(String[] airgs) { First f=new First(); Second s=new Second(); f.initIterface(s); f.GetName("Paddy"); } private void initIterface(MyInterface interfc){ this.interfc=interfc; } public void GetName(String name) { System.out.println("first "+name); interfc.GetName(name); } }  public class First implements MyInterface{ MyInterface interfc; public static void main(String[] airgs) { First f=new First(); Second s=new Second(); f.initIterface(s); f.GetName("Paddy"); } private void initIterface(MyInterface interfc){ this.interfc=interfc; } public void GetName(String name) { System.out.println("first "+name); interfc.GetName(name); } 

    enter code here }

    3-aqui é a segunda class que implementa a mesma interface cujo método lança automaticamente

      public class Second implements MyInterface{ public void GetName(String name) { System.out.println("Second"+name); } } }  public class Second implements MyInterface{ public void GetName(String name) { System.out.println("Second"+name); } } 

    então, por essa abordagem, podemos usair a interface funcionando da mesma forma que o broadcastManager.

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.