Android跳转到系统Activity关闭并返回原Activity的解决办法

在Android开发时,有时因为需求,需要跳转到系统的一些页面,比如从UI中跳转到系统设置项、WIFI设置等,那要如何返回到原来的Activity中呢?

我们可以通过WindowManager来实现。原理可以简单的理解为在跳转到系统的Activity中后,在该Activity的上方添加一个按钮,然后对这个按钮添加事件。

先看看效果图

Android跳转到系统Activity关闭并返回原Activity的解决办法

实现代码如下

CallSystemActivity.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中添加权限,如下

<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在开启后一直存在,如果不这样处理,在点硬返回键时,才不会返回到系统的activity中。因为由A应用开启B应用的Activity,正常是无法从A中关闭B应用的Activity的,对于我们启动系统的Activity也是一样的道理。所以为了避免该问题,我们增加了flag,这样启动后的activity就不会保存到activity的堆栈中,自然在点返回时,也就不会返回到该activity中了。

Android跳转到系统Activity关闭并返回原Activity的解决办法

上一篇:Eclipse导入Android项目的方法


下一篇:第二十六章:自定义布局(五)