2

在过去的几个小时里,我一直在查看有关该主题的其他问题,但我发现没有一个能够给我任何答案。

在后台将字符串从活动传递到广播接收器的最佳方法是什么?

这是我的主要活动

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>

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

任何人都可以帮助我或指出正确的方向吗?

4

1 回答 1

3

从评论和聊天中添加

您的 StringownAddress将始终为 null ,除非 Intent 具有带有passAddressBundle extras 中的键的 String。任何时候您的 Receiver 捕获到一个 Intent (无论是来自SMS_SENTSMS_RECEIVED还是BOOT_COMPLETEDownAddress都将为空,因为操作系统不提供额外的 String 命名为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();
}
于 2012-08-06T20:04:39.923 回答