将字符串从Android Java活动传递到广播接收器 [英] Pass a string from an Android Java activity to a broadcast receiver

查看:81
本文介绍了将字符串从Android Java活动传递到广播接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have spent the last couple of hours looking through other questions on this topic and none that I found were able to give me any answers.

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

What is the best way to pass a string from an activity to a broadcast receiver in the background?

这是我的主要活动

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



这是我的广播接收器


Here is my broadcast receiver

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

 }
}



这是我的接收者的清单


Here is the manifest for my receiver

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

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

When the broadcast receiver logs an event the app crashes. I need to have it run behind the scenes and pull a string from my main activity.

有人可以帮助我解决这个问题或向正确的方向指出吗?

Can anyone help me with this or point me in the right direction?

推荐答案

添加评论和聊天

您的字符串 ownAddress 始终为null,除非Intent在Bundle Extras中具有键为 passAddress 的字符串。每当接收方发现意图时(无论是从 SMS_SENT SMS_RECEIVED 还是 BOOT_COMPLETED ownAddress 将为空,因为操作系统没有提供额外的字符串,名为 passAddress 。希望能解决问题。

Your String ownAddress will always be null unless the Intent has an String with the key passAddress in the the Bundle extras. Anytime your Receiver catches an Intent (whether from SMS_SENT, SMS_RECEIVED, or BOOT_COMPLETED) ownAddress will be null because the OS doesn't provide a String extra named passAddress. Hope that clears things up.

原始答案

我没用过DroidGap,但这是常规Android活动所需要的。

I haven't used DroidGap but this is what you want for a regular Android Activity.

活动:

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上注销接收器( ),就像这样:

Don't forget to unregister the receiver in onDestroy(), like this:

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

这篇关于将字符串从Android Java活动传递到广播接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆