本文介绍了不必要的方法获取调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有被延长,每隔活性BaseActivity。关键是,我有音乐静音每当用户离开(的onPause)的活性。我也停止监听电话。 的问题是,的onPause 获取调用每次活动之间的用户切换,这意味着应用程序是不必要的静音和停止 telephonymanager ,即使它只能静音和停止 telephonymanager 如果用户要离开应用程序:I have a BaseActivity that gets extended by every other activity. The thing is, I have the music muted whenever the user leaves (onPause) the activity. I also stop listening for telephone calls. The problem is, onPause is getting called whenever the user switches between activities, meaning the app is unnecessarily muting and stopping telephonymanager, even though it should only be muting and stopping telephonymanager if the user were to leave the app.:@Override protected void onPause() { Log.v(TAG, "IN onPause!"); // unregister phone listener to telephony manager tManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE); mute(); super.onPause(); }现在说我的切换公共类MyClass的扩展BaseActivity 键,切换到公共类myOtherClass扩展BaseActivity 。此开关不必要的执行的onPause ,即使我只是想的onPause 被称为当用户离开时,应用程序。我该怎么办?Now say I switch between public class myClass extends BaseActivity and switch to public class myOtherClass extends BaseActivity. This switch is unnecessarily executing onPause, even though I only want onPause to be called when the user leaves the app. What should I do?感谢您的专家意见,富推荐答案从我的理解你的静音,而不是你的音乐游戏活动里写你的音乐BaseActivity中的onPause玩,From my understanding you are muting your music playing in onPause of BaseActivity, instead of that write it inside your Music play activity例如: public class BaseActivity extends AppCompatActivity{ @Override public void onPause(){ //do things that common for all activities } }public void MusicPlayActivity extends AppCompatActivity{ @Override public void onPause(){ music.mute() }}这将工作 更新有几种方法来检测后台应用程序是否正在运行,但其中只有一个是完全可靠的:你的应用程序自己使用 Activity.onPause , Activity.onResume 方法跟踪的知名度。商店知名度的地位在其它的类。There are few ways to detect whether your application is running in the background, but only one of them is completely reliable:Track visibility of your application by yourself using Activity.onPause, Activity.onResume methods. Store "visibility" status in some other class. 示例的 :实现自定义的应用程序类(注意isActivityVisible()静态方法):Example : Implement custom Application class (note the isActivityVisible() static method):公共类MyApplication的扩展应用{public class MyApplication extends Application { public static boolean isActivityVisible() { return activityVisible; } public static void activityResumed() { activityVisible = true; } public static void activityPaused() { activityVisible = false; } private static boolean activityVisible;}填写您的应用程序类在AndroidManifest.xml中:Register your application class in AndroidManifest.xml:<application android:name="your.app.package.MyApplication" android:icon="@drawable/icon" android:label="@string/app_name" >添加的onPause 和 onResume 在项目的每个活动(您可以创建一个共同的祖先为您的活动,如果你想,但如果你的活动是从MapActivity / ListActivity等已经扩展仍然需要手工编写以下):Add onPause and onResume to every Activity in the project (you may create a common ancestor for your Activities if you'd like to, but if your activity is already extended from MapActivity/ListActivity etc. you still need to write the following by hand):@Overrideprotected void onResume() { super.onResume(); MyApplication.activityResumed();}@Overrideprotected void onPause() { super.onPause(); MyApplication.activityPaused();} 中加入API级别14 ActivityLifecycleCallbacks(安卓4.0)。你可以用它们来跟踪你的应用程序的活动是否是当前可见的用户。检查玉米秆的回答下面的详细资料。ActivityLifecycleCallbacks were added in API level 14 (Android 4.0). You can use them to track whether an activity of your application is currently visible to the user. Check Cornstalks' answer below for the details. 这篇关于不必要的方法获取调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-20 09:20