2012-04-05 52 views
7

我想要做的是,我的應用程序發送消息到的數字,傳遞給BraodcastReceiver ...但到目前爲止,我得到空或BroadcastReceiver只是簡單地崩潰..如何將數據傳遞給BroadcastReceiver?

這是我的意圖從類的CreateMessage的BroadcastReceiver ...

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

而且在廣播接收器(SmsReceiver類),我試圖抓住意圖這樣的..

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(); 
    } 

我的清單文件:

<?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> 

我已經嘗試給出了#1所有的答案...但是沒有用......

請你們告訴我,我錯了...謝謝!

錯誤日誌:

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

Intent類有一個'putExtra()'方法。不需要顯式的'Bundle'構造。另外,'startActivity()'不會將意圖傳遞給廣播接收者 - 它開始一個活動。 – 2012-04-05 16:41:47

+0

如何廣播這個廣播意味着使用'sendBroadcast'和'sendOrderedBroadcast'? – 2012-04-05 16:44:52

+0

你爲什麼需要廣播?你的目標是在兩個活動之間傳遞數據?或者是什麼? – iTurki 2012-04-05 16:48:50

回答

22

繼@Jason的榜樣......我這樣做...

在MainActivity或任何活動在那裏,你想從

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

然後發送意圖在我SmsReceiver類我這樣做

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"); 

    } 
} 

而且在我manifest.xml中添加"my.action.string"雖然這是一個選項..

<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> 

工作般的魅力!

+0

我正在研究一個非常類似的概念,即我的應用程序需要處理。在我的情況下,我必須通過設備間機制將數據從一臺設備傳遞到另一臺設備,我已將其定義爲SMS。是否有可能像你一樣發送數據,通過給意圖賦值?如何讓我的應用程序成爲收到短信時的默認應用程序? – JPerk 2017-05-15 22:49:34

0

您在開始Activity,而不是廣播Intent。 嘗試改變

startActivity(intent); 

sendBroadcast(intent); 

UPDATE

您可以設置任何動作,沒有組件名稱爲Intent。 嘗試創建意向如下:

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

只是試過... null – 2012-04-05 17:22:01

+0

究竟什麼是空? – pepyakin 2012-04-05 17:58:01

+0

在我的接收類中,字符串receiveNumbers = intent.getStringExtras(「phN」;' – 2012-04-05 18:05:04

0

你會發送一個廣播,像這樣:

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

action參數是String相關於您註冊的BroadcastReceiver的動作。所以,如果您註冊的接收器,像這樣:

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

然後action"com.myapp.myaction"

+0

我在哪裏註冊我的BroadcastReceiver?在我的activity類中? – 2012-04-05 18:06:53

+0

是的,通常在'onCreate()'方法中。一定要在'onDestroy()'方法中調用'unregisterReceiver(receiver)'這意味着你需要有一個類的引用到接收器) – 2012-04-05 18:12:08

+0

請看我的清單文件...我試過你的代碼...但應用程序不斷崩潰 – 2012-04-06 16:00:27

-2

使用sendbroadcast而不是startactivity.it將工作.. !!

0

你的問題很簡單。只要改變onRecive()代碼就足夠了:

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"); 

    } 
} 
相關問題