三步曲:
在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 利用系统广播实现开机启动APP & 判断app在前台运行or后台运行全部内容,希望文章能够帮你解决Android 利用系统广播实现开机启动APP & 判断app在前台运行or后台运行所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)