在Android开发时,有时因为需求,需要跳转到系统的一些页面,比如从UI中跳转到系统设置项、WIFI设置等,那要如何返回到原来的Activity中呢?
我们可以通过WindowManager来实现。原理可以简单的理解为在跳转到系统的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);
- }