我正在尝试制作一个Xposed模块来 Hook Android系统的功能,它告诉应用程序A它已经进入后台。 换句话说,这是一个例子: 我们正在使用应用程序A,然后我们按主页按钮,或者进入另一个应用程序。系统会向我们的应用程序A发送一条消息,让它知道焦点已经改变,现在它在后台了(用户再也看不到它了)。系统这样做是为了让应用程序可以运行 onPause() 方法等等。
我在 XDA 的 Xpose 部分发帖,尽管我能够缩小范围,但我无法解决它。 LINK
我也在 github 和 grepcode 的 Android 存储库中进行了很多搜索,但一直无法找到哪个方法可以做到这一点。我有点失落。 这些是我得到的链接:
- http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/app/Instrumentation.java#1234
- http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/app/Activity.java#5346
- https://github.com/CyanogenMod/android_frameworks_base/blob/19a2266fed147ae051ba2df74f755cd7427c6eaa/docs/html/guide/topics/ui/ui-events.jd
谢谢,非常感谢任何帮助或建议。
最佳答案
是的,这是可能的,而且很有意义。但这需要做很多事情,例如。
1).您需要将您的应用程序设置为开机启动,这意味着每当用户重新启动移动设备或设备时,您的应用程序就应该自动启动。
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name=".OnBootReceiver" >
<intent-filter
android:enabled="true"
android:exported="false" >
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
</receiver>
<receiver android:name=".OnGPSReceiver" >
</receiver>
2).显然,您必须使应用程序没有启动器模式,因为它是第一个 Activity ,然后将第二个 Activity 作为服务而不是 Activity 调用。
所以基本上你必须创建这样的东西。
public class AppService extends WakefulIntentService{
// your stuff goes here
}
当从你的 mainActivity 调用服务时,像这样定义它。
Intent intent = new Intent(MainActivity.this, AppService.class);
startService( Intent ); hideApp(getApplicationContext().getPackageName());
hideApp//在 mainActivity 之外使用它。
private void hideApp(String appPackage) {
ComponentName componentName = new ComponentName(appPackage, appPackage
+ ".MainActivity");
getPackageManager().setComponentEnabledSetting(componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP);
}
3).然后在 list 中定义该服务,如下所示。
<service android:name=".AppService" >
</service>
关于java - Android系统如何告诉应用程序该应用程序已进入后台(不可见)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31213876/