Pass a string from an Android Java activity to a broadcast receiver

我的梦境 提交于 2019-12-12 21:02:04

问题


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?

Here is my main activity

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?


回答1:


Addition from comments and chat

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.

Original Answer

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

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

Receiver:

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

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

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


来源:https://stackoverflow.com/questions/11834742/pass-a-string-from-an-android-java-activity-to-a-broadcast-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!