在xml文件的Preference标签中,用<extra>给<intent>标签加参数

在xml文件的Preference标签中,用<extra>给<Intent>标签加参数,以及<intent>标签的android:targetPackage和android:targetClass属性使用注意事项

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <Preference
        android:title="prefs_category_one"
        android:summary="prefs_summary_category_one">
        <!-- 这里android:targetPackage是应用程序的Context,而android:targetClass的路径在子包settings下 -->
        <!-- 如果把 android:targetPackage="com.demo.artshell.uidemo.settings" 运行时找不到Activity -->
        <intent
            android:action="prefs_category_action_ONE"
            android:targetPackage="com.demo.artshell.uidemo"
            android:targetClass="com.demo.artshell.uidemo.settings.SupportOldVersionAndReusedActivityOrFragment$ReusedActivity">
            <!-- 官网没有说明,但确实可以通过<extra>传附加信息 getIntent().getStringExtra("reused_key") -->
            <extra
                android:name="reused_key"
                android:value="reused_fragment_two"/>
        </intent>
    </Preference>

    <Preference
        android:title="prefs_category_two"
        android:summary="prefs_summary_category_two">
        <intent
            android:action="prefs_category_action_TWO"
            android:targetPackage="com.demo.artshell.uidemo"
            android:targetClass="com.demo.artshell.uidemo.settings.SupportOldVersionAndReusedActivityOrFragment$ReusedActivity"/>
    </Preference>
</PreferenceScreen>


转载于:https://my.oschina.net/artshell/blog/397132

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 Ehcache 的读取大型 CSV 文件并缓存到临时文件的示例代码: ```java import org.ehcache.Cache; import org.ehcache.CacheManager; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.impl.config.persistence.CacheManagerPersistenceConfiguration; import org.ehcache.impl.persistence.DefaultLocalPersistenceService; import org.ehcache.persistence.PersistentStorageFactory; import org.supercsv.cellprocessor.ParseBool; import org.supercsv.cellprocessor.ParseDate; import org.supercsv.cellprocessor.ParseInt; import org.supercsv.cellprocessor.ParseLong; import org.supercsv.cellprocessor.ParseBigDecimal; import org.supercsv.cellprocessor.Optional; import org.supercsv.cellprocessor.constraint.NotNull; import org.supercsv.cellprocessor.ift.CellProcessor; import org.supercsv.io.CsvBeanReader; import org.supercsv.io.ICsvBeanReader; import org.supercsv.prefs.CsvPreference; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class CsvCacheDemo { private static final String CACHE_NAME = "csv-cache"; private static final String CACHE_PATH = "temp/cache"; private static final String CSV_PATH = "data.csv"; public static void main(String[] args) throws IOException { // 创建缓存管理器 CacheManager cacheManager = createCacheManager(); // 从缓存获取数据,如果缓存不存在则从 CSV 文件读取并缓存 List<DemoDto> demoList = readFromCache(cacheManager); // 打印结果 demoList.forEach(System.out::println); // 关闭缓存管理器 cacheManager.close(); } private static CacheManager createCacheManager() { // 创建本地持久化服务 DefaultLocalPersistenceService persistenceService = new DefaultLocalPersistenceService( new CacheManagerPersistenceConfiguration(new File(CACHE_PATH))); // 创建缓存管理器 return CacheManagerBuilder.newCacheManagerBuilder() .using(persistenceService) .withCache(CACHE_NAME, CacheConfigurationBuilder.newCacheConfigurationBuilder( String.class, List.class, ResourcePoolsBuilder.heap(1)) .withExpiry(Expirations.timeToLiveExpiration(Duration.ofMinutes(10)))) .build(true); } private static List<DemoDto> readFromCache(CacheManager cacheManager) throws IOException { // 获取或创建缓存 Cache<String, List<DemoDto>> cache = cacheManager.getCache(CACHE_NAME, String.class, List.class); // 尝试从缓存读取数据 String key = CSV_PATH; List<DemoDto> demoList = cache.get(key); if (demoList == null) { // 缓存不存在,从 CSV 文件读取并缓存 demoList = readFromCsv(); cache.put(key, demoList); } return demoList; } private static List<DemoDto> readFromCsv() throws IOException { // 创建 CSV 读取器 FileReader fileReader = new FileReader(CSV_PATH); ICsvBeanReader beanReader = new CsvBeanReader(fileReader, CsvPreference.STANDARD_PREFERENCE); // 定义 CSV 列名和属性映射关系 final String[] header = beanReader.getHeader(true); final CellProcessor[] processors = new CellProcessor[] { new NotNull(), new ParseLong(), new Optional(new ParseInt()), new Optional(new ParseBigDecimal()), new Optional(new ParseDate("yyyy-MM-dd")), new Optional(new ParseBool())}; // 读取 CSV 数据并转换为 DTO 对象 List<DemoDto> demoList = new ArrayList<>(); DemoDto demoDto; while ((demoDto = beanReader.read(DemoDto.class, header, processors)) != null) { demoList.add(demoDto); } // 关闭 CSV 读取器 beanReader.close(); // 将数据写入临时文件 File tempFile = File.createTempFile("csv-cache", ".tmp"); FileWriter fileWriter = new FileWriter(tempFile); tempFile.deleteOnExit(); for (DemoDto dto : demoList) { fileWriter.write(dto.toString()); fileWriter.write(System.lineSeparator()); } fileWriter.close(); return demoList; } public static class DemoDto { private String name; private long id; private Integer age; private BigDecimal amount; private Date date; private Boolean flag; // getter 和 setter 省略 } } ``` 此示例代码使用 Ehcache 缓存管理器和本地持久化服务,将读取的 CSV 数据缓存到 `temp/cache` 目录下的临时文件,并以 `List<DemoDto>` 的形式返回给调用方。如果缓存存在数据,则直接从缓存读取;否则从 CSV 文件读取并缓存到缓存和临时文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值