2013年8月28日

Android─ Action bar Navigate Up


                                                               The Up button in the action bar.

原本的up button按了會返回上一個parent activity

back與home的差別

也可以更改為返回上一頁
範例 code如下

@Override
public boolean onOptionsItemSelected(MenuItem item) { 
        switch (item.getItemId()) {
        case android.R.id.home: //為up butoon的id 
            onBackPressed();
            return true;
        }
    return super.onOptionsItemSelected(item);
}

Java─getclass() 用途

Use Object.getClass(). It returns the runtime type of the object.

Object instance = new SomeClass();
instance.getClass().getName(); //will return the name (as String) (== "SomeClass")
instance.getClass(); //will return the SomeClass' Class object
參考連結

Android─取得系統時間 和比較時間

Calendar c = Calendar.getInstance(); 
取得系統日期:year = c.get(Calendar.YEAR);
              month = c.grt(Calendar.MONTH);
              day = c.get(Calendar.DAY_OF_MONTH);
取得系統時間:hour = c.get(Calendar.HOUR_OF_DAY); 
              minute = c.get(Calendar.MINUTE);

如何比較時間
Calendar c = Calendar.getInstance();
    c.set(Calendar.MONTH, Calendar.MARCH);
    c.set(Calendar.DAY_OF_MONTH, 3);
 long time2=   c.getTimeInMillis();
 c.set(Calendar.MONTH, Calendar.AUGUST);
 c.set(Calendar.DAY_OF_MONTH, 8);   
 long time3=   c.getTimeInMillis();
 if(time>time2){
     //Logic
     if(time>time3){
         //Logic
     }
 }

2013年8月14日

Java ─ Arraylist基本操作

宣告一個新的arraylist
 ArrayList arlist=new ArrayList(); 

 加入字串進入arraylist 
 arlist.add("First Element"); 
 arlist.add("Second Element"); 

 取得arraylist內容 
 arlist.get(0); 
 arlist.get(1);

 清空arraylist所有資料 arlist.clear();

參考來源

Android ─ 如何將string.xml資料變成字串

<string name="content">我是顯示內容</string>

透過layout 設定
<TextView<
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/content">
</TextView>

java程式碼 設定
text.setText(getString(R.string.content));

設定為字串變數

在activity下
this.getString(R.string.content);

在非activity下
context.getString(R.string.content);
application.getString(R.string.content);

Android ─ Textview 顯示html語法內容

需要
import android.text.Html;

範例程式碼:
mytextview.setText(Html.fromHtml("<h1>&hearts;zcbbupt</h1>&#8226; <b>foo</b><br/> &#8226; bar<br/> &#8226; baz<br/>"));
則mytextview則會顯示html語法的內容

目前支援的Html tag如下
  • br
  • p
  • div
  • em
  • b
  • strong
  • cite
  • dfn
  • i
  • big
  • small
  • font
  • blockquote
  • tt
  • monospace
  • a
  • u
  • sup
  • sub


由於沒有支援 li tag,所以如果想要有bullet效果
可以利用ASCII碼來顯示 bullet 黑點

程式碼如下
mytextview.setText(Html.fromHtml("&#8226;item1<br> &#8226;item2<br>"));

結果如下
  • item1
  • item2

Java ─ int string 轉換

int to string


  1. String stringValue = Integer.toString(12345);
  2. String stringValue = String.valueOf(12345);
  3. String stringValue = new String(""+12345);

String to int


  1.  int intValue = Integer.valueOf("12345");
  2.  int intValue = Integer.parseInt("12345");


Android ─ Intent 傳送int,取得int方式

傳入參數方式

public void onClick(DialogInterface dialog, int id) {
       Intent intent = new Intent(mContext, org.gpsagenda.DetailsContainer.class);
       intent.putExtra("id", item.ID());
       mContext.startActivity(intent);
   }

接收參數方式

int id = getIntent().getExtras().getInt("id");

2013年8月13日

Android install / uninstall APK by Intent

使用此方法,會有提示視窗

參考連結

Install APK using Intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
startActivity(intent);
Uninstall APK using Intent:
Intent intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package",
getPackageManager().getPackageArchiveInfo(apkUri.getPath(), 0).packageName,null));
startActivity(intent);

Android install uninstall 回傳值意義

    public final int INSTALL_REPLACE_EXISTING = 2;

    public static final int INSTALL_SUCCEEDED = 1;
    public static final int INSTALL_FAILED_ALREADY_EXISTS = -1;
    public static final int INSTALL_FAILED_INVALID_APK = -2;
    public static final int INSTALL_FAILED_INVALID_URI = -3;

    public static final int INSTALL_FAILED_INSUFFICIENT_STORAGE = -4;
    public static final int INSTALL_FAILED_DUPLICATE_PACKAGE = -5;
    public static final int INSTALL_FAILED_NO_SHARED_USER = -6;
    public static final int INSTALL_FAILED_UPDATE_INCOMPATIBLE = -7;
    public static final int INSTALL_FAILED_SHARED_USER_INCOMPATIBLE = -8;
    public static final int INSTALL_FAILED_MISSING_SHARED_LIBRARY = -9;
    public static final int INSTALL_FAILED_REPLACE_COULDNT_DELETE = -10;
    public static final int INSTALL_FAILED_DEXOPT = -11;
    public static final int INSTALL_FAILED_OLDER_SDK = -12;
    public static final int INSTALL_FAILED_CONFLICTING_PROVIDER = -13;
    public static final int INSTALL_FAILED_NEWER_SDK = -14;
    public static final int INSTALL_FAILED_TEST_ONLY = -15;
    public static final int INSTALL_FAILED_CPU_ABI_INCOMPATIBLE = -16;
    public static final int INSTALL_FAILED_MISSING_FEATURE = -17;

    public static final int INSTALL_FAILED_CONTAINER_ERROR = -18;
    public static final int INSTALL_FAILED_INVALID_INSTALL_LOCATION = -19;
    public static final int INSTALL_FAILED_MEDIA_UNAVAILABLE = -20;
    public static final int INSTALL_PARSE_FAILED_NOT_APK = -100;
    public static final int INSTALL_PARSE_FAILED_BAD_MANIFEST = -101;
    public static final int INSTALL_PARSE_FAILED_UNEXPECTED_EXCEPTION = -102;
    public static final int INSTALL_PARSE_FAILED_NO_CERTIFICATES = -103;
    public static final int INSTALL_PARSE_FAILED_INCONSISTENT_CERTIFICATES = -104;
    public static final int INSTALL_PARSE_FAILED_CERTIFICATE_ENCODING = -105;
    public static final int INSTALL_PARSE_FAILED_BAD_PACKAGE_NAME = -106;
    public static final int INSTALL_PARSE_FAILED_BAD_SHARED_USER_ID = -107;
    public static final int INSTALL_PARSE_FAILED_MANIFEST_MALFORMED = -108;
    public static final int INSTALL_PARSE_FAILED_MANIFEST_EMPTY = -109;
    public static final int INSTALL_FAILED_INTERNAL_ERROR = -110;

2013年8月12日

Android 靜默刪除app

此方法必須擁有系統權限,第三方的app無法執行

首先需要
import android.content.pm.IPackageDeleteObserver;


此class系統會callback returnCode告知app是否刪除成功
class PackageDeleteObserver extends IPackageDeleteObserver.Stub { 
    public void packageDeleted(String packageName, int returnCode) {
    Log.i("service", "uninstall packageDeleted callback!!!");  
    if(returnCode!=1)
        //returnCode不等於一,代表刪除失敗 
    else
        //returnCode等於一代表刪除成功

呼叫靜默刪除function
private void uninstallapp(){ 
    PackageDeleteObserver observer = new PackageDeleteObserver();
    PackageManager pm = this.getApplicationContext().getPackageManager();
    pm.deletePackage(packagename, observer, 0);
 }
Manifest.xml要加以下權限
uses-permission android:name="android.permission.INSTALL_PACKAGES" 
uses-permission android:name="android.permission.DELETE_PACKAGES" 


參考網頁

PackageManagerService.java source code

Blogger加掛SyntaxHighlighter,可插入美麗的程式碼

在Blogger的後台,打開「範本」、「編輯HTML」。

先找到<b:skin>標籤,在此標籤前加入以下代碼。
<!-- SyntaxHighlighter  3.0.83-->
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/>
<link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shAutoloader.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushAS3.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushColdFusion.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCpp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDelphi.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushDiff.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushErlang.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushGroovy.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJava.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJavaFX.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPerl.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPhp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPowerShell.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushScala.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushVb.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>

再找到</body>標籤,在標籤前加入以下代碼。
<!-- SyntaxHighlighter  3.0.83-->
<script type='text/javascript'>
     SyntaxHighlighter.all()
</script>

完成修改後,儲存。

接著在編輯網誌文章時,使用「修改HTML」模式,使用<pre>標籤來呈現程式碼,而class屬性則設為「brush: 程式語言代碼」,程式語言代碼請參考在文章開頭的SyntaxHighlighter已支援程式語言列表。
<pre class="brush: groovy">
println 'Hello World'
println 1 + 2 + 3
</pre>
SyntaxHighlighter支援語言
參考網址