用户点击home键后,把申请前用户、home

2023-09-12 07:58:27 作者:仗剑闯天下

我的应用是在运行模式[前景] ,然后用户点击home键,这使应用程序的背景并仍在运行] 。我有闹钟功能在我的应用程序,它会调用。我要的是,当我的闹钟响的时候,我想带着我的后台运行的应用前景和在它最后的状态。

 <应用
            机器人:名称=nl.ziggo.android.state.management.MainEPGApp
            机器人:图标=@可绘制/图标
            机器人:标签=@字符串/ APP_NAME
            机器人:largeHeap =真
            机器人:标志=@可绘制/ app_logo>
            <活动
                机器人:名称=闪屏
                机器人:标签=@字符串/ APP_NAME
                机器人:launchMode =singleTop
                机器人:screenOrientation =nosensor
                机器人:主题=@风格/ Theme.Sherlock>
                <意向滤光器>
                    <作用机器人:名称=android.intent.action.MAIN/>
                    <类机器人:名称=android.intent.category.LAUNCHER/>
                &所述; /意图滤光器>
            < /活性GT;
        <活动
                    机器人:名称=入门
                    机器人:configChanges =方向|屏幕尺寸
                    机器人:screenOrientation =幕后黑手
                    机器人:launchMode =singleTop
                    机器人:uiOptions =无
                    机器人:windowSoftInputMode =adjustPan/>
< /用途>
 

解决方案

 意向意图=新的意图(背景下,MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(意向);
 

您应该使用你的起始或根作为活动 MyRootActivity

iPad更新之后双击Home键找不到屏幕录制怎么办

这将使现有的任务推到前台,而无需实际创建一个新的活动。如果应用程序没有运行,它会创建 MyRootActivity 一个实例,并启动它。

修改

我添加 Intent.FLAG_ACTIVITY_SINGLE_TOP 来意图,使其真正发挥作用!

第二次修改

还有另一种方式来做到这一点。您可以模拟应用程序的启动相同的方式,Android的启动应用程序,当用户从可用的应用程序列表中选择它。如果用户启动一个已经运行的应用程序,Android的只是使现有任务的前景(这是你想要的)。像这样做:

 意向意图=新的意图(背景下,SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //你需要这个,如果启动
                                                //从服务的活性
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(意向);
 

My application is in running mode[foreground] and user clicks on home button, which puts application to background[and still running]. I have alarm functionality in my application which fires up. I want is when my alarm goes off i want to bring my background running application in foreground and from last state in which it was.

    <application
            android:name="nl.ziggo.android.state.management.MainEPGApp"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:largeHeap="true"
            android:logo="@drawable/app_logo" >
            <activity
                android:name=".SplashScreen"
                android:label="@string/app_name"
                android:launchMode="singleTop"
                android:screenOrientation="nosensor"
                android:theme="@style/Theme.Sherlock" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        <activity
                    android:name=".Starter"
                    android:configChanges="orientation|screenSize"
                    android:screenOrientation="behind"
                    android:launchMode="singleTop"
                    android:uiOptions="none"
                    android:windowSoftInputMode="adjustPan" />
</application>

解决方案

Intent intent = new Intent(context, MyRootActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

You should use your starting or root activity for MyRootActivity.

This will bring an existing task to the foreground without actually creating a new Activity. If your application is not running, it will create an instance of MyRootActivity and start it.

EDIT

I added Intent.FLAG_ACTIVITY_SINGLE_TOP to Intent to make it really work!

Second EDIT

There is another way to do this. You can simulate the "launching" of the app the same way that Android launches the app when the user selects it from the list of available apps. If the user starts an application that is already running, Android just brings the existing task to the foreground (which is what you want). Do it like this:

Intent intent = new Intent(context, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting
                                                //  the activity from a service
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);