Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android跳轉到系統Activity關閉並返回原Activity的解決辦法

Android跳轉到系統Activity關閉並返回原Activity的解決辦法

編輯:關於Android編程

在Android開發時,有時因為需求,需要跳轉到系統的一些頁面,比如從UI中跳轉到系統設置項、WIFI設置等,那要如何返回到原來的Activity中呢?

我們可以通過WindowManager來實現。原理可以簡單的理解為在跳轉到系統的Activity中後,在該Activity的上方添加一個按鈕,然後對這個按鈕添加事件。

先看看效果圖

\

實現代碼如下<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+Q2FsbFN5c3RlbUFjdGl2aXR5LmphdmE8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">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中添加一個按鈕callSystemSet_button

2、WindowManager需要相應的權限,所以需要在AndroidManifest.xml中添加權限,如下




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在開啟後一直存在,如果不這樣處理,在點硬返回鍵時,才不會返回到系統的activity中。因為由A應用開啟B應用的Activity,正常是無法從A中關閉B應用的Activity的,對於我們啟動系統的Activity也是一樣的道理。所以為了避免該問題,我們增加了flag,這樣啟動後的activity就不會保存到activity的堆棧中,自然在點返回時,也就不會返回到該activity中了。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved