編輯:關於 Android 程式設計
開發時常有這種需求:從自己的介面跳到系統頁面去,例如設定、Wi-Fi 設定等,設定完之後希望能回到原本的 Activity,而不是被系統的返回鍵帶到別的地方。
做法之一是透過 WindowManager:在跳轉之後,於系統 Activity 的上層疊一顆返回按鈕,並為這顆按鈕掛上點擊事件。因為 A 應用原則上無法關閉 B 應用的 Activity,所以我們反過來處理——把自己這邊的 Activity 先 finish(),再按返回鍵時重新把自己拉回前景。
先看效果:

package com.example.callsystemactivity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Init();
}
private void Init() {
Button callSystemSet_button=(Button)findViewById(R.id.CallSystemSet_button);
callSystemSet_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
WindowManagerSp windowManagerSp=new WindowManagerSp(MainActivity.this);
windowManagerSp.AddBackButton();
IntentSp.StartActivity(MainActivity.this, android.provider.Settings.ACTION_WIFI_IP_SETTINGS,false);
}
});
}
}
兩點補充:
1、activity_main.xml 中要有一顆 id 為 CallSystemSet_button 的按鈕。
2、懸浮層需要權限,記得在 AndroidManifest.xml 宣告:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> <uses-permission android:name="android.permission.SYSTEM_OVERLAY_WINDOW" />
package com.example.callsystemactivity;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
public class WindowManagerSp {
WindowManager _winManager = null;
Activity _activity = null;
Context _context = null;
public WindowManagerSp(Activity activity) {
if (activity == null) {
return;
}
_activity = activity;
_context = _activity.getBaseContext();
_winManager = (WindowManager) _activity.getApplicationContext()
.getSystemService(_activity.WINDOW_SERVICE);
}
public void AddBackButton() {
if (_winManager == null) {
return;
}
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.gravity = Gravity.TOP | Gravity.LEFT;
layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
| WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;
// 以下 width/height/x/y 未必最合適,需依實際頁面調整
layoutParams.width = 32;
layoutParams.height = 32;
layoutParams.x = 280;
layoutParams.y = 0;
final ImageView backButton = new ImageView(_context);
backButton.setBackgroundResource(R.drawable.back);// 請自行加入對應的背景圖
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
IntentSp.RestartActivity(_activity, false);// 見另一個類別
if (_winManager != null) {
_winManager.removeView(backButton);
}
_winManager = null;
}
});
_activity.finish();// 關閉 activity,返回時再重新開啟
_winManager.addView(backButton, layoutParams);
}
}
package com.kitsp.contentsp;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
public class IntentSp {
/**
*
* @param activity
* @param isSaveActivityToHistory
* true:save activity to history.System may back to the activity
* when other activity finish. false:no save.
*/
public static void RestartActivity(Activity activity, boolean isSaveActivityToHistory) {
if (activity == null) {
return;
}
Intent intent = new Intent();
String packageName = activity.getPackageName();
String className = activity.getLocalClassName();
String componentClassName = packageName + "." + className;
if (className != null && className.split(".").length > 0) {
componentClassName = className;
}
ComponentName componentName = new ComponentName(packageName, componentClassName);
intent.setComponent(componentName);
if (!isSaveActivityToHistory) {
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);// 加上此旗標後,activity 不會被保留
}
activity.startActivity(intent);
activity.finish();
return;
}
/**
*
* @param context
* @param action
* @param isSaveActivityToHistory
* true:save activity to history.System may back to the activity
* when other activity finish. false:no save.
*/
public static void StartActivity(Context context, String action, boolean isSaveActivityToHistory) {
if (context == null || action == null) {
return;
}
Intent intent = new Intent(action);
if (!isSaveActivityToHistory) {
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
}
context.startActivity(intent);
}
}
這裡的關鍵是 intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)。如果不加這個旗標,系統 Activity 一旦開啟就會留在堆疊裡,使用者按硬體返回鍵時反而會跑回系統頁面。由於由 A 應用去關閉 B 應用的 Activity 在架構上做不到,我們索性讓自己啟動的 Activity 不進入堆疊,返回時自然就不會回到那裡。
最後提醒一個實務細節:Android 6.0(API 23)之後,SYSTEM_ALERT_WINDOW 屬於特殊權限,光在 Manifest 宣告不夠,還必須在執行期透過 Settings.ACTION_MANAGE_OVERLAY_PERMISSION 引導使用者手動開啟,否則 addView 會失敗。此外懸浮按鈕的 x/y 座標是寫死的像素值,換一台解析度不同的機器位置就會跑掉,正式版本建議依螢幕尺寸動態計算。
Android 多執行緒實戰:從 Handler 到 AsyncTask 與執行緒池的取捨
先釐清一個常見誤解:handler.post(r) 並不會開啟新執行緒,它只是把 Runnable 的 run() 排進訊息佇列執行,並沒有呼叫 start(),所以 r
Android 禁止 ViewPager 滑動
最近項目中有個需求:禁止 ViewPager 的滑動事件。我們看 360 手機助手的界面,風格大概就是這樣——中間內容是不可滑動的。 下面寫個 demo 說明如何禁止
Android 如何獲取正在運行的應用程式並管理
想製作一個類似「清理加速」的小工具嗎?關鍵的第一步就是取得手機上正在運行的應用程式列表。本文以一個實際範例,示範如何透過 ActivityManager 讀取執行中的程式
Android 學生管理系統:Dialog、Adapter 與反射工廠的組合實踐
功能概述 這個練習項目實現了學生與教師的注冊登錄、選課、成績修改等基礎功能,主要目的是把 Dialog 的各種形態用熟。 界面如下: Adapter 部分