2012-02-21 14 views
5

Próbuję nadać wiadomość toast za pomocą następującego kodu przedłużającego Activity. Ale transmisja nie jest odbierana przez inny numer Activity, toast nie jest wyświetlany. Czy ktoś może rozwiązać mój błąd? Głównym profilem działalności jest SendBroadcast.javaBroadcastReceiver Nie otrzymuję Broadcast

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 

public class SendBroadcast extends Activity { 

    public static String BROADCAST_ACTION = 
          "com.unitedcoders.android.broadcasttest.SHOWTOAST"; 

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

    public void sendBroadcast(View v) { 
     Intent broadcast = new Intent(); 
     broadcast.setAction(BROADCAST_ACTION); 
     sendBroadcast(broadcast); 
    } 
} 

Toast Wyświetlacz aktywny jest ToastDisplay.java

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.widget.Toast; 

public class ToastDisplay extends Activity { 

    private BroadcastReceiver receiver = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      Toast.makeText(getApplicationContext(), "received", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }; 

    @Override 
    protected void onResume() { 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(SendBroadcast.BROADCAST_ACTION); 
     registerReceiver(receiver, filter); 
     super.onResume(); 
    } 

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

i manifest.java jest następujący

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.broad" 
    android:versionCode="1" 
    android:versionName="1.0" > 
    <uses-sdk android:minSdkVersion="3" /> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".SendBroadcast" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <receiver android:name=".ToastReceiver" > 
      <intent-filter> 
       <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST" /> 
      </intent-filter> 
     </receiver> 
    </application> 
</manifest> 

Odpowiedz

9

Możliwe są dwa rodzaje broacast: statyczne i dynamiczne. Statyczne to te, które są zadeklarowane w pliku manifestu. Dynamiczny może być zadeklarowany w czasie wykonywania. Połączyłeś te dwa typy transmisji w jednej emisji.

Do zadeklarowania prostej emisji dynamicznej można użyć następującego kodu (bazującego na kodzie). Wyświetli toast po wyświetleniu aktywności.

import android.app.Activity; 
import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.content.IntentFilter; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Toast; 

public class BroadcastTestActivity extends Activity { 

    public static String BROADCAST_ACTION =  
          "com.unitedcoders.android.broadcasttest.SHOWTOAST"; 
    BroadcastReceiver br = new BroadcastReceiver() { 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.w("Check", "Inside On Receiver"); 
      Toast.makeText(getApplicationContext(), "received", 
        Toast.LENGTH_SHORT).show(); 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     IntentFilter filter = new IntentFilter(); 
     filter.addAction(BROADCAST_ACTION); 
     filter.addCategory(Intent.CATEGORY_DEFAULT); 
     registerReceiver(br, filter); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     sendBroadcast(); 
    } 

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

    public void sendBroadcast() { 
     Intent broadcast = new Intent(); 
     broadcast.setAction(BROADCAST_ACTION); 
     broadcast.addCategory(Intent.CATEGORY_DEFAULT); 
     sendBroadcast(broadcast); 
    } 
} 

Teraz zamiast pokazywać tosty możesz nazwać swoją nową aktywność. Poniższe działania zależą od Twoich potrzeb (co chcesz zrobić).

+0

ok bracie możesz mi powiedzieć, że muszę usunąć tę statyczną transmisję z manifestu i czy możesz podpowiedz mi, jaka będzie moja reszta plików .java. –

+0

To zależy od tego, co chcesz zrobić. Pokazałem wam bardzo prosty przykład, jak wyświetlić komunikat toastu, który jest wyświetlany po odebraniu transmisji. Możesz po prostu utworzyć nowy projekt i przetestować tę klasę. – Yury

+0

Przede wszystkim chciałbym wyświetlać TOast dynamicznie –

0

Spróbuj

<activity android:name=".SendBroadcast" android:label="@string/app_name"> 
    <intent-filter> 
     <action android:name="android.intent.action.MAIN" /> 
     <category android:name="android.intent.category.LAUNCHER" /> 
    </intent-filter> 
    <intent-filter> 
     <action android:name="android.intent.action.BROADCAST" /> 
    </intent-filter> 
</activity>  

W onCreate() rozmowy

sendBroadcast(v); 
+0

koleś nie działa próbował –

+0

brata –

+0

nie działa Jaki jest '

3

Gdzie jest ToastReceiver klasa?

<receiver android:name=".ToastReceiver"> 
    <intent-filter> 
     <action android:name="com.unitedcoders.android.broadcasttest.SHOWTOAST"/> 
</intent-filter> 
</receiver>` 

Zmień

public class ToastDisplay extends Activity { 
    private BroadcastReceiver receiver = new BroadcastReceiver() { 

} 

do

public class ToastReceiver extends BroadcastReceiver { 

} 
+0

nie działający brat –

-2
Button b1 = (Button)findViewById(R.id.button1); 

b1.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View arg0) { 
     try { 
      String RESULT = new TestAsyncTask().execute(" ").get(); 
      System.out.println("RESULT "+RESULT); 
     } 
     catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     catch (ExecutionException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
}); 
Powiązane problemy