2011年4月28日 星期四

How to scan a barcode from another Android application via Intents

How to scan a barcode from another Android application via Intents
透過其他的應用程式,使用其內部所提供的功能
    public Button.OnClickListener mScan = new Button.OnClickListener() {
       
public void onClick(View v) {
           
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
            intent
.setPackage("com.google.zxing.client.android");
            // "SCAN_MODE" for all unknow code

            intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
            // use this method, can get the response by "onActivityResult( )"
            startActivityForResult(intent, 0);
       
}
    };
    // receive the result    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
       
if (requestCode == 0) // this line maybe can marked
           
if (resultCode == RESULT_OK) // -1
               
String contents = intent.getStringExtra("SCAN_RESULT");
               
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
               
// Handle successful scan
           
} else if (resultCode == RESULT_CANCELED) // 0
               
// Handle cancel
           
}
       
}
    }

2011年4月25日 星期一

NMEA資訊

NMEA資訊
    // get location service
    LocationManager location = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    // set location update settings(time...)
     location.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,new LocationListener(){
        public void onLocationChanged(Location loc) {}
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status,Bundle extras) {}
    });
    // listen the NMEA information when change
    location.addNmeaListener(new NmeaListener() {
        public void onNmeaReceived(long timestamp, String nmea) {
            msg.append(nmea);
        }
    });

System Settings修改

System Settings修改
    // get screen off timeout time
    try {
        time = Settings.System.getInt(
            this.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT);
    } catch (SettingNotFoundException e) {
        e.printStackTrace();
    }
    // modify timeout time to 15 seconds
    Settings.System.putInt(
        this.getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 15000);
    // modify rotation enable, 1: auto rotate on; 0: auto rotate off
    Settings.System.putInt(
        this.getContentResolver(), Settings.System.ACCELEROMETER_ROTATION, 1);
    // vibrator control
    vb = (Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
    vb.vibrate(1000*1000);    // parameter: vibrate time(ms)
    vb.cancel();    // stop

2011年4月21日 星期四

Load Image

Load Image
    String myJpgPath = "/sdcard/test2.jpg";
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2;
    Bitmap bm = BitmapFactory.decodeFile(myJpgPath, options);
    jpgView.setImageBitmap(bm);

Notification

Notification
簡易範例
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
     
    Notification notification = new Notification();
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    mNotificationManager.notify(123, notification);

2011年4月20日 星期三

Light感測器

Light感測器
    public void onCreate(Bundle savedInstanceState) {
        ................
        // get reference to SensorManager
        SensorManager sm = (SensorManager) getSystemService(SENSOR_SERVICE);
        Sensor sensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
        sm.registerListener(lsn, sensor, SensorManager.SENSOR_LIGHT);
    }

    SensorEventListener lsn = new SensorEventListener() {
        public void onAccuracyChanged(Sensor sensor, int accuracy) { }

        public void onSensorChanged(SensorEvent event) {
            // light sensor informations
            cloudy.setText(String.valueOf(SensorManager.LIGHT_CLOUDY));
            fullmoon.setText(String.valueOf(SensorManager.LIGHT_FULLMOON));
            no_moon.setText(String.valueOf(SensorManager.LIGHT_NO_MOON));
            overcast.setText(String.valueOf(SensorManager.LIGHT_OVERCAST));
            shade.setText(String.valueOf(SensorManager.LIGHT_SHADE));
            sunlight.setText(String.valueOf(SensorManager.LIGHT_SUNLIGHT));
            sunlight_max.setText(String.valueOf(SensorManager.LIGHT_SUNLIGHT_MAX));
            sunrise.setText(String.valueOf(SensorManager.LIGHT_SUNRISE));
            // light sensor values
            txtMsg.setText("Ambient light level = " + event.values[0] + " lux");
        }
    };

FingerPaint功能

FingerPaint功能
下面為一繪圖的範例
    public class Project1 extends Activity {
        private Bitmap  mBitmap;
        private MyView mView;

        public void onCreate (Bundle bundle) {
            super.onCreate(bundle);
            // remove the title, and display "full screen"
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            this.getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
            // get the windows resolution, for bitmap use
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            // create a new bitmap for paint
            mBitmap = Bitmap.createBitmap(dm.widthPixels, dm.heightPixels, Bitmap.Config.ARGB_8888);
            // use "MyView" class, this is define by author
            mView = new MyView(this);
            setContentView(mView);
        }

        private class MyView extends View {
            private Canvas mCanvas;
            private Path mPath;
            private Paint mBitmapPaint;
            private Paint mPaint;

            private float new_x, new_y;
            private float old_x, old_y;

            public MyView(Context context) {
                super(context);
                // create and initial object
                mCanvas = new Canvas(mBitmap);
                mPath = new Path();
                mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        
                mPaint = new Paint();
                // make paint more smooth
                mPaint.setAntiAlias(true);
                mPaint.setDither(true);
                // (1.FILL 2.FILL_AND_STROKE 3.STROKE)
                mPaint.setStyle(Paint.Style.STROKE);
                // (1.BEVEL 2.MITER 3.ROUND) default is MITER
                mPaint.setStrokeJoin(Paint.Join.ROUND);
                // (1.BUTT 2.ROUND 3.SQUARE) default is BUIT
                mPaint.setStrokeCap(Paint.Cap.ROUND);
                // if set "0", will be a line
                mPaint.setStrokeWidth(12);
                mPaint.setColor(Color.GREEN);
            }
    
            protected void onDraw(Canvas canvas) {
                // set background color
                canvas.drawColor(Color.WHITE);
                // draw the specified bitmap
                canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
                // the path will be filled or framed based on the Style in the paint
                canvas.drawPath(mPath, mPaint);
            }
 
            public boolean onTouchEvent(MotionEvent event) {
                new_x = event.getX();
                new_y = event.getY();
      
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    // save the previous path, then reset the path
                    mPath.reset();
                    // start position
                    mPath.moveTo(new_x, new_y);
                    old_x = new_x;
                    old_y = new_y;
                }
                else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                    float dx = Math.abs(new_x - old_x);
                    float dy = Math.abs(new_y - old_y);
          
                    if (dx >= 4 || dy >= 4) {
                        // set path from (old_x, old_y) to ((new_x + old_x)/2, (new_y + old_y)/2)
                        mPath.quadTo(old_x, old_y, (new_x + old_x)/2, (new_y + old_y)/2);
                        old_x = new_x;
                        old_y = new_y;
                    }
                }
                else if (event.getAction() == MotionEvent.ACTION_UP) {
                    // unknown, this line like no used
                    mPath.lineTo(old_x, old_y);
                    // paint the path when "ACTION_UP"
                    mCanvas.drawPath(mPath, mPaint);
                    // save the paint
                    mPath.reset();
                }
      
                // update the paint, it will recall the onDraw function
                invalidate();
                return true;
            }
        }
    }