Android 利用系统广播实现开机启动APP & 判断app在前台运行or后台运行

开机启动APP

三步曲:

  • 在AndroidManifest.xml里添加权限:
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
  • 在项目工程里面新建一个广播接收器,用来接收系统开机完成广播:
public class BootCompletedReceiver extends BroadcastReceiver {

    private static final String action_boot ="android.intent.action.BOOT_COMPLETED";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (action_boot.equals(intent.getAction())){
            Intent mBootIntent = new Intent(context, MainActivity.class);
            mBootIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(mBootIntent);
        }
    }

}
  • 最后一步,在AndroidManifest.xml里面注册这个广播:
<application
        ......
        ....>
        <activity .....>
            .....
        </activity>
        
        ......

        <receiver android:name=".broadcastReceiver.BootCompletedReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
                
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>
    </application>

如果需要隐藏app的标题,可以修改theme:

android:theme="@style/Theme.AppCompat.Light.NoActionBar"

如果需要强制app运行的时候使用横屏,可以在对应的Activity后面加上:android:screenOrientation="landscape"

判断app在前台运行or后台运行

通过实现Application里面的ActivityLifecycleCallbacks回调接口,可以拿到App所有Activity的生命周期回调,这是它的部分源码:

    public interface ActivityLifecycleCallbacks {
        void onActivityCreated(Activity activity, Bundle savedInstanceState);
        void onActivityStarted(Activity activity);
        void onActivityResumed(Activity activity);
        void onActivityPaused(Activity activity);
        void onActivityStopped(Activity activity);
        void onActivitySaveInstanceState(Activity activity, Bundle outState);
        void onActivityDestroyed(Activity activity);
    }

  • 新建一个AppFrontBackHelper,用来注册Application状态监听registerActivityLifecycleCallbacks,统计打开的Activity数量,如果数量从0变到1说明是从后台切到前台,反之,如果数量从1到0说明是从前台切到后台。最后预留一个OnAppStatusListener回调接口,用于别的程序传入app前台或者后台运行需要执行的业务逻辑。

贴上代码:

public class AppFrontBackHelper {

    private OnAppStatusListener mOnAppStatusListener;

    /**
     * 注册状态监听,仅在Application中使用
     * @param application
     * @param listener
     */
    public void register(Application application, OnAppStatusListener listener){
        mOnAppStatusListener = listener;
        application.registerActivityLifecycleCallbacks(activityLifecycleCallbacks);
    }

    public void unRegister(Application application){
        application.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks);
    }

    private Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks() {
        //打开的Activity数量
        private int activityStartCount = 0;

        @Override
        public void onActivityCreated(Activity activity, Bundle savedInstanceState) {

        }

        @Override
        public void onActivityStarted(Activity activity) {
            activityStartCount++;
            //数量从0变到1说明是从后台切到前台
            if (activityStartCount == 1){
                //从后台切到前台
                if(mOnAppStatusListener != null){
                    mOnAppStatusListener.onFront();
                }
            }
        }

        @Override
        public void onActivityResumed(Activity activity) {

        }

        @Override
        public void onActivityPaused(Activity activity) {

        }

        @Override
        public void onActivityStopped(Activity activity) {
            activityStartCount--;
            //数量从1到0说明是从前台切到后台
            if (activityStartCount == 0){
                //从前台切到后台
                if(mOnAppStatusListener != null){
                    mOnAppStatusListener.onBack();
                }
            }
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle outState) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {

        }
    };

    public interface OnAppStatusListener{
        void onFront();
        void onBack();
    }

}
  • 新建一个继承Application的子类,并且通过AppFrontBackHelper注册自身的运行状态,此时再传入需要实现的业务,比如如果APP在前台运行,则隐藏导航栏、状态栏,反之则显示导航栏、状态栏。

贴上代码:

public class MyApp extends Application
{
    private AppFrontBackHelper helper;

    @Override
    public void onCreate() {
        super.onCreate();
        helper = new AppFrontBackHelper();
        helper.register(MyApp.this, new AppFrontBackHelper.OnAppStatusListener() {
            @Override
            public void onFront() {
                //隐藏导航栏、状态栏
                Intent intent = new Intent();
                intent.setAction("hide.systemui");
                sendBroadcast(intent);
            }

            @Override
            public void onBack() {
                //显示导航栏、状态栏
                Intent intent = new Intent();
                intent.setAction("show.systemui");
                sendBroadcast(intent);
            }
        });
    }

    @Override
    public void onTerminate() {
        super.onTerminate();
        helper.unRegister(MyApp.this);
    }
}

  • 最后只需要在AndroidManifest.xml里面添加上这个子类:
<application
        android:name=".MyApp"
        .......
    </application>

你可能感兴趣的:(android)