1. 程式人生 > >關於跳轉到系統介面,返回不了或者不儲存系統介面的辦法

關於跳轉到系統介面,返回不了或者不儲存系統介面的辦法

前面內容轉載至(支援原創)0:http://blog.csdn.net/xxdddail/article/details/19537145

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

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

先看看效果圖


實現程式碼如下

CallSystemActivity.Java

  1. package com.example.callsystemactivity;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.View;  
  5. import android.view.View.OnClickListener;  
  6. import android.widget.Button;  
  7. publicclass MainActivity extends Activity {  
  8.     @Override
  9.     protectedvoid onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.         Init();  
  13.     }  
  14.     privatevoid Init()  
  15.     {  
  16.         Button callSystemSet_button=(Button)findViewById(R.id.CallSystemSet_button);  
  17.         callSystemSet_button.setOnClickListener(new OnClickListener() {  
  18.             @Override
  19.             public
    void onClick(View v) {  
  20.                 // TODO Auto-generated method stub
  21.                 WindowManagerSp windowManagerSp=new WindowManagerSp(MainActivity.this);  
  22.                 windowManagerSp.AddBackButton();  
  23.                 IntentSp.StartActivity(MainActivity.this, android.provider.Settings.ACTION_WIFI_IP_SETTINGS,false);  
  24.             }  
  25.         });  
  26.     }  
  27. }  

注:

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

  1. package com.example.callsystemactivity;  
  2. import android.app.Activity;  
  3. import android.content.Context;  
  4. import android.view.Gravity;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.view.WindowManager;  
  8. import android.widget.ImageView;  
  9. publicclass WindowManagerSp {  
  10.     WindowManager _winManager = null;  
  11.     Activity _activity = null;  
  12.     Context _context = null;  
  13.     public WindowManagerSp(Activity activity) {  
  14.         if (activity == null) {  
  15.             return;  
  16.         }  
  17.         _activity = activity;  
  18.         _context = _activity.getBaseContext();  
  19.         _winManager = (WindowManager) _activity.getApplicationContext()  
  20.                 .getSystemService(_activity.WINDOW_SERVICE);  
  21.     }  
  22.     publicvoid AddBackButton() {  
  23.         if (_winManager == null) {  
  24.             return;  
  25.         }  
  26.         WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();  
  27.         layoutParams.gravity = Gravity.TOP | Gravity.LEFT;  
  28.         layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL  
  29.                 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;  
  30.         layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT  
  31.                 | WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;  
  32.         // 以下width/height/x/y不一定是最合適的,需根據實現的頁面加以調整
  33.         layoutParams.width = 32;  
  34.         layoutParams.height = 32;  
  35.         layoutParams.x = 280;  
  36.         layoutParams.y = 0;  
  37.         final ImageView backButton = new ImageView(_context);     
  38.         backButton.setBackgroundResource(R.drawable.back);// 請自行新增相應的背景圖片
  39.         backButton.setOnClickListener(new OnClickListener() {  
  40.             @Override
  41.             publicvoid onClick(View v) {  
  42.                 IntentSp.RestartActivity(_activity, false);// 在另一個類中
  43.                 if (_winManager != null) {  
  44.                     _winManager.removeView(backButton);  
  45.                 }  
  46.                 _winManager = null;  
  47.             }  
  48.         });  
  49.         _activity.finish();// 關閉activity,在返回時再次開啟
  50.         _winManager.addView(backButton, layoutParams);  
  51.     }  
  52. }  


IntentSp.java

  1. package com.kitsp.contentsp;  
  2. import android.app.Activity;  
  3. import android.content.ComponentName;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.net.Uri;  
  7. publicclass IntentSp {  
  8.     /** 
  9.      *  
  10.      * @param activity 
  11.      * @param isSaveActivityToHistory 
  12.      *            true:save activity to history.System may back to the activity 
  13.      *            when other activity finish. false:no save. 
  14.      */
  15.     publicstaticvoid RestartActivity(Activity activity,  
  16.             boolean isSaveActivityToHistory) {  
  17.         if (activity == null) {  
  18.             return;  
  19.         }  
  20.         Intent intent = new Intent();  
  21.         String packageName = activity.getPackageName();  
  22.         String className = activity.getLocalClassName();  
  23.         String componentClassName = packageName + "." + className;  
  24.         if (className != null && className.split(".").length > 0) {  
  25.             componentClassName = className;  
  26.         }  
  27.         ComponentName componentName = new ComponentName(packageName,  
  28.                 componentClassName);  
  29.         intent.setComponent(componentName);  
  30.         if (!isSaveActivityToHistory) {  
  31.             intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);//新增該標誌後,讓activity不儲存
  32.         }  
  33.         activity.startActivity(intent);  
  34.         activity.finish();  
  35.         return;  
  36.     }  
  37. /** 
  38.      *  
  39.      * @param context 
  40.      * @param action 
  41.      * @param isSaveActivityToHistory 
  42.      *            true:save activity to history.System may back to the activity 
  43.      *            when other activity finish. false:no save. 
  44.      */
  45.     publicstaticvoid StartActivity(Context context, String action,  
  46.             boolean isSaveActivityToHistory) {  
  47.         if (context == null || action == null) {  
  48.             return;  
  49.         }  
  50.         Intent intent = new Intent(action);  
  51.         if (!isSaveActivityToHistory) {  
  52.             intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);  
  53.         }  
  54.         context.startActivity(intent);  
  55.     }  
  56. }  
注:

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)