例子一则

在main的onStart中注册一个BroadCastReceiver用来监听ACTION_CHANGING事件,在事件发生之后,修改textview的text。在onStop之中卸载改该listener,这样当第二个activity是透明的风格的时候main可以通过listener改变样式。
package org.cxz.lifeCycle;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

	public static final String ACTION_CHANGING = "org.cxz.lifeCycle.ChangeBackGround";

	private static final int MENU_OPTION_NEXT = 1;

	private ScreenChangingReceiver receiver = new ScreenChangingReceiver();

	private TextView tv = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_activity);
		tv = (TextView) findViewById(R.id.TextView01);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_OPTION_NEXT, 0, R.string.next_activity);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		super.onMenuItemSelected(featureId, item);
		switch (item.getItemId()) {
		case MENU_OPTION_NEXT:
			fireTransparentActivity();
		}
		return true;
	}

	private void fireTransparentActivity() {
		Intent i = new Intent(this, TransparentActivity.class);
		startActivity(i);
	}

	@Override
	protected void onStart() {
		super.onStart();
		IntentFilter filter = new IntentFilter(ACTION_CHANGING);
		registerReceiver(receiver, filter);
	}

	@Override
	protected void onStop() {
		super.onStop();
		unregisterReceiver(receiver);
	}

	private class ScreenChangingReceiver extends BroadcastReceiver {

		private static final String NEW_TEXT = "new Text";

		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					tv.setText(NEW_TEXT);
				}

			});
		}

	}

}

第二个透明的activity但点击menu button的时候改变上一个activity的外观。
package org.cxz.lifeCycle;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class TransparentActivity extends Activity {
	private static final int MENU_OPTION_CHANGE = 1; 
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.transparent_activity);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu);
		menu.add(0, MENU_OPTION_CHANGE, 0, R.string.change_bg);
		return true;
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		super.onMenuItemSelected(featureId, item);
		switch (item.getItemId()) {
		case MENU_OPTION_CHANGE:
			changeMainBackGround();
		}
		return true;
	}

	private void changeMainBackGround() {
		Intent i = new Intent(MainActivity.ACTION_CHANGING);
		// startActivity(i);
		// not start an activity
		sendBroadcast(i);
	}
}

你可能感兴趣的:(android,OS)