顯示具有 不顯示 標籤的文章。 顯示所有文章
顯示具有 不顯示 標籤的文章。 顯示所有文章

2014年9月2日 星期二

[Android] 如何開啟後隱藏Activty UI ?

在寫App的過程當中遇到兩個問題

1. 開機時自動啟動
2. 開啟後隱藏UI (用service在背景執行)

第一個問題可能之後幾篇會講到,不過大概就是android 會廣播一則開機訊息,你只要去抓那個訊

息就知道開機了,然後利用intent去啟動class...

第二個問題網路上比較少講到,這邊也沒有做到很完全


大概歸納為三個解決方案:

1.修改Theme 改為Theme.No_Display,但這經過實測,不行。

2.使用finish()函式,經過實測,可行。

使用方法:  在onCreate()裡面都做完事情以後新增一行 finish();

3.使用 moveTaskToBack()

public boolean moveTaskToBack (boolean nonRoot)

Added in API level 1
Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged.
Parameters
nonRootIf false then this only works if the activity is the root of a task; if true it will work for any activity in a task.
Returns
  • If the task was moved (or it was already at the back) true is returned, else false.

Google API上面的形容,會回傳值true就是已經移到背景,false則否。

有人會使用moveTaskToBack(true),也有人說也可以改成false,但經過實際測試,兩者皆可。

但API上寫是說true是無論如何都會work,false只有activity在task的root的時候才會作用 。


2014年2月13日 星期四

[C#/winform] 呼叫外部程式(傳入參數) + 不會顯示出來 + 結束強制關閉程式

本篇分成三部份來講

a)呼叫外部程式

//呼叫程式(啟動)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;

namespace Activate_exe
{
    static class Program
    {
        /// <summary>
        /// 應用程式的主要進入點。
        /// </summary>
        [STAThread]
        static void Main()
        {
            string target = 目標路徑;

            ProcessStartInfo pInfo = new ProcessStartInfo(target);
            pInfo.Arguments = "我是參數";
            using (Process p = new Process())
            {
                p.StartInfo = pInfo;
                p.Start();
            }

        }
    }
}


//主程式(被呼叫)
//在Program.cs內部
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace (被呼叫的程式,名稱自行更改)
{
    static class Program
    {
        /// <summary>
        /// 應用程式的主要進入點。
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            //Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (args.Length == 0)
            {
                Application.Run(new Form1());
            }
            else
            {
                Application.Run(new Form1(args[0].ToString()));
            }
        }
    }
}

呼叫程式十分簡單,也不需要用到winform,就直接寫在Program.cs即可

一開始記得要加入"using System.Diagnostics;"不然ProcessStartInfo 沒法用,

new一個ProcessStartInfo(要呼叫的程式路徑),然後 ProcessStartInfo.Argument內就是要傳的參數。

(參數遇到空格 疑似可以用\" (http://blogs.msdn.com/b/csharpfaq/archive/2004/03/12/88415.aspx)來解決)

傳多個參數,就是使用空格作為分隔 參數A空格B,在Form1値得注意的地方就是有兩個多載,當

沒有參數傳來的時後就用Form1(),有則Form1(args[])



-----------------------------------------------------------------------------------------------------------

被呼叫的程式要接參數,所以main(string[] args

主程式//Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Argument_IN_exe
{
    public partial class Form1 : Form
    {
        private string _args;
        public Form1()
        {
            InitializeComponent();

        }

        public Form1(string value)
        {
            InitializeComponent();
            if (!string.IsNullOrEmpty(value))
            {
                _args = value;
                MessageBox.Show(_args);
            }
        }
    }
}

b)不會顯示出來

要讓程式一開啟就不顯示出來,目前只知道這個方法(最小化+不顯是在系統列)

寫在Form load事件當中,一讀就縮最小,但是有個問題就是你無法停止它(所以才需要

c.強制關閉程式)

private void Form1_Load_1(object sender, EventArgs e)
{
       this.WindowState = FormWindowState.Minimized;
       this.ShowInTaskbar = false;
       this.Hide();
}

private void Form1_Shown(object sender, EventArgs e)
{
      this.Hide();
}


c)結束強制關閉程式

光有Close()是不夠的,可以試試看Ctrl+Alt+Del,會發現沒有全部關完。

加入Enviroment這行吧!

this.Close();
Environment.Exit(Environment.ExitCode);

參考: http://www.dotblogs.com.tw/atowngit/archive/2009/12/26/12681.aspx