2014年11月15日 星期六

Android-將物件 存放 / 讀取 Cache

各位跟我一樣是開發新手的大大們
想必多少都會遇到想暫存資料
是圖片那還有很多資訊可找
但如果是自訂的物件呢?
用 SharedPreferences 又有型別的限制
讓人超級頭痛的
這幾天不斷的用G大神搜索,終於在GitHub找到超好用的Class
但前提是,必須要將物件序列化 implements Serializable


import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

public class SimpleObjectCache {

    public static void saveObject(Object object, String id,Context context) {
 
        try {
            ObjectOutput out = new ObjectOutputStream(new FileOutputStream(
    new File(context.getCacheDir(), "") + "/"+id));
    
            out.writeObject(object);
            out.close();
   
        } catch (Exception e) {
            e.printStackTrace();
   
        }
    }

    public static Object loadObject(String id,Context context) 
   throws FileNotFoundException {
   
        Object object = null;
        ObjectInputStream in = null;
  
        try {
            in = new ObjectInputStream(new FileInputStream(
    new File(new File(context.getCacheDir(), "") + "/"+id)));
        
            object = in.readObject();
            in.close();
   
        }catch (IOException | ClassNotFoundException e){
            throw new FileNotFoundException();
   
        }
        return object;
  
    }
    
    public static Boolean removeObject(String id,Context context){
     File file = new File(new File(context.getCacheDir(), "") + "/"+id);
     return file.delete();
  
    }
}

* 服用方式
final String ID_BANDS = "bandsObejct";

// List of bands
ArrayList<String> bands = new ArrayList<String>();
bands.add("Arctic Monkeys");
bands.add("Led Zeppelin");
bands.add("Yann Tiersen");

// Saving
SimpleObjectCache.saveObject(bands, ID_BANDS, getApplicationContext());

// Retrieving
try {
    ArrayList<String> bandsTemp = (ArrayList<String>) SimpleObjectCache
            .loadObject(ID_BANDS, getApplicationContext());
    for (String string : bandsTemp) {
        Log.d("BANDS", string);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

// Deleting
Boolean wasRemoved = SimpleObjectCache.removeObject(ID_BANDS,
        getApplicationContext());

資料來源 : https://github.com/wesleiwpa/simple-object-cache-android

2014年11月13日 星期四

Android- 如何啟動 Service 服務程式 ( 一 )

Android 應用程式四大組成員之一 : Service
Service跟Activity建立方法非常相似
Service被啟動後,就算關閉ActivityService不會因此而關閉
就像是我們在手機執行音樂撥放,我們還是可以執行其他的程式

首先,我們先創立Activity 並且用 Intent啟動 Service

Activity.class 底下

啟動Service
Intent startIntent = new Intent(this, ServiceTest.class);  
startService(startIntent);


關閉Service
Intent stopIntent = new Intent(this, ServiceText.class);
stopService(stopIntent);

ServiceTest.class

創立Service
public class ServiceTest extends Service {  
  
    @Override  
    public void onCreate() {  
        super.onCreate();  
        Log.d("tag", "ServiceTest onCreate()");  
    }  
  
    @Override  
    public int onStartCommand(Intent intent, int flags, int startId) {  
        Log.d("tag", "ServiceTest onStartCommand()");  
        return super.onStartCommand(intent, flags, startId);  
    }  
      
    @Override  
    public void onDestroy() {  
        super.onDestroy();  
        Log.d("tag", "ServiceTest onDestroy()");  
    }  
  
    @Override  
    public IBinder onBind(Intent intent) {  
        return null;  
    }  
  
}


並且在AndroidManifest.xml 新增Service
   <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name="ServiceTest"></service>
    </application>

基本上就可以看Log變化
基礎的Service就完成了

2014年11月10日 星期一

Arduino-Upload 後 avrdude: stk500_getsync(): not in sync: resp=0x00

最近在玩Arduino UNO
卻發生avrdude: stk500_getsync(): not in sync: resp=0x00



結果只是藍芽裝置必須先拔除,不然無法同步更新
說真的只因為好懶呀~!

Arduino-藍芽發送資料( 測試藍芽功能是否正常 )

#define BUFFERSIZE 127
int numLoop = 0; // number of times we looped

void setup() {
  Serial.begin(9600);

}
void loop() {

  Serial.println(numLoop);
  char str[20];
  sprintf(str,"\nanalogRead,%d@",50);
  Serial.write(str);
  delay(5000);
  numLoop++;
}

2014年10月30日 星期四

Android-將目前 圖片 傳至下一個 頁面 ( The image pass to next activity by intent )

相信各位也有跟我一樣的問題
那就是如何將圖片送至下一個頁面
網路上的資訊似乎都轉成陣列傳送
但傳送的容量又會受到限制
這似乎已經成為大家都痛

目前的作法僅供參考

A Activity

try {
          
   Bitmap bitmap = drawableToBitmap(imageView.getDrawable());
   File file = new File(getCacheDir(),"MyImage");
   FileOutputStream fileOutStream = new FileOutputStream(file);
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutStream);

   Intent intent = new Intent();  
   intent.setClass(MainActivity.this, BActivity.class);
   intent.putExtra("image", Uri.fromFile(file) );
   startActivity(intent);
catch (FileNotFoundException e) {

   e.printStackTrace();
}

public static Bitmap drawableToBitmap(Drawable drawable) {  
    
  int w = drawable.getIntrinsicWidth();  
  int h = drawable.getIntrinsicHeight();  

    
  Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888  
        : Bitmap.Config.RGB_565;  
     
  Bitmap bitmap = Bitmap.createBitmap(w, h, config);  
      
  Canvas canvas = new Canvas(bitmap);  
  drawable.setBounds(0, 0, w, h);  
     
  drawable.draw(canvas);  
  return bitmap;  
}

B Activity

ImageView imageView = (ImageView) findViewById(R.id.imageView1);
Uri uri = getIntent().getParcelableExtra("MyImage");
imageView.setImageURI(uri);


目前的作法僅供參考


歡迎轉載,請註明出處

2014年10月27日 星期一

Android-在 Eclipse 更新 SDK failed the rename 一直失敗

話說前幾天因為一時興起,來去更新 android 的 SDK
想說開啟Eclipse後直接點Android SDK Manager 再點 Install X packages
然後掛著離開電腦前
2小時候,悲劇在此發生,只看到錯誤訊息 Failde to rename directory : ........
接下來 2-3 次都是一樣的結果
只好有請Google大神了

目前搜尋最佳解法如下 :

  1. 關閉Eclipse
  2. 執行命令提示字元( cmd )
  3. 直接開啟SDK底下的Tools
    cd C:\JAVA\eclipse\adt-bundle-windows-x86_64-20140702\sdk\tools
  4. 接下來直接輸入 android.bat 就可以更新了
就目前為止還沒出現錯誤

歡迎轉載

Android-混淆代碼 dexguard

com.saikoa.dexguard.eclipse.adt_23.0.0.v6_0_20.jar
放置在
C:\JAVA\eclipse\adt-bundle-windows-x86_64-20140702\eclipse\dropins底下
在輸出APP時就能有多混淆這個選項輸出