【问题标题】:How does the Android system tell an application that this app has gone to the background (invisible)?Android 系统如何告诉应用程序该应用程序已进入后台(不可见)?
【发布时间】:2015-07-03 20:32:41
【问题描述】:

我正在尝试制作一个 Xposed 模块 来挂钩 Android 系统的功能,它告诉应用程序 A,它已进入后台。 换句话说,这里有一个例子: 我们正在使用应用程序A,然后我们按下主页按钮,或者进入另一个应用程序。系统会向我们的应用程序A发送一条消息,让它知道焦点已经改变,现在它在后台(用户再也看不到它了)。系统这样做是为了让应用程序可以运行 onPause() 方法等等。

我在 XDA 上的 Xposed 部分发布了帖子,尽管我能够缩小范围,但我无法解决它。 LINK

我也一直在 github 和 grepcode 的 Android 存储库中进行大量搜索,但一直无法找到执行此操作的方法。我有点失落。 这些是我得到的链接:

  1. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/app/Instrumentation.java#1234
  2. http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/app/Activity.java#5346
  3. https://github.com/CyanogenMod/android_frameworks_base/blob/19a2266fed147ae051ba2df74f755cd7427c6eaa/docs/html/guide/topics/ui/ui-events.jd

谢谢,非常感谢任何帮助或建议。

【问题讨论】:

    标签: java android android-activity system xposed-framework


    【解决方案1】:

    是的,这是可能的,而且很有意义。但是,例如,它需要做很多事情。

    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)。显然,您必须制作没有启动器模式的应用程序,因为它是第一个活动,然后将第二个活动称为服务而不是活动。

    所以基本上你必须创建这样的东西。

    public class AppService extends WakefulIntentService{
       // your stuff goes here
    

    }

    从你的 mainActivity 调用服务时,像这样定义它。

    Intent intent = new Intent(MainActivity.this, AppService.class);
    

    startService(意图); 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)。然后在清单中定义此服务,如下所示。

    <service android:name=".AppService" >
        </service>
    

    【讨论】:

    • 嗨 Meghal,你误会了;我不想创建一个应用程序,然后是它的服务,所以它在后台运行。我想做的是一个 Xposed 模块,它所做的只是连接到系统方法,告诉应用程序何时进入后台并在其中注入代码(可能只是一个简单的 XC_MethodReplacement),因此 X 应用程序不会收到该消息,他们会继续运行,就好像他们在前台一样。如果您需要进一步解释,请查看我发布的 XDA 链接。谢谢
    • m 对不起,我忘记了那个帖子。 .Will Find Time 会尝试完美回答:24 小时内
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多