package zhangphil.home; 
 
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.widget.Toast; 
 
public class MainActivity extends Activity { 
 
	private MyReceiver receiver; 
 
	@Override 
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState); 
		// setContentView(R.layout.activity_main); 
 
		receiver = new MyReceiver(); 
		IntentFilter homeFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); 
 
		registerReceiver(receiver, homeFilter); 
	} 
 
	@Override 
	public void onDestroy() { 
		unregisterReceiver(receiver); 
		super.onDestroy(); 
	} 
 
	private class MyReceiver extends BroadcastReceiver { 
 
		private final String SYSTEM_DIALOG_REASON_KEY = "reason"; 
		private final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey"; 
		private final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps"; 
 
		@Override 
		public void onReceive(Context context, Intent intent) { 
			String action = intent.getAction(); 
			if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { 
				String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY); 
 
				if (reason == null) 
					return; 
 
				// Home键 
				if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) { 
					Toast.makeText(getApplicationContext(), "按了Home键", Toast.LENGTH_SHORT).show(); 
				} 
 
				// 最近任务列表键 
				if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) { 
					Toast.makeText(getApplicationContext(), "按了最近任务列表", Toast.LENGTH_SHORT).show(); 
				} 
			} 
		} 
	} 
} 

发布评论

分享到:

IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!

Android加载Gif图片的一般方法:Movie实现详解
你是第一个吃螃蟹的人
发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。