Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 跳轉系統 Activity 後關閉並返回原 Activity 的解法

編輯:關於 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" />

WindowManagerSp.java

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);
 }
}

IntentSp.java

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 座標是寫死的像素值,換一台解析度不同的機器位置就會跑掉,正式版本建議依螢幕尺寸動態計算。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved