关于跳转到系统界面,返回不了或者不保存系统界面的办法

前面内容转载至(支持原创)0:http://blog.csdn.net/xxdddail/article/details/19537145


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

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

先看看效果图


实现代码如下

CallSystemActivity.Java

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.callsystemactivity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8.   
  9. public class MainActivity extends Activity {  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_main);  
  15.         Init();  
  16.     }  
  17.       
  18.     private void Init()  
  19.     {  
  20.         Button callSystemSet_button=(Button)findViewById(R.id.CallSystemSet_button);  
  21.       
  22.         callSystemSet_button.setOnClickListener(new OnClickListener() {  
  23.               
  24.             @Override  
  25.             public void onClick(View v) {  
  26.                 // TODO Auto-generated method stub  
  27.                 WindowManagerSp windowManagerSp=new WindowManagerSp(MainActivity.this);  
  28.                 windowManagerSp.AddBackButton();  
  29.                 IntentSp.StartActivity(MainActivity.this, android.provider.Settings.ACTION_WIFI_IP_SETTINGS,false);  
  30.             }  
  31.         });  
  32.     }  
  33.   
  34. }  

注:

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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.example.callsystemactivity;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.view.Gravity;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.view.WindowManager;  
  9. import android.widget.ImageView;  
  10.   
  11. public class WindowManagerSp {  
  12.   
  13.     WindowManager _winManager = null;  
  14.     Activity _activity = null;  
  15.     Context _context = null;  
  16.   
  17.     public WindowManagerSp(Activity activity) {  
  18.         if (activity == null) {  
  19.             return;  
  20.         }  
  21.         _activity = activity;  
  22.         _context = _activity.getBaseContext();  
  23.   
  24.         _winManager = (WindowManager) _activity.getApplicationContext()  
  25.                 .getSystemService(_activity.WINDOW_SERVICE);  
  26.     }  
  27.   
  28.     public void AddBackButton() {  
  29.   
  30.         if (_winManager == null) {  
  31.             return;  
  32.         }  
  33.         WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();  
  34.   
  35.         layoutParams.gravity = Gravity.TOP | Gravity.LEFT;  
  36.   
  37.         layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL  
  38.                 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;  
  39.   
  40.         layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT  
  41.                 | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;  
  42.   
  43.         // 以下width/height/x/y不一定是最合适的,需根据实现的页面加以调整  
  44.         layoutParams.width = 32;  
  45.         layoutParams.height = 32;  
  46.   
  47.         layoutParams.x = 280;  
  48.         layoutParams.y = 0;  
  49.   
  50.         final ImageView backButton = new ImageView(_context);     
  51.         backButton.setBackgroundResource(R.drawable.back);// 请自行添加相应的背景图片  
  52.   
  53.         backButton.setOnClickListener(new OnClickListener() {  
  54.   
  55.             @Override  
  56.             public void onClick(View v) {  
  57.   
  58.                 IntentSp.RestartActivity(_activity, false);// 在另一个类中  
  59.                 if (_winManager != null) {  
  60.                     _winManager.removeView(backButton);  
  61.                 }  
  62.                 _winManager = null;  
  63.             }  
  64.         });  
  65.   
  66.         _activity.finish();// 关闭activity,在返回时再次开启  
  67.         _winManager.addView(backButton, layoutParams);  
  68.     }  
  69. }  


IntentSp.java

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. package com.kitsp.contentsp;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Context;  
  6. import android.content.Intent;  
  7. import android.net.Uri;  
  8.   
  9. public class IntentSp {  
  10.   
  11.     /** 
  12.      *  
  13.      * @param activity 
  14.      * @param isSaveActivityToHistory 
  15.      *            true:save activity to history.System may back to the activity 
  16.      *            when other activity finish. false:no save. 
  17.      */  
  18.     public static void RestartActivity(Activity activity,  
  19.             boolean isSaveActivityToHistory) {  
  20.         if (activity == null) {  
  21.             return;  
  22.         }  
  23.         Intent intent = new Intent();  
  24.         String packageName = activity.getPackageName();  
  25.         String className = activity.getLocalClassName();  
  26.         String componentClassName = packageName + "." + className;  
  27.         if (className != null && className.split(".").length > 0) {  
  28.             componentClassName = className;  
  29.         }  
  30.         ComponentName componentName = new ComponentName(packageName,  
  31.                 componentClassName);  
  32.   
  33.         intent.setComponent(componentName);  
  34.         if (!isSaveActivityToHistory) {  
  35.             intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);//添加该标志后,让activity不保存  
  36.         }  
  37.         activity.startActivity(intent);  
  38.         activity.finish();  
  39.         return;  
  40.     }  
  41.   
  42. /** 
  43.      *  
  44.      * @param context 
  45.      * @param action 
  46.      * @param isSaveActivityToHistory 
  47.      *            true:save activity to history.System may back to the activity 
  48.      *            when other activity finish. false:no save. 
  49.      */  
  50.     public static void StartActivity(Context context, String action,  
  51.             boolean isSaveActivityToHistory) {  
  52.         if (context == null || action == null) {  
  53.             return;  
  54.         }  
  55.   
  56.   
  57.         Intent intent = new Intent(action);  
  58.         if (!isSaveActivityToHistory) {  
  59.             intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);  
  60.         }  
  61.         context.startActivity(intent);  
  62.     }  
  63. }  
  64.       
注:

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




所以:如果不想保存系统界面,跳转时,记得加

intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)



另外:在跳转到系统界面时,如:亮度设置,还有弹出系统的dialog时,需要权限,以及wifi设置等等,跳转后,可以添加两个按钮,来手动的返回.添加后的




//wifi未连接
Intent wifiSettingsIntent = new Intent("android.settings.WIFI_SETTINGS");
wifiSettingsIntent.putExtra("extra_prefs_show_button_bar", true);
wifiSettingsIntent.putExtra("wifi_enable_next_on_connect", true);
startActivity(wifiSettingsIntent);
添加上面的内容即可


intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)


settings源码(Settings\src\com\android\settings\wifi\WifiSettings.java和WifiPickerActivity.java)找到了如下信息:
private static final String EXTRA_PREFS_SHOW_BUTTON_BAR = "extra_prefs_show_button_bar";//是否显示button bar,传递值为true的话是显示
private static final String EXTRA_PREFS_SET_NEXT_TEXT = "extra_prefs_set_next_text";//自定义按钮的名字,不传递的话,默认为下一步
private static final String EXTRA_PREFS_SET_BACK_TEXT = "extra_prefs_set_back_text";//自定义按钮的名字,不传递的话,默认为上一步
private static final String EXTRA_ENABLE_NEXT_ON_CONNECT = "wifi_enable_next_on_connect";//是否打开网络连接检测功能(如果连上wifi,则下一步按钮可被点击)
Intent intent = new Intent();
intent.setAction("android.net.wifi.PICK_WIFI_NETWORK");
intent.putExtra("extra_prefs_show_button_bar", true);
//intent.putExtra("extra_prefs_set_next_text", "完成"); //设置下一步按钮得文字.默认为下一步
//intent.putExtra("extra_prefs_set_back_text", "返回");//设置上一步按钮得文字,默认为上一步
intent.putExtra("wifi_enable_next_on_connect", true);
startActivity(intent);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值