Android SharedPreferences使用

使用SharedPreferences设置摄像头默认像素

package com.android.camera;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.hardware.Camera.CameraInfo;
import android.util.Log;
import android.content.SharedPreferences;
import android.os.SystemProperties;

// We want to disable camera-related activities if there is no camera. This
// receiver runs when BOOT_COMPLETED intent is received. After running once
// this receiver will be disabled, so it will not run again.

public class ResetCameraSettingsReceiver extends BroadcastReceiver {

    private ComboPreferences mPreferences;

    String str_16M = "1280x960";
    String str_20M = "1600x1200";
    String str_24M = "2560x1920";
    
    @Override
    public void onReceive(Context context, Intent intent) {
        mPreferences = new ComboPreferences(context);
        if(SystemProperties.get("persist.sys.selcusanim").equals("27")) {
            SharedPreferences cameraPref  = mPreferences.getSharedPreference(context, 0);
            SharedPreferences.Editor editor = cameraPref.edit();
            editor.putString("pref_camera_picturesize_ratio_key", "1.3333");
            editor.putString("pref_camera_picturesize_key", str_16M);
            editor.apply();

            SharedPreferences cameraPref1  = mPreferences.getSharedPreference(context, 1);
            SharedPreferences.Editor editor1 = cameraPref1.edit();
            editor1.putString("pref_camera_picturesize_ratio_key", "1.3333");
            editor1.putString("pref_camera_picturesize_key", str_20M);
            editor1.apply();
        } else {
            SharedPreferences cameraPref  = mPreferences.getSharedPreference(context, 0);
            SharedPreferences.Editor editor = cameraPref.edit();
            editor.putString("pref_camera_picturesize_ratio_key", "1.3333");
            editor.putString("pref_camera_picturesize_key", str_20M);
            editor.apply();

            SharedPreferences cameraPref1  = mPreferences.getSharedPreference(context, 1);
            SharedPreferences.Editor editor1 = cameraPref1.edit();
            editor1.putString("pref_camera_picturesize_ratio_key", "1.3333");
            editor1.putString("pref_camera_picturesize_key", str_24M);
            editor1.apply();
        }
    }
}

 

2.读取SharedPreferences中的值

SharedPreferences cameraPref1  = mPreferences.getSharedPreference(context, 1);
String ratio=cameraPref1.getString("pref_camera_picturesize_ratio_key"."1.3333");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值