1

我尝试学习 LocalBroadcastManager。在网上搜索并实现了一个简单的应用程序,其中包含两个活动来执行 LocalBroadcastManager。但它不起作用。我找不到我的代码有什么问题。请帮我!

这是我的代码。

第一活动.java

public class FirstActivity extends Activity {

private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_first);

    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(FirstActivity.this, SecondActivity.class));
            Intent intent = new Intent("localReceiver");
            LocalBroadcastManager.getInstance(FirstActivity.this).sendBroadcast(intent);
        }
    });
}    }

SecondActivity.java

public class SecondActivity extends Activity {

private TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);
    textView = (TextView) findViewById(R.id.hi_text);
    LocalBroadcastManager.getInstance(this).registerReceiver(message, new IntentFilter("localReceiver"));
}

@Override
protected void onPause() {
    super.onPause();
    LocalBroadcastManager.getInstance(this).unregisterReceiver(message);
}

private BroadcastReceiver message = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d("SecondActivity", "Receiver Initiated...");
        textView.setText("Intent receiver activated");
    }
};   }

AndroidManifest.xml

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".FirstActivity"
        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:name=".SecondActivity"
        android:windowSoftInputMode="adjustResize|stateHidden"
        android:label="SecondActivity" />

</application>
4

2 回答 2

2

你正在从 广播一些东西FirstActivity,并且你期望它SecondActivity接收它。为了实现这一点,您应该能够同时运行两个不同Activity的 s,这在设计上是不可能的。当您广播字符串时,您没有人在听它。尝试使用 aService广播String,并恢复接收Activity,它将起作用

于 2015-07-09T09:33:04.313 回答
1

SecondActivity看起来像这样:

private BroadcastReceiver uiUpdateReceiver; 

onStart()

@Override
protected void onStart() {
    LocalBroadcastManager.getInstance(SecondActivity.this).registerReceiver((uiUpdateReceiver), new IntentFilter("Your_Data");
    super.onStart();
}

onCreate()

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    uiUpdateReceiver = new BroadcastReceiver() {
           @Override
           public void onReceive(Context context, Intent intent) {

           }
     };
}

onStop()

@Override
protected void onStop() {
    LocalBroadcastManager.getInstance(ChatActivity.this).unregisterReceiver(uiUpdateReceiver);
    super.onStop();
}

希望它会帮助你。

于 2015-07-09T10:09:15.060 回答