0
public class MyReceiver extends BroadcastReceiver {
private static IReceiveMessage iReceiveMessage;
@Override
public void onReceive(Context context, Intent intent) {
       iReceiveMessage.receiveMessage(intent.getStringExtra("message"));
}       
public static void setReceiveMessage(IReceiveMessage iReceiveMessage1) {
    iReceiveMessage = iReceiveMessage1;
}}

public interface IReceiveMessage {
public void receiveMessage(String message);
}




public class MainActivity extends Activity implements IReceiveMessage{

    protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
              MyReceiver.setReceiveMessage(this);

}
     @Override
public void onReceiverMessage(String message) {
Log.i("test", message);

}
}

Mainactivity が終了した後も MyReciever からメッセージを受け取るのはなぜですか?

4

1 に答える 1

1

onDestroy() でアクティビティの登録を解除する必要があります。

protected void onDestroy() {
      super.onDestroy();
      MyReceiver.setReceiveMessage(null);
}
于 2013-09-05T09:25:18.120 に答える