将Android Java活动中的字符串传递给广播接收器

问题描述:

我花了最近几个小时查看有关此主题的其他问题,但没有发现能够给我任何答案。将Android Java活动中的字符串传递给广播接收器

什么是从活动字符串传递给后台的广播接收器的最佳方式?

这是我的主要活动

public class AppActivity extends DroidGap { 
SmsReceiver mSmsReceiver = new SmsReceiver(); 

    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);  

    ScrollView scroll; 
    scroll = new ScrollView(this); 

    Bundle bundle = getIntent().getExtras(); 
    final String ownAddress = bundle.getString("variable"); 

    registerReceiver(mSmsReceiver, new IntentFilter("MyReceiver")); 
      Intent intent = new Intent("MyReceiver"); 
       intent.putExtra("passAddress", ownAddress); 
      sendBroadcast(intent); 

      Log.v("Example", "ownAddress: " + ownAddress); 
} 
} 


这里是我的广播接收器

public class AppReceiver extends BroadcastReceiver { 
public void onReceive(Context context, Intent intent) { 

    final String ownAddress = intent.getStringExtra("passAddress"); 
    Toast test = Toast.makeText(context,""+ownAddress,Toast.LENGTH_LONG); 
    test.show(); 

    Log.v("Example", "ownAddress: " + ownAddress); 

} 
} 


这里是明显的对我的接收机

<service android:name=".MyService" android:enabled="true"/> 
<receiver android:name="AppReceiver"> 
      <intent-filter android:priority="2147483647"> 
       <action android:name="android.provider.Telephony.SMS_SENT"/> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      </intent-filter> 
<receiver> 
<service android:name=".MyServiceSentReceived" android:enabled="true"/> 
<receiver android:name="AppReceiver"> 
       <intent-filter android:priority="2147483645"> 
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
        <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       </intent-filter> 
</receiver> 

当广播接收器记录应用程序崩溃的事件。我需要让它在幕后运行并从我的主要活动中拉出一个字符串。

任何人都可以帮我解决这个问题,或者指点我一个正确的方向吗?从评论

+0

邮政logcat的。 – JoxTraex 2012-08-06 19:55:17

+0

加入@JoxTraex – localhost 2012-08-06 19:58:22

加法和聊天

您的字符串ownAddress将永远是空的,除非有意向与在该包额外的关键passAddress的字符串。每当您捕获接收器的意图(无论是从SMS_SENTSMS_RECEIVED,或BOOT_COMPLETEDownAddress将是空的,因为操作系统不提供命名passAddress额外的字符串。希望这能说明问题。

原来的答案

我没有使用过DroidGap但是这是你想要的一个普通的Android活动。

活动:

public class AppActivity extends Activity { 
    AppReceiver mAppReceiver = new AppReceiver(); 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     registerReceiver(mAppReceiver, new IntentFilter("MyReceiver")); 

     String string = "Pass me."; 
     Intent intent = new Intent("MyReceiver"); 
     intent.putExtra("string", string); 
     sendBroadcast(intent); 
    } 
} 

接收机:

public class AppReceiver extends BroadcastReceiver { 
    public void onReceive(Context context, Intent intent) { 
     Toast.makeText(context, intent.getStringExtra("string"), Toast.LENGTH_LONG).show(); 
    } 
} 

不要忘记注销接收器的onDestroy(),像这样:

@Override 
protected void onDestroy() { 
    unregisterReceiver(mAppReceiver); 
    super.onDestroy(); 
} 
+0

谢谢。这样可行。 – localhost 2012-08-06 20:23:48

+0

不客气。虽然许多方法都有描述性名称,但我没有解释这些行为,请不要犹豫,询问您是否需要一点澄清。 – Sam 2012-08-06 20:28:33

+0

其实,在我的接收器中,我现在有最终的字符串ownAddress = intent.getStringExtra(“passAddress”);捕捉传递的字符串,但是当我在我的HttpPost中使用“ownAddress”时,它返回为null。你知道这里会发生什么吗? – localhost 2012-08-06 20:36:54