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

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

The 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.TimeInterval;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import com.mycomm.IProtocol.log.UniversalLogHolder;

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

    private static final String ACTION_TIMER = "com.hadoopz.d33600715c5e79c155e268fbf99556tt.app";
    private PendingIntent pi;
    private AlarmManager alarmManager;
    private BroadcastReceiver receiver;

    @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() {
        TimeInterval interval = initRTC();
        if (interval == null || interval.getInterval() <= 0) {
            UniversalLogHolder.e(getClass().getSimpleName(), "the interval is invalid :"+ interval);
            return;
        }
        UniversalLogHolder.e(getClass().getSimpleName(), "the interval is:" + interval.getInterval());

        receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context cntxt, Intent intent) {
                UniversalLogHolder.e(getClass().getSimpleName(), "the timer event comes....");
                onTimer(cntxt);
            }
        };
        registerReceiver(receiver, new IntentFilter(ACTION_TIMER));
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(ACTION_TIMER);
        pi = PendingIntent.getBroadcast(this, 0, intent, 0);
        
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,0, interval.getInterval(), pi);

    }

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

    public abstract BaseExceptionHandler getDefaultUncaughtExceptionHandler();

    public abstract TimeInterval initRTC();
    
    public abstract void onTimer(Context context);
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy