All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hadoopz.MyDroidLib.app.MyApplication Maven / Gradle / Ivy

There is a newer version: 1.0.62
Show newest version
/*
 * 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.DefaultLogUtil;
import com.hadoopz.MyDroidLib.util.JStringKit;

/**
 * Created by jw362j on 12/30/2016.
 */
public abstract class MyApplication extends Application {

    private PendingIntent pi;
    private AlarmManager alarmManager;

    @Override
    public void onCreate() {
        super.onCreate();
        initApp();
    }

    private void initApp() {
        initExceptionHandler();
        initRTCTimer();
    }

    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;
        DefaultLogUtil.getInstance().e("MyApplication", "the length of initRTC is:" + RTCs.length);
        for (String item : RTCs) {
            DefaultLogUtil.getInstance().e("MyApplication", "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 {
            DefaultLogUtil.getInstance().e("MyApplication", "no alarm service available...");
        }
    }

    protected void stopMe() {
        if (alarmManager != null) {
            if (pi != null) {
                alarmManager.cancel(pi);
                pi = null;
            }
            alarmManager = null;
        }

    }

    public abstract BaseExceptionHandler getDefaultUncaughtExceptionHandler();

    public abstract String[] initRTC();//返回的第一个参数代表广播的action 第二个代表间隔时长
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy