Android基础--简单介绍Application
来源:互联网 发布:java split函数 多个 编辑:程序博客网 时间:2024/06/07 00:32
在项目开发中,我们经常会写一个BaseApplication这个类,然后继承Application,在这个里面我们经常会做哪些事情呢?
首先:
private static BaseApplication application; @Override public void onCreate() { super.onCreate(); application = this; }
这个里面onCreate(),我们指明application对象,还有其他的一些初始化都在这个里面,比如第三方的加载图片ImageLoader,极光推送等等初始化。
第二:获取版本号
public static String getVersion() { PackageManager packageManager = getApplication().getPackageManager(); PackageInfo packInfo; try { packInfo = packageManager.getPackageInfo(getApplication().getPackageName(), 0); String version = packInfo.versionName; return version; } catch (NameNotFoundException e) { return ""; } }
第三个:轮询服务
// 开启轮询服务 public static void startPushService(Context context, int seconds, Class<?> cls, String action) { // 获取AlarmManager系统服务 AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); // 包装需要执行Service的Intent Intent intent = new Intent(context, cls); intent.setAction(action); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // 触发服务的起始时间 long triggerAtTime = SystemClock.elapsedRealtime(); // 使用AlarmManger的setRepeating方法设置定期执行的时间间隔(seconds秒)和需要执行的Service manager.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime, seconds * 1000, pendingIntent); } // 停止轮询服务 public static void stopPushService(Context context, Class<?> cls, String action) { AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, cls); intent.setAction(action); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); // 取消正在执行的服务 manager.cancel(pendingIntent); }
0 0
- Android基础--简单介绍Application
- Android 使用 Application 简单介绍
- 【Application】Android Application 对象介绍
- Android Application 对象介绍
- Android Application 对象介绍
- Android Application 对象介绍
- Android Application 对象介绍
- Android Application 对象介绍
- Android Application 对象介绍
- Android Application 对象介绍
- Android Application 对象介绍
- Android Application 介绍
- Android Application 对象介绍
- Android Application 对象介绍
- Android Application 对象介绍
- Android Application 对象介绍
- Android Application 对象介绍
- Android Application 对象介绍
- hibernate进阶之路之多对多映射(五)
- iOS Provisioning Profile总莫名其妙失效问题
- Object.keys的兼容性和用法
- 子线程操作malloc内存
- VC6.0蕉HOOK_timeGetTime 达到变速效果
- Android基础--简单介绍Application
- 数据库损坏解决:数据库已损坏,无法分配空间
- 项目中tld文件使用
- Android 拦截Home键的常用方法
- 动态库和静态库的区别
- 小白进阶之贪心算法VS动态规划
- java jar 打包命令
- hdu4940Destroy Transportation system【无源汇点有上下流限制的网络流】2014多校联合
- redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resourc