Android intent and activity經典程序 – Android移動開發技術文章_手機開發 Android移動開發教學課程

 #massage_box {  
    position: absolute;  
    left: expression((body.clientwidth-350)/ 2 );  
    top: expression((body.clientheight-200)/ 2 );  
    width: 350px;  
    height: 200px;  
   
    z-index: 2;  
    visibility: hidden  
}  
 
#mask {  
    position: absolute;  
    top: 0;  
    left: 0;  
    width: expression(body.scrollwidth);  
    height: expression(body.scrollheight);  
    background: #d3ddea;  
    filter: alpha(opacity =50);  
    z-index: 1;  
    visibility: hidden  

Java代碼 
//數據寫入intentintent openwelcomeactivityintent=new intent();bundle mybundelforname=new bundle();mybundelforname.putstring("key_name",inname.gettext().tostring());mybundelforname.putstring("key_age",inage.gettext().tostring());openwelcomeactivityintent.putextras(mybundelforname);openwelcomeactivityintent.setclass(androidbundel.this, welcome.class);startactivity(openwelcomeactivityintent);//從intent 中獲取數據bundle mybundelforgetname=this.getintent().getextras();string name=mybundelforgetname.getstring("key_name"); 


Java代碼 
package zyf.android.study;import ……import android.content.intent;import android.net.uri;import android.view.view.onclicklistener;public class androidstudy_two extends activity {@overridepublic void oncreate(bundle savedinstancestate) {super.oncreate(savedinstancestate);setcontentview(r.layout.main);final edittext inname = (edittext) findviewbyid(r.id.name);final textview result = (textview) findviewbyid(r.id.result);button button_start_browser = (button) findviewbyid(r.id.submit_tonet);button button_login=(button) findviewbyid(r.id.show_login);button button_showloginname=(button)findviewbyid(r.id.submit_toshowloingname);button_start_browser.setonclicklistener(new onclicklistener(){@overridepublic void onclick(view v) {uri myuri = uri.parse("http://www.flashwing.net");intent openbrowserintent = new intent(intent.action_view,myuri);startactivity(openbrowserintent);}});button_login.setonclicklistener(new onclicklistener(){@overridepublic void onclick(view v) {intent openwelcomeactivityintent=new intent();openwelcomeactivityintent.setclass(androidstudy_two.this,welcome.class);startactivity(openwelcomeactivityintent);}});button_showloginname.setonclicklistener(new onclicklistener(){@overridepublic void onclick(view v) {result.settext(inname.gettext()+"歡迎您進入");}});}} 

 

Java代碼 
使用bundle在activity間傳遞數據2從源請求activity 中通過一個intent 把一個服務請求傳到目標activity 中//數據寫入intentintent openwelcomeactivityintent=new intent();bundle mybundelforname=new bundle();mybundelforname.putstring("key_name",inname.gettext().tostring());mybundelforname.putstring("key_age",inage.gettext().tostring());openwelcomeactivityintent.putextras(mybundelforname);openwelcomeactivityintent.setclass(androidbundel.this, welcome.class);startactivity(openwelcomeactivityintent);//從intent 中獲取數據bundle mybundelforgetname=this.getintent().getextras();string name=mybundelforgetname.getstring("key_name");mytextview_showname.settext("歡迎您進入:"+name);private intent tonextintent;//intent 成員聲明tonextintent=new intent();//intent 定義tonextintent.setclass(twoactivityme3.this, secondactivity3.class);//設定開啟的下一個activitystartactivityforresult(tonextintent, request_ask);//開啟intent 時候,把請求碼同時傳遞———————————–android 編程基礎3在源請求activity 中等待intent 返回應答結果,通過重載onactivityresult()方法☻ 第一個參數是你開啟請求intent時的對應請求碼,可以自己定義。☻ 第二個參數是目標activity返回的驗證結果碼☻ 第三個參數是目標activity返回的intent目標activity 中發送請求結果代碼,連同源activity 請求的數據一同綁定到bundle中通過intent 傳回源請求activity 中log與ddms(查看log等信息)@overrideprotected void onactivityresult(int requestcode,int resultcode, intent data) {// todo auto-generated method stubsuper.onactivityresult(requestcode, resultcode, data);if(requestcode==request_ask){if(resultcode==result_canceled){settitle("cancel****");}else if(resultcode==result_ok){showbundle=data.getextras();//從返回的intent中獲得bundlename=showbundle.getstring("myname");//從bundle中獲得相應數據text.settext("the name get from the second layout:\n"+name);}}}backintent=new intent();stringbundle=new bundle();stringbundle.putstring("myname", name);backintent.putextras(stringbundle);setresult(result_ok, backintent);//返回activity結果碼finish(); 


Java代碼 
1、打開cmd輸入netca。 2、選擇【本地net服務名配置】→下一步。 3、選擇【添加】→下一步。 4、在【服務名】中輸入你想要連接的服務器上的oracle服務的名字→下一步。 5、選擇【tcp】→下一步。 6、在【主機名】中輸入你想要連接的服務器ip,端口號不變→下一步。 7、選擇【進行測試】→下一步。(當然你也可以選擇不測試) ※1測試如果不通過,點擊【更改登錄】輸入一個肯定能登入的【用戶名】和【口令】→確定,出現【正在連接…測試成功。】表明測試成功。 ※2測試不通過的話,點【上一步】確認你寫的【服務名】和【主機名】,確認無誤的情況下,確認你要連接的服務器oracle監聽服務已經打開。 8、在【net服務名中】輸入你想要使用的本地的服務名→下一步。 ※就是【sqlplus user/passwd @本地服務名】。 9、選擇【否】→下一步。 10、出現【net服務配置完畢】字樣→下一步。 11、點擊【完成】完成服務配置。 12、在上面服務配置完成以後,在cmd中輸入如下命令即可遠程登錄oracle服務器: sqlplus user_name/password@本地服務名 一定記住,上面的本地服務名是你第8步寫的服務名。 

 

Java代碼 
3. jboss 的安全設置3.1 jmx-console 登錄的用戶名和密碼設置 默認情況訪問 http://localhost:8080/jmx-console 就可以瀏覽jboss的部署管理的一些信息,不需要輸入用戶名和密碼,使用起來有點安全隱患。下面我們針對此問題對jboss進行配置,使得訪問jmx-console也必須要知道用戶名和密碼才可進去訪問。步驟如下:3.1.1 修改jboss-web.xml文件打開/server/default/deploy/jmx-console.war/web-inf/jboss-web.xml文件,去掉<security-domain>java:/jaas/jmx-console</security-domain>的註釋。修改後的該文件內容為:<jboss-web>   <!– uncomment the security-domain to enable security. you will      need to edit the htmladaptor login configuration to setup the      login modules used to authentication users.–>      <security-domain>java:/jaas/jmx-console</security-domain></jboss-web>3.1.2 修改web.xml文件與3.1.1中的jboss-web.xml同級目錄下的web.xml文件,查找到<security-constraint/>節點,去掉它的註釋,修改後該部分內容為:<!– a security constraint that restricts access to the html jmx console   to users with the role jbossadmin. edit the roles to what you want and   uncomment the web-inf/jboss-web.xml/security-domain element to enable   secured access to the html jmx console.–>   <security-constraint>     <web-resource-collection>       <web-resource-name>htmladaptor</web-resource-name>       <description>an example security config that only allows users with the         role jbossadmin to access the html jmx console web application       </description>       <url-pattern>/*</url-pattern>       <http-method>get</http-method>       <http-method>post</http-method>     </web-resource-collection>     <auth-constraint>       <role-name>jbossadmin</role-name>     </auth-constraint>   </security-constraint>   在此處可以看出,為登錄配置瞭角色jbossadmin。3.1.3 修改login-config.xml文件在第一步中的jmx-console安全域和第二步中的運行角色jbossadmin都是在login-config.xml中配置,我們在jboss安裝目錄/server/default/config下找到它。查找名字為:jmx-console的application-policy:<application-policy name = "jmx-console">       <authentication>          <login-module code="org.jboss.security.auth.spi.usersrolesloginmodule"             flag = "required">           <module-option name="usersproperties">props/jmx-console-users.properties</module-option>           <module-option name="rolesproperties">props/jmx-console-roles.properties</module-option>          </login-module>       </authentication>    </application-policy>在此處可以看出,登錄的角色、用戶等的信息分別在props目錄下的jmx-console-roles.properties和jmx-console-users.properties文件中設置,分別打開這兩個文件。其中jmx-console-users.properties文件的內容如下:# a sample users.properties file for use with the usersrolesloginmoduleadmin=admin該文件定義的格式為:用戶名=密碼,在該文件中,默認定義瞭一個用戶名為admin,密碼也為admin的用戶,讀者可將其改成所需的用戶名和密碼。jmx-console-roles.properties的內容如下:# a sample roles.properties file for use with the usersrolesloginmoduleadmin=jbossadmin, httpinvoker該文件定義的格式為:用戶名=角色,多個角色以“,”隔開,該文件默認為admin用戶定義瞭jbossadmin和httpinvoker這兩個角色。配置完成後讀者可以通過訪問: http://localhost:8088/jmx-console/ ,輸入jmx-console-roles.properties文件中定義的用戶名和密碼,訪問jmx-console的頁面。如圖: 


Java代碼 
package action;import java.awt.image;import java.awt.toolkit;import java.awt.image.bufferedimage;import java.awt.image.cropimagefilter;import java.awt.image.filteredimagesource;import java.awt.image.imagefilter;import java.awt.image.imageproducer;import java.io.bufferedinputstream;import java.io.bufferedoutputstream;import java.io.file;import java.io.fileinputstream;import java.io.fileoutputstream;import java.io.ioexception;import java.io.inputstream;import java.io.outputstream;import java.util.arraylist;import java.util.date;import java.util.list;import java.util.map;import java.util.random;import javax.imageio.imageio;import javax.servlet.http.httpservletrequest;import org.apache.struts2.servletactioncontext;import po.blog;import po.login;import po.photo;import po.userinfo;import service.qqrservice;import util.imagehepler;import com.opensymphony.xwork2.actioncontext;import com.sun.image.codec.jpeg.jpegcodec;import com.sun.image.codec.jpeg.jpegimageencoder;public class kindeditoraction extends baseaction {  private static final long serialversionuid = 1l;    private qqrservice qqrservice;  // 封裝參數 private file imgfile;   private string imgfilefilename; private string imgfilecontenttype;  private string filedir = "/blogimg";    private string imgtitle = "";   private string align;   private int imgwidth = 0;   private int imgheight = 0;  // 上傳kindeditor 圖片及壓縮   @suppresswarnings("deprecation")    public string uploadimg() throws exception {        // todo auto-generated method stub      // 隻能傳圖片        try {           if (this.imgfile != null) {             file dir = new file(servletactioncontext.getrequest()                       .getrealpath(filedir));             if (!dir.exists())                  dir.mkdir();                integer login_id = (integer) actioncontext.getcontext()                     .getsession().get("login_id");              date dt = new date();               file dst = new file(dir.getabsolutepath() + file.separator                      + login_id + new date().gettime() + ".jpg");                this.resize_image(this.imgfile, dst);               string id = "context";              string url = servletactioncontext.getrequest().getcontextpath()                     + "/" + this.filedir + "/" + dst.getname();             string border = "0";                string result = "<script type='text/javascript'>";                result += "parent.ke.plugin['image'].insert('" + id + "','"                     + url + "','" + imgtitle + "','" + imgheight + "','"                        + imgheight + "','" + border + "','" + align + "');   ";                result += "</script>";                servletactioncontext.getresponse().getwriter().write(result);           } else {                return null;            }       } catch (runtimeexception e) {          // todo auto-generated catch block          e.printstacktrace();        }       return null;    }   // 自己封裝的一個把源文件對象復制成目標文件對象   public boolean copy(file src, file dst) {       inputstream in = null;      outputstream out = null;        try {           in = new bufferedinputstream(new fileinputstream(src), 1024);           out = new bufferedoutputstream(new fileoutputstream(dst), 1024);            byte[] buffer = new byte[1024];         int len = 0;            while ((len = in.read(buffer)) > 0) {                out.write(buffer, 0, len);          }           return true;        } catch (exception e) {         e.printstacktrace();            return false;       } finally {         if (null != in) {               try {                   in.close();             } catch (ioexception e) {                   e.printstacktrace();                }           }           if (null != out) {              try {                   out.close();                } catch (ioexception e) {                   e.printstacktrace();                }           }       }   }   /**  * 圖像壓縮  *   * @param fi大圖文件     * @param fo將要轉換出的小圖文件   */ public boolean resize_image(file fi, file fo) {     try {           // file fi = new file("e:/3.jpg"); // 大圖文件          // file fo = new file("e:/333.jpg"); // 將要轉換出的小圖文件          // affinetransform transform = new affinetransform();           bufferedimage bis = imageio.read(fi);           int w = bis.getwidth();         // system.out.println(w);           int h = bis.getheight();            // system.out.println(h);           // double scale = (double) w / h;           int nw = w;         int nh = h;         bufferedimage tag = new bufferedimage(nw, nh,                   bufferedimage.type_int_rgb);            this.imgwidth = tag.getwidth();         this.imgheight = tag.getheight();           if (this.imgwidth > 250)             this.imgwidth = 250;            if (this.imgheight >= 250)               this.imgheight = 250;           tag.getgraphics().drawimage(bis, 0, 0, nw, nh, null); // 繪制縮小後的圖            // 轉換為32*32 jpg格式           fileoutputstream newimage = new fileoutputstream(fo); // 輸出到文件流         jpegimageencoder encoder = jpegcodec.createjpegencoder(newimage);           encoder.encode(tag);            newimage.close();           return true;        } catch (exception e) {         e.printstacktrace();            return false;       }   }   public void imageshow(int width, int height, int w, int h) {        int i = width;      int j = height;     width = w;      height = h;     if (i > w) {         float width_bili = w / i;           float height_bili = h / j;          float bili = width_bili;            if (width_bili < height_bili) {              bili = width_bili;          } else {                bili = height_bili;         }           i = (int) (i * bili);           j = (int) (j * bili);       } else {            if (j > h) {             float bili = (float) (h / j);               i = (int) (i * bili);               j = (int) (j * bili);           }       }       width = i;      height = j; }   /**  * 圖像的縮略     *   *   * width 輸出圖片大小 height   *   * @param fi大圖文件     * @param fo將要轉換出的小圖文件   */ public boolean resize_image(file fi, file fo, int width, int height) {      try {           bufferedimage bis = imageio.read(fi);           int w = bis.getwidth();         int h = bis.getheight();            int nw = width;         int nh = height;            bufferedimage tag = new bufferedimage(nw, nh,                   bufferedimage.type_int_rgb);            tag.getgraphics().drawimage(bis, 0, 0, nw, nh, null); // 繪制縮小後的圖            // 轉換為32*32 jpg格式           fileoutputstream newimage = new fileoutputstream(fo); // 輸出到文件流         jpegimageencoder encoder = jpegcodec.createjpegencoder(newimage);           encoder.encode(tag);            newimage.close();           return true;        } catch (exception e) {         e.printstacktrace();            return false;       }   }   /**  * 圖像的等比縮略   *   *   * width 輸出圖片大小 height   *   * @param fi大圖文件     * @param fo將要轉換出的小圖文件   */ public boolean geometric_image(file fi, file fo, int width, int height) {       try {           bufferedimage bis = imageio.read(fi);           int w = bis.getwidth();         int h = bis.getheight();            int nw = w;         int nh = h;         if (w > width) {             nw = width;             nh = nw * h / w;                if (nh > height) {                   nh = height;                    nw = nh * w / h;                }           } else if (h > height) {             nh = height;                nw = nh * w / h;                if (nw > width) {                    nw = width;                 nh = nw * h / w;                }           }           bufferedimage tag = new bufferedimage(nw, nh,                   bufferedimage.type_int_rgb);            tag.getgraphics().drawimage(bis, 0, 0, nw, nh, null); // 繪制縮小後的圖            // 轉換為32*32 jpg格式           fileoutputstream newimage = new fileoutputstream(fo); // 輸出到文件流         jpegimageencoder encoder = jpegcodec.createjpegencoder(newimage);           encoder.encode(tag);            newimage.close();           return true;        } catch (exception e) {         e.printstacktrace();            return false;       }   }   public string getimgtitle() {       return imgtitle;    }   public void setimgtitle(string imgtitle) {      this.imgtitle = imgtitle;   }   public string getalign() {      return align;   }   public void setalign(string align) {        this.align = align; }   public qqrservice getqqrservice() {     return qqrservice;  }   public void setqqrservice(qqrservice qqrservice) {      this.qqrservice = qqrservice;   }   public file getimgfile() {      return imgfile; }   public void setimgfile(file imgfile) {      this.imgfile = imgfile; }   public string getimgfilefilename() {        return imgfilefilename; }   public void setimgfilefilename(string imgfilefilename) {        this.imgfilefilename = imgfilefilename; }   public string getimgfilecontenttype() {     return imgfilecontenttype;  }   public void setimgfilecontenttype(string imgfilecontenttype) {      this.imgfilecontenttype = imgfilecontenttype;   }   public string getfiledir() {        return filedir; }   public void setfiledir(string filedir) {        this.filedir = filedir; }} 

 

本文出自“tianlanlan”

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。