2014年7月10日 星期四

Android 程式小技巧

A.訊息提示:
      Toast.makeText(contextAct, "readHRDatas",
              Toast.LENGTH_SHORT).show();

B.讓APP完整的退出for執行緒的處理:
   A. intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

   B.@Override
public void onDestroy()
{

stopDraw();
Log.d("test onDestroy", "destory");
setResult(0);
finish();
super.onDestroy();
}

@Override
public void onStop(){
super.onStop();
stopDraw();
 finish();
}



@Override
public void onPause() {
        super.onPause();
        stopDraw();
        finish();

    }

public void stopDraw(){
Log.d("stopDraw","stopDraw!!");
isRunGraph = false;

if(th1 != null ){
   th1.interrupt();
   th1 = null;
}

if(graphView != null){
//graphView.destroyDrawingCache();
graphView.removeAllViews();

graphView = null;
}


}

C.偵測dialog點擊視窗範圍內與否:
extends Dialog{
@Override
public boolean onTouchEvent(MotionEvent event) {

 if ( event.getAction () == MotionEvent.ACTION_UP ) {

   Rect r = new Rect ( 0, 0, 0, 0 );
   this.getWindow ().getDecorView ().getHitRect ( r );
   boolean intersects = r.contains ( (int) event.getX (), (int) event.getY () );
 
   if ( !intersects ) {
     this.dismiss();
     return true;
   }else{
    countClick++;
    if(countClick == 5){
    Intent ite = new Intent();
    ite.setClass(mContext, com.foxlink.wearable.engineering.EngineeringActivity.class);
    mContext.startActivity(ite);
    this.dismiss();
    countClick = 0;
    }
   }
 
 }
 return super.onTouchEvent ( event );
}

D.APP中呼叫藍芽設定:
 final Intent intent = new Intent(Intent.ACTION_MAIN, null);
         intent.addCategory(Intent.CATEGORY_LAUNCHER);
         ComponentName cn = new ComponentName("com.android.settings",
                 "com.android.settings.bluetooth.BluetoothSettings");
         intent.setComponent(cn);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity( intent);

E.執行續處理方式:
 http://stackoverflow.com/questions/10544515/how-to-stop-a-running-thread-when-activity-on-destroy-at-android

F.網站下載檔案並存檔
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet();
try {
File root = Environment.getExternalStorageDirectory();
BufferedOutputStream bout = new BufferedOutputStream(
new FileOutputStream(
root.getAbsolutePath() + "/version_an.txt"));


Log.d("root.getAbsolutePath()-", root.getAbsolutePath().toString());
request.setURI(new URI("http://1111111/img/version.cfg"));
HttpResponse response = client.execute(request);
StatusLine status = response.getStatusLine();
//textView1.append("status.getStatusCode(): " + status.getStatusCode() + "\n");
Log.d("Test", "Statusline: " + status);
Log.d("Test", "Statuscode: " + status.getStatusCode());

HttpEntity entity = response.getEntity();
//textView1.append("length: " + entity.getContentLength() + "\n");
//textView1.append("type: " + entity.getContentType() + "\n");
Log.d("Test", "Length: " + entity.getContentLength());
Log.d("Test", "type: " + entity.getContentType());

entity.writeTo(bout);

bout.flush();
bout.close();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
//textView1.append("URISyntaxException");
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
//textView1.append("ClientProtocolException");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
//textView1.append("IOException");
e.printStackTrace();
}

沒有留言:

張貼留言