
com.hadoopz.MyDroidLib.app.MyApplication Maven / Gradle / Ivy
/*
* Copyright 2017 jw362j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hadoopz.MyDroidLib.app;
/**
*
* @author jw362j
*/
import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.content.Intent;
import com.hadoopz.MyDroidLib.exceptions.BaseExceptionHandler;
import com.hadoopz.MyDroidLib.exceptions.LocalFileHandler;
import com.hadoopz.MyDroidLib.util.JStringKit;
import com.hadoopz.MyDroidLib.util.MyLogUtil;
/**
* Created by jw362j on 12/30/2016.
*/
public abstract class MyApplication extends Application {
private PendingIntent pi;
private AlarmManager alarmManager;
private AppInitinizer[] initinizers;
@Override
public void onCreate() {
super.onCreate();
initApp();
}
private void initApp() {
initExceptionHandler();
initRTCTimer();
initApplication();
}
private void initApplication() {
initinizers = getInitinizers();
if (initinizers == null || initinizers.length <= 0) {
return;
}
new Thread(new Runnable() {
public void run() {
for (AppInitinizer initinizer : initinizers) {
if (initinizer == null) {
continue;
}
try {
initinizer.onInitinize(MyApplication.this);
} catch (Exception e) {
MyLogUtil.LogMe("Exception in initinizers:"+e.getMessage());
}
}
}
}).start();
}
private void initExceptionHandler() {
if (getDefaultUncaughtExceptionHandler() == null) {
Thread.setDefaultUncaughtExceptionHandler(new LocalFileHandler(getApplicationContext()));
} else {
Thread.setDefaultUncaughtExceptionHandler(getDefaultUncaughtExceptionHandler());
}
}
private void initRTCTimer() {
String[] RTCs = initRTC();
if (RTCs == null) {
return;
}
int x = 0;
MyLogUtil.LogMe("the length of initRTC is:" + RTCs.length);
for (String item : RTCs) {
MyLogUtil.LogMe("the item(" + (x++) + ")is:" + item);
}
if (RTCs.length >= 2 && JStringKit.isNumber(RTCs[1])) {
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent();
intent.setAction(RTCs[0]);
pi = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
0, Long.valueOf(RTCs[1]), pi);
} else {
MyLogUtil.LogMe("no alarm service available...");
}
}
protected void stopMe() {
alarmManager.cancel(pi);
alarmManager = null;
pi = null;
}
public abstract BaseExceptionHandler getDefaultUncaughtExceptionHandler();
public abstract String[] initRTC();//返回的第一个参数代表广播的action 第二个代表间隔时长
public abstract AppInitinizer[] getInitinizers();
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy