2012年5月30日 星期三

[Android] [JAVA] 顯示剩餘電量的AP

1). 在主程式中,建立BroadcastReceiver類型的變數,利用BatteryManager讀出電量的資料

2). 註冊receive,僅有此可以利外寫在這,而不用寫在AndroidManifest.xml

//=============================================================
package com.broadcasttest2;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Bundle;
import android.widget.TextView;
public class BroadcastTest2 extends Activity {
  private TextView BatteryChange;
  private BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
  @Override
  public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
      // 目前電量的值
      int level = intent.getIntExtra("level", 0);
      // 表示電量的總刻度
      int scale = intent.getIntExtra("Scale", 100);
      // 也可以寫成這樣
      //int voltage = intent.getIntExtra(BatteryManager.EXTRA_VOLTAGE, 100);
    BatteryChange.setText("電池用量" + (level * 100 / scale) + "%" );
    }
  }
 };
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  BatteryChange = (TextView) findViewById(R.id.battery);
  registerReceiver(batteryReceiver, new IntentFilter(
  Intent.ACTION_BATTERY_CHANGED));
 }
}

[Android] [JAVA] 開機自動執行的AP

1). 寫一個主程式,Broadcast需要去啟動的程式。BroadcastTest1

2). 繼承Broadcast類別,並設定intent連到主程式。StartUpReceiver

3). 在Androidmanifest.xml設置<receive>的標籤

4). 在Androidmanifest.xml添加權限

//=============================================================

[BroadcastTest1]
package com.broadcasttest1;
import android.app.Activity;
import android.os.Bundle;

public class BroadcastTest1 extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
  }
}


//-------------------------------------------------------------------------------------------------------------

[StartUpReceiver]
package com.broadcasttest1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class StartUpReceiver extends BroadcastReceiver {
  @Override
  public void onReceive(Context arg0, Intent arg1) {

    Intent intent = new Intent(arg0, BroadcastTest1.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    arg0.startActivity(intent);

  }
}


//-------------------------------------------------------------------------------------------------------------

[AndroidManifest.xml]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="
http://schemas.android.com/apk/res/android"
  package="com.broadcasttest1"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk android:minSdkVersion="15" />
  
  
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <!-- Activity -->
    <activity
      android:name=".BroadcastTest1"
      android:label="@string/app_name" >
       <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
    </activity>
       
    <!-- Receiver -->
    <receiver android:name=".StartUpReceiver" >
      <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
      </intent-filter>
    </receiver>
  

  </application>
</manifest>

[Android] [JAVA] Broadcast

1). 編寫一個繼承Broadcast的類別,並實現裡面的onReceive方法

2). 在AndroidManifest.xml使用<receive>標籤指定第一步的類別接收哪一個Broadcast Action

3). 如果有需要,在AndroidManifest.xml添加權限

2012年5月29日 星期二

[Repo] note

● repo的流程:
1). git init -u [site] 會將設定檔及source code的server型態,下載到project_dir/.repo中

2). 若有需要,修改project_dir/.repo/manifest/default.xml
此會決定要下載哪些project,版本分別是什麼

3). 再到project_dir/platform中repo sync
source code就會從server型態轉成git型態,此步不需要網路連線
server型態的存在project_dir/.repo/projects
裡面每一個.git都是一個git project


● git init 若加--mirror,出來的會是git server的型態

2012年5月25日 星期五

[Android] [JAVA] Intent介紹

一、介紹
Intent的功能類似網頁的超連結,使用者可以藉由超連結前往另一個頁面
Intent的動作是從目前的Activity執行另一個Activity。當目前的Activity執行另一個Activity,原來的Activity會進入休眠的狀態,然後將執行的工作權交給另一個Activity



二、intent流程
1). 定義intent
Intent intent=new Intent(動作, 內容)
Intent intent=new Intent(主程式類別.class, 自訂類別.class)
ex: Intent intent = new Intent(DeviceInfo.this, mService.class);

2). 決定誰要去用他
ex: startActivity(Intent)


三、使用intent傳遞資料的流程
1). 使用setClass方法指定執行類別
Intent intent = new Intent(DeviceInfo.this, mService.class);

2). 依照資料型別以Bundle物件打包
Bundle bundle=new Bundle();
bundle.putString("Name","Peter");

3). 利用intent的putExtras方法加入Bundle物件
intent.putExtras(bundle);

4). 使用startActivity方法執行intent
startActivity(intent);



四、取出intent資料的流程
1). 以getIntent()方法取得傳送的Intent
Intent intent=this.getIntent();

2). 利用intent的getExtras()方法,從intent中取得bundle物件
Bundle bundle=intent.getExtras();

3). 根據名稱取得bundle物件的資料
String Name=bundle.getString("Name");

2012年5月24日 星期四

[Example] [Android] [JAVA] Android APP應用2

















按了button觸發啟動Service,顯示Toast並延遲兩秒啟動Notification
按下Notification進入到另外一個Activity
此可以列出device跟sw的資訊

參考文章:


[Android] [JAVA] Service & Notification

按了button觸發啟動Service,顯示Toast並延遲兩秒啟動Notification
按下Notification進入到另外一個Activity

1). 在Activity中設置點擊按鈕觸發的事件如下
宣告Intent所要執行的java檔(mService.java),並啟動Service

2). mService.java檔中宣告Service的lifecycle,分別是onCreate(),onStart(),onDestory
宣告handler,管理新開的thread,名為task的Runnable
宣告Runnable,執行所要做的Notification method

3). 撰寫開啟Notification Manager的method
設定要跳去哪一個Activity
參考[Android] [JAVA] Notification & PendingIntent

4). onCreate()繼承原有的onCreate()
onStart()中寫好延遲2秒去執行task,接著結束此Service,使lifecycle進入onDestory()
onDestory()繼承原有的onDestory()

5). Runnable中去執行Notification的method

//=============================================================


[string.xml]

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="hello">Hello World, DeviceInfo1!</string>
  <string name="app_name">DeviceInfo2</string>
  <string name="android_version">Android Version</string>
  <string name="kernel_version">Kernel Version</string>
  <string name="model_number">Model Number</string>
  <string name="product_sku">Product Sku</string>
  <string name="image_version">Image Version</string>
  <string name="ec_version">EC Version</string>
  <string name="cpu_vendor">CPU Vendor</string>
  <string name="cpu_speed">CPU Speed</string>
  <string name="cpu_version">CPU Version</string>
  <string name="info_submit">Submit</string>
   
  <string name="main_text">Please select the function as list. </string>    
  <string name="main_text_info1">Start service and notification bar after 2 senconds. 
              Then it will change activity to DeviceInfo.</string>    
  <string name="main_submit_info1">1. Device Info</string>     


</resources>


//-------------------------------------------------------------------------------------------------------------


[info.xml]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal" >

  <LinearLayout
    android:layout_width="219dp"
    android:layout_height="691dp"
    android:orientation="vertical" >

  <CheckBox
    android:id="@+id/checkBox1_android_version"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:text="@string/android_version" />

  <CheckBox
    android:id="@+id/checkBox2_kernel_version"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/kernel_version" />

  <CheckBox
    android:id="@+id/checkBox3_model_number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/model_number" />

  <CheckBox
    android:id="@+id/checkBox4_product_sku"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/product_sku" />

  <CheckBox
    android:id="@+id/checkBox5_image_version"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/image_version" />

  <CheckBox
    android:id="@+id/checkBox6_ec_version"
    android:layout_width="match_parent"
     android:layout_height="wrap_content"
    android:text="@string/ec_version" />

  <CheckBox
    android:id="@+id/checkBox7_cpu_vendor"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/cpu_vendor" />

  <CheckBox
     android:id="@+id/checkBox8_cpu_speed"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/cpu_speed" />

  <CheckBox
     android:id="@+id/checkBox9_cpu_version"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/cpu_version" />

  <Button
    android:id="@+id/submit"
    android:layout_width="191dp"
    android:layout_height="wrap_content"
    android:text="@string/info_submit" />

  <AbsoluteLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
  </AbsoluteLayout>

  </LinearLayout>

  <LinearLayout
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical" >

    <TextView
      android:id="@+id/result"
      android:layout_width="1015dp"
      android:layout_height="match_parent"
      android:text="" />

  </LinearLayout>
</LinearLayout>

//-------------------------------------------------------------------------------------------------------------

[main.xml]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >


  <TextView
    android:id="@+id/main_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/main_text"
    android:textAppearance="?android:attr/textAppearanceLarge" />


  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >


  <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="0.02"
    android:orientation="vertical" >


    <Button
      android:id="@+id/main_submit_info1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="@string/main_submit_info1" />


  </LinearLayout>


  <LinearLayout
    android:layout_width="1028dp"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <TextView
      android:id="@+id/main_text_info1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom"
      android:text="@string/main_text_info1" />


    </LinearLayout>
  </LinearLayout>
</LinearLayout>

//-------------------------------------------------------------------------------------------------------------

[DeviceInfo2.java]

package com.deviceinfo2;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class DeviceInfo2 extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // 找到元件資源
    TextView text_main=(TextView)findViewById(R.id.main_text);
    TextView text_main_info1=(TextView)findViewById(R.id.main_text_info1);
    Button button_info1 = (Button) findViewById(R.id.main_submit_info1);

    // 設定title字體大小
    text_main.setTextSize(25);
    text_main_info1.setTextSize(20);
    // 設定按鍵觸發的method
    button_info1.setOnClickListener(listDeviceInfo);
}


  private Button.OnClickListener listDeviceInfo = new Button.OnClickListener() {
    @Override
    public void onClick(View v) {

    // Toast
    Toast.makeText(v.getContext(), "Please wait for notification about 2 senconds",
               Toast.LENGTH_LONG).show();
    // 宣告intent,並指定要啟動的class
    Intent intent = new Intent(DeviceInfo2.this, mService.class);
    // 以startservice方式啟動intent
    startService(intent);
    }
  };
}

//-------------------------------------------------------------------------------------------------------------

[mService.java]

package com.deviceinfo2;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;


public class mService extends Service {
  private Handler handle = new Handler();
  private int count=0;


  @Override
  public void onCreate() {
    Log.d("TAG", "onCreate");
    super.onCreate();
  }


  @Override
  public IBinder onBind(Intent arg0) {
    return null;
  }


  public void onStart(Intent intent, int startId) {
    Log.d("TAG", "onStart");
    handle.postDelayed(tasks, 2000);
    // 停掉自己這個服務
    mService.this.stopSelf();
  }


  private Runnable tasks = new Runnable() {
    @Override
    public void run() {
    Log.d("TAG", "Runnable");
  Log.d("RUN", "RunCount: " + Integer.toString(count++));
      notification();
      handle.removeCallbacks(tasks);
    }
  };

  private void notification(){
    Log.d("TAG", "Notification");
    // 取得Notification服務
    NotificationManager notificationManager = (NotificationManager) 
              getSystemService(NOTIFICATION_SERVICE);
    // 設定當按下這個通知之後要執行的activity
    Intent notifyIntent = new Intent(mService.this,
                  DeviceInfoActivity.class);
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent appIntent = PendingIntent.getActivity(
                  mService.this, 0, notifyIntent, 0);
    Notification notification = new Notification();
    // 設定出現在狀態列的圖示
    notification.icon = R.drawable.ic_launcher;
    // 顯示在狀態列的文字
    notification.tickerText = "notification on status bar.";
    // Notification被點擊後,便消失
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    // 會有通知預設的鈴聲、振動、light
    notification.defaults |= Notification.DEFAULT_ALL;
    // 設定通知的標題、內容
    notification.setLatestEventInfo(mService.this, "DeviceInfo",
              "Checkoout the infomation of device", appIntent);
    // 送出Notification
    notificationManager.notify(0, notification);
  }


  public void onDestroy() {
    Log.d("TAG", "onDestory");
    super.onDestroy();
  }
}

//-------------------------------------------------------------------------------------------------------------

[DeviceInfoActicity.java]
package com.deviceinfo2;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.util.Log;
import android.view.View;
import android.os.SystemProperties;

public class DeviceInfoActivity extends Activity {
  private CheckBox checkBox1_android_version, checkBox2_kernel_version,
      checkBox3_model_number, checkBox4_product_sku,
      checkBox5_image_version, checkBox6_ec_version,
      checkBox7_cpu_vendor, checkBox8_cpu_speed, checkBox9_cpu_version;
  private Button button;
  private TextView result;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.info);

    // 取得資源類別中的介面元件
    checkBox1_android_version = (CheckBox) findViewById(R.id.checkBox1_android_version);
    checkBox2_kernel_version = (CheckBox) findViewById(R.id.checkBox2_kernel_version);
    checkBox3_model_number = (CheckBox) findViewById(R.id.checkBox3_model_number);
    checkBox4_product_sku = (CheckBox) findViewById(R.id.checkBox4_product_sku);
    checkBox5_image_version = (CheckBox) findViewById(R.id.checkBox5_image_version);
    checkBox6_ec_version= (CheckBox) findViewById(R.id.checkBox6_ec_version);
    checkBox7_cpu_vendor = (CheckBox) findViewById(R.id.checkBox7_cpu_vendor);
    checkBox8_cpu_speed = (CheckBox) findViewById(R.id.checkBox8_cpu_speed);
    checkBox9_cpu_version = (CheckBox) findViewById(R.id.checkBox9_cpu_version);
    button = (Button) findViewById(R.id.submit);
    result = (TextView) findViewById(R.id.result);

    // button元件要偵聽動作及觸發執行的方法名稱
    button.setOnClickListener(listInfo);
  }

  private Button.OnClickListener listInfo = new Button.OnClickListener() {
    @Override
    public void onClick(View v) {
      result.setText("");
      result.setTextSize(20);

      if (checkBox1_android_version.isChecked()) {
        result.append("Android Version: "
             + SystemProperties.get("ro.build.version.release")
             + "\n\n");
      }
      if (checkBox2_kernel_version.isChecked()) {
        Properties props = System.getProperties();
        result.append("Kernel Version: "
             + props.getProperty("os.version") + "\n\n");
      }
      if (checkBox3_model_number.isChecked()) {
        result.append("Model Number: "
             + SystemProperties.get("ro.product.model") + "\n\n");
      }
      if (checkBox4_product_sku.isChecked()) {
        result.append("Product Sku: "
             + SystemProperties.get("ro.dinfo.sku") + "\n\n");
      }
      if (checkBox5_image_version.isChecked()) {
        result.append("Image Version: "
             + SystemProperties.get("ro.build.display.id") + "\n\n");
      }
      if (checkBox6_ec_version.isChecked()) {
        result.append("EC Version: " + getECVer() + "\n");
      }
      if (checkBox7_cpu_vendor.isChecked()) {
        result.append("CPU Vendor: "
             + SystemProperties.get("ro.cpu.vendor") + "\n\n");
      }
      if (checkBox8_cpu_speed.isChecked()) {
        result.append("CPU Speed: "
             + SystemProperties.get("ro.cpu.speed") + "\n\n");
      }
      if (checkBox9_cpu_version.isChecked()) {
        result.append("CPU Version: "
             + SystemProperties.get("ro.cpu.version") + "\n\n");
      }
    }
  };

  private String getECVer() {
    String str = "";
    int len = 0;
    char[] cbuf = new char[200];
    try {
      FileReader fn = new FileReader("EC路徑");
      try {
        while ((len = fn.read(cbuf, 0, cbuf.length)) >= 0) {
          Log.d("***", str);
          str += String.valueOf(cbuf, 0, len);
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
    return str;
  }
}

//-------------------------------------------------------------------------------------------------------------

[AndroidManifest.xml]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.deviceinfo2"
  android:versionCode="1"
  android:versionName="1.0" >

  <uses-sdk android:minSdkVersion="15" />
  <uses-permission android:name="android.permission.VIBRATE"></uses-permission>
  <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
  <activity
    android:name=".DeviceInfo2"
    android:label="@string/app_name" >
    <intent-filter>
    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
  </activity>
  <activity android:name="DeviceInfoActivity" >
  </activity>
  <service
    android:name=".mService"
    android:exported="true"
    android:process=":remote" >
  </service>
  </application>
</manifest>