안드로이드 개발 만보기

6410 단어 토대
1. 안드로이드 매니페스트에 있어야 합니다.xml에 권한 추가
    
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    <uses-feature android:name="android.hardware.sensor.accelerometer" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-feature
    android:name="android.hardware.sensor.stepcounter" android:required="true" />
    <uses-feature  android:name="android.hardware.sensor.stepdetector"android:required="true" />

2. 휴대전화가 계략을 지원하는지 검사
 /**
     *            
     *
     * @param context
     * @return
     */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    public static boolean isSupportStepCountSensor(Context context) {
        //            
        SensorManager sensorManager = (SensorManager) context
                .getSystemService(context.SENSOR_SERVICE);
        Sensor countSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
        Sensor detectorSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
        return countSensor != null || detectorSensor != null;
    }

3. 기능 사용

    private boolean isBind = false;
    private Messenger mGetReplyMessenger = new Messenger(new Handler(this));
    private Messenger messenger;

    /**
     *       
     */
    private void setupService() {
        Intent intent = new Intent(this, StepService.class);
        isBind = bindService(intent, conn, Context.BIND_AUTO_CREATE);
        startService(intent);


    }
    /**
     *  service       
     *
     * @param msg
     * @return
     */
    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case Constant.MSG_FROM_SERVER:
                cc.setCurrentCount(10000, msg.getData().getInt("step"));
                break;
        }
        return false;
    }


    /**
     *         (application Service)      interface,
     *           Service   context.bindService()    ,
     *               ,ServiceConnection               。
     */
    ServiceConnection conn = new ServiceConnection() {
        /**
         *      Service          ,  Android   IBind          。
         * @param name        Service    
         * @param service         IBind,    Service      
         */
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            try {
                messenger = new Messenger(service);
                Message msg = Message.obtain(null, Constant.MSG_FROM_CLIENT);
                msg.replyTo = mGetReplyMessenger;
                messenger.send(msg);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }

        /**
         *   Service                ,
         *          Service          Kill     ,
         *         Service   ,                onServiceConnected()。
         * @param name          
         */
        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

좋은 웹페이지 즐겨찾기