2012-04-05 17 views
7

Co staram się robić to, że numery, do których moja aplikacja wysyła wiadomości, są przekazywane do BraodcastReceiver ... ale do tej pory albo otrzymuję null lub BroadcastReceiver po prostu zawiesza ..Jak przekazać dane do BroadcastReceiver?

tu jest mój intencją dla BroadcastReceiver z klasy CreateMessage ...

 Intent intent = new Intent(); 
     Bundle bundle = new Bundle(); 
     bundle.putString("phN", phoneNo); 
     intent.putExtras(bundle); 
     startActivity(intent); 

I BroadcastReceiver (klasa SmsReceiver) próbuję złapać intencję takiego ..

public void onReceive(Context context, Intent intent) { 
    //---get the SMS message passed in--- 
    Bundle bundle = intent.getExtras(); 
    try{ 
    //receiveNumbers = intent.getExtras().get("phN").toString(); 
     String receiveNumbers = intent.getStringExtra("phN"); 
     Toast.makeText(context, receiveNumbers, Toast.LENGTH_LONG).show(); 
    }catch(Exception e){ 
     e.printStackTrace(); 
    } 

Mój plik manifestu:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.AEM_n" 
     android:versionCode="2" android:versionName="2.0"> 
    <uses-sdk android:minSdkVersion="8" /> 
    <uses-permission android:name="android.permission.READ_CONTACTS"/> 
    <uses-permission android:name="android.permission.READ_SMS"/>  
    <uses-permission android:name="android.permission.SEND_SMS"/> 
    <uses-permission android:name="android.permission.RECEIVE_SMS"/> 

    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true"> 
     <activity android:name=".new_menu" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:label="@string/app_name" 
      android:name=".SetEvent" > 
      <intent-filter> 
       <action android:name="com.AEM_n.SETEVENT" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      </activity> 
      <activity 
      android:label="@string/app_name" 
      android:name=".AddParticipants" /> 
      <activity 
      android:label="@string/app_name" 
      android:name=".CreateMessage" /> 
      <activity 
      android:label="@string/app_name" 
      android:name=".DataBaseClass" /> 
      <activity 
      android:label="@string/app_name" 
      android:name=".IntentReceiver"/> 
      <activity 
      android:label="@string/app_name" 
      android:name=".SmsReceiver" 
      /> 
      <receiver android:name=".SmsReceiver" android:exported="true"> 
      <intent-filter android:priority="999"> 
      <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
      </intent-filter> 
      </receiver> 
    </application> 

</manifest> 

Próbowałem już wszystkie odpowiedzi podane Stackoverflow ... Ale nie ma sensu ...

Proszę mi powiedzieć, gdzie chłopaki mam zamiar złego ... Dzięki!

Error Log:

04-07 02:34:02.770: ERROR/AndroidRuntime(25593): FATAL EXCEPTION: main 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593): java.lang.RuntimeException: Error receiving broadcast Intent { act=com.myapp.myaction (has extras) } in [email protected] 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:722) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at android.os.Handler.handleCallback(Handler.java:587) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at android.os.Looper.loop(Looper.java:123) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at android.app.ActivityThread.main(ActivityThread.java:3701) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at dalvik.system.NativeStart.main(Native Method) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593): Caused by: java.lang.NullPointerException 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at com.AEM_n.SmsReceiver.onReceive(SmsReceiver.java:37) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:709) 
04-07 02:34:02.770: ERROR/AndroidRuntime(25593):  ... 9 more 
+1

Klasa Intent ma metodę "putExtra()". Nie ma potrzeby stosowania jawnej konstrukcji 'Bundle'. Ponadto funkcja 'startActivity()' nie zapewnia zamiaru emisji odbiorników - rozpoczyna działanie. –

+0

w jaki sposób nadawanie tego programu oznacza używanie 'sendBroadcast' i' sendOrderedBroadcast'? –

+0

dlaczego potrzebujesz transmisji? Twoim celem jest przekazanie danych między dwoma działaniami? albo co? – iTurki

Odpowiedz

22

Idąc za przykładem @Jason „s ... Zrobiłem to ...

W główną działalność lub każda działalność z gdzie chcesz wysłać intencję z

Intent intent = new Intent("my.action.string"); 
intent.putExtra("extra", phoneNo); \\ phoneNo is the sent Number 
sendBroadcast(intent); 

a potem w moim SmsReceiver klasy i zrobił to

public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 

    Log.i("Receiver", "Broadcast received: " + action); 

    if(action.equals("my.action.string")){ 
    String state = intent.getExtras().getString("extra"); 

    } 
} 

I manifest.xml dodałem "my.action.string" chociaż to była opcja ..

<receiver android:name=".SmsReceiver" android:enabled="true"> 
    <intent-filter> 
     <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
     <action android:name="my.action.string" /> 
     <!-- and some more actions if you want --> 
    </intent-filter> 
</receiver> 

działało jak uroku !!

+0

Pracuję nad całkiem podobną koncepcją, którą musi obsługiwać moja aplikacja. W moim przypadku muszę przekazać dane za pośrednictwem mechanizmu między urządzeniami, który zdefiniowałem jako SMS, z jednego urządzenia na drugie. Czy możliwe jest wysłanie danych tak jak Ty, przypisując do intencji wartości? Jak ustawić aplikację jako domyślną aplikację do obsługi po otrzymaniu wiadomości SMS? – JPerk

0

Ty rozpoczęciem Activity zamiast nadawania Intent. Spróbuj zmienić

startActivity(intent); 

do

sendBroadcast(intent); 

UPDATE:

ustawić żadnych działań i nie ma nazwy składnika do Intent. Spróbuj stworzyć intencję, jak następuje:

Intent intent = new Intent(context, YourReceiver.class); 
intent.putExtra("phN", phoneNo); 
sendBroadcast(intent); 
+0

po prostu wypróbowany ... null –

+0

Co dokładnie zerowy? – pepyakin

+0

W mojej klasie odbierania 'String receiveNumbers = intent.getStringExtras (" phN ";' –

0

Można by wysłać transmisję tak:

Intent intent = new Intent(action); 
intent.putExtra("phN", phoneNo); 
sendBroadcast(intent); 

Parametr action jest String że koreluje z działaniem ty zarejestrował BroadcastReceiver z. Więc jeśli jesteś zarejestrowany odbiornik tak:

MyBroadcastReceiver receiver = new MyBroadcastReceiver(); 
registerReceiver(receiver, new IntentFilter("com.myapp.myaction")); 

następnie action byłoby "com.myapp.myaction"

+0

gdzie mogę zarejestrować mój program BroadcastReceiver? W mojej klasie aktywności? –

+0

Tak, zazwyczaj w metodzie 'onCreate()' Pamiętaj również, aby wywołać 'unregisterReceiver (receiver)' w metodzie 'onDestroy()' (co oznacza, że ​​musisz mieć odwołanie do klasy do odbiornika) –

+0

Proszę zobaczyć mój plik Manifest ... Próbowałem twojego kodu ... ale aplikacja ciągle się zawiesza –

-2

użyć sendbroadcast zamiast startactivity.it będzie działać .. !!

0

Twój problem jest bardzo prosty. Wystarczy, że zmienimy kody onRecive() takie jak:

public void onReceive(Context context, Intent intent) { 
    String action = intent.getAction(); 

    Log.i("Receiver", "Broadcast received: " + action); 

    if(action.equals("my.action.string")){ 
     String state = bundle.getString("phN"); 

    } 
} 
Powiązane problemy