編輯:關於Android編程
一.需求分析:
1.通過按Android平板(或手機)的物理按鍵(如音量鍵)實現Activity界面的跳轉;以前基本是自己寫控件進行跳轉啊!
2.按音量減鍵跳轉:界面1到界面2,2到3,3到4,4到5,5到1;按音量加鍵跳轉:1到5,5到4,4到3,3到2,2到1。
五個界面顏色1:白色,2:黑色,3:紅,4:綠,5:藍。 目的是用來進行屏的測試。
二、直接上代碼
1.五個類的代碼
Launcher1:
package com.zhc.launcher; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; public class Launcher1 extends Activity implements KeyEvent.Callback { private static final String TAG = "Launcher"; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 1.隱藏標題欄 requestWindowFeature(Window.FEATURE_NO_TITLE); // 2.隱藏狀態欄 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 3.隱藏導航欄和狀態欄 View decorView = getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); setContentView(R.layout.activity_one); } // 4. 攔截系統熱鍵 @Override public boolean dispatchKeyEvent(KeyEvent event) { int key = event.getKeyCode();//獲取物理按鍵的key類型:比如音量鍵,power鍵等 int key1 = event.getAction();//獲取某一物理按鍵的對應的事件類型;比如音量鍵的按下(down)事件,音量鍵的松開(up)事件 if (key == KeyEvent.KEYCODE_DPAD_LEFT || key == KeyEvent.KEYCODE_VOLUME_UP) {//按下的是安卓物理左鍵或者是音量鍵上鍵 if (key1 == KeyEvent.ACTION_UP) {//音量鍵上鍵的up事件 Log.i(TAG, "1"); Intent intentright = new Intent(Launcher1.this, Launcher5.class); startActivity(intentright); return true; } } else if (key == KeyEvent.KEYCODE_DPAD_RIGHT || key == KeyEvent.KEYCODE_VOLUME_DOWN) {//按下的是安卓物理右鍵或者是音量鍵下鍵 if (key1 == KeyEvent.ACTION_UP) {//音量鍵下鍵的up事件 Log.i(TAG, "2"); Intent intentright = new Intent(Launcher1.this, Launcher2.class); startActivity(intentright); return true; } } return super.dispatchKeyEvent(event); } }Launcher2:
package com.zhc.launcher; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; public class Launcher2 extends Activity { private static final String TAG = "Launcher2"; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 1.隱藏標題欄 requestWindowFeature(Window.FEATURE_NO_TITLE); // 2.隱藏狀態欄 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 3.隱藏導航欄和狀態欄 View decorView = getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); setContentView(R.layout.activity_two); } // 攔截系統熱鍵 @Override public boolean dispatchKeyEvent(KeyEvent event) { int key = event.getKeyCode();// 獲取物理按鍵的key類型:比如音量鍵,power鍵等 int key1 = event.getAction();// 獲取某一物理按鍵的對應的事件類型;比如音量鍵的按下(down)事件,音量鍵的松開(up)事件 if (key == KeyEvent.KEYCODE_DPAD_LEFT || key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左鍵或者是音量鍵上鍵 if (key1 == KeyEvent.ACTION_UP) {// 音量鍵上鍵的up事件 Log.i(TAG, "1"); Intent intentright = new Intent(Launcher2.this, Launcher1.class); startActivity(intentright); return true; } } else if (key == KeyEvent.KEYCODE_DPAD_RIGHT || key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右鍵或者是音量鍵下鍵 if (key1 == KeyEvent.ACTION_UP) {// 音量鍵下鍵的up事件 Log.i(TAG, "2"); Intent intentright = new Intent(Launcher2.this, Launcher3.class); startActivity(intentright); return true; } } return super.dispatchKeyEvent(event); } }Launcher3:
package com.zhc.launcher; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; public class Launcher3 extends Activity { private static final String TAG = "Launcher3"; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 1.隱藏標題欄 requestWindowFeature(Window.FEATURE_NO_TITLE); // 2.隱藏狀態欄 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 3.隱藏導航欄和狀態欄 View decorView = getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); setContentView(R.layout.activity_three); } // 攔截系統熱鍵 @Override public boolean dispatchKeyEvent(KeyEvent event) { int key = event.getKeyCode();// 獲取物理按鍵的key類型:比如音量鍵,power鍵等 int key1 = event.getAction();// 獲取某一物理按鍵的對應的事件類型;比如音量鍵的按下(down)事件,音量鍵的松開(up)事件 if (key == KeyEvent.KEYCODE_DPAD_LEFT || key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左鍵或者是音量鍵上鍵 if (key1 == KeyEvent.ACTION_UP) {// 音量鍵上鍵的up事件 Log.i(TAG, "1"); Intent intentright = new Intent(Launcher3.this, Launcher2.class); startActivity(intentright); return true; } } else if (key == KeyEvent.KEYCODE_DPAD_RIGHT || key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右鍵或者是音量鍵下鍵 if (key1 == KeyEvent.ACTION_UP) {// 音量鍵下鍵的up事件 Log.i(TAG, "2"); Intent intentright = new Intent(Launcher3.this, Launcher4.class); startActivity(intentright); return true; } } return super.dispatchKeyEvent(event); } }
Launcher4:
package com.zhc.launcher; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; public class Launcher4 extends Activity { private static final String TAG = "Launcher4"; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 1.隱藏標題欄 requestWindowFeature(Window.FEATURE_NO_TITLE); // 2.隱藏狀態欄 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 3.隱藏導航欄和狀態欄 View decorView = getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); setContentView(R.layout.activity_four); } // 攔截系統熱鍵 @Override public boolean dispatchKeyEvent(KeyEvent event) { int key = event.getKeyCode();// 獲取物理按鍵的key類型:比如音量鍵,power鍵等 int key1 = event.getAction();// 獲取某一物理按鍵的對應的事件類型;比如音量鍵的按下(down)事件,音量鍵的松開(up)事件 if (key == KeyEvent.KEYCODE_DPAD_LEFT || key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左鍵或者是音量鍵上鍵 if (key1 == KeyEvent.ACTION_UP) {// 音量鍵上鍵的up事件 Log.i(TAG, "1"); Intent intentright = new Intent(Launcher4.this, Launcher3.class); startActivity(intentright); return true; } } else if (key == KeyEvent.KEYCODE_DPAD_RIGHT || key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右鍵或者是音量鍵下鍵 if (key1 == KeyEvent.ACTION_UP) {// 音量鍵下鍵的up事件 Log.i(TAG, "2"); Intent intentright = new Intent(Launcher4.this, Launcher5.class); startActivity(intentright); return true; } } return super.dispatchKeyEvent(event); } }
Launcher5:
package com.zhc.launcher; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.view.WindowManager; public class Launcher5 extends Activity { private static final String TAG = "Launcher5"; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 1.隱藏標題欄 requestWindowFeature(Window.FEATURE_NO_TITLE); // 2.隱藏狀態欄 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); // 3.隱藏導航欄和狀態欄 View decorView = getWindow().getDecorView(); int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_FULLSCREEN; decorView.setSystemUiVisibility(uiOptions); setContentView(R.layout.activity_five); } // 攔截系統熱鍵 @Override public boolean dispatchKeyEvent(KeyEvent event) { int key = event.getKeyCode();// 獲取物理按鍵的key類型:比如音量鍵,power鍵等 int key1 = event.getAction();// 獲取某一物理按鍵的對應的事件類型;比如音量鍵的按下(down)事件,音量鍵的松開(up)事件 if (key == KeyEvent.KEYCODE_DPAD_LEFT || key == KeyEvent.KEYCODE_VOLUME_UP) {// 按下的是安卓物理左鍵或者是音量鍵上鍵 if (key1 == KeyEvent.ACTION_UP) {// 音量鍵上鍵的up事件 Log.i(TAG, "1"); Intent intentright = new Intent(Launcher5.this, Launcher4.class); startActivity(intentright); return true; } } else if (key == KeyEvent.KEYCODE_DPAD_RIGHT || key == KeyEvent.KEYCODE_VOLUME_DOWN) {// 按下的是安卓物理右鍵或者是音量鍵下鍵 if (key1 == KeyEvent.ACTION_UP) {// 音量鍵下鍵的up事件 Log.i(TAG, "2"); Intent intentright = new Intent(Launcher5.this, Launcher1.class); startActivity(intentright); return true; } } return super.dispatchKeyEvent(event); } }
3.清單文件
現在很多的應用基本都會集成分享這個功能,該功能包括系統分享(比如郵件,短信)和第三方分享(比如QQ和微信)。其中有些公司會選擇使用第三方的庫來簡化這些操作,加快開發,用的
我們通過使用DexClassLoader能夠將classes.dex中的類動態的加載進入當前進程。當然,也就可以預先定一些代理的接口完成四大組件的功能。整體功
摘自網上的android生命周期圖: cocos2dx-2.X前後台切換分析,基於android平台:1、從後台進入前台項目的activity一般繼承自Coco
本文實例講述了Android編程之Animation動畫用法。分享給大家供大家參考,具體如下:Animations一、Animations介紹Animations是一個實