如何以编程方式恢复安卓活动与背景 [英] How to resume Android Activity programmatically from background

查看:98
本文介绍了如何以编程方式恢复安卓活动与背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:

  1. 比方说,我有目前正在启动的应用程序的活动一。
  2. 在经过​​一段时间后,我pressing主页按钮。应用程序A被切换到后台。
  3. 在这个时候,我开始使用其他应用程序。B - YouTube上,例如或等
  4. 有事(不要紧,在这种情况下,假设计时器完成计算时间)在当前最小化到后台的应用程序中的。
  5. 在事件的发生,应用程序中的活动自动从后台恢复。

问:

如何完成第5步?基本上我需要知道如何从后台编程恢复应用程序。

我尝试启动意图重新启动我的应用程序的活动,但它并没有奏效:

 意向意图=新的意图(背景下,MainActivity.class);
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            context.startActivity(意向);
 

我的清单文件:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.taxti
    安卓版code =43
    机器人:VERSIONNAME =1.5.3>

    <用途-SDK
        安卓的minSdkVersion =13
        机器人:targetSdkVersion =21/>

    <许可
        机器人:名称=com.example.taxti.permission.MAPS_RECEIVE
        安卓的ProtectionLevel =签名/>

    <用途特征
        机器人:glEsVersion =0x00020000
        机器人:要求=真/>

    <使用-权限的Andr​​oid:名称=com.example.taxti.permission.MAPS_RECEIVE/>
    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
    <使用-权限的Andr​​oid:名称=android.permission.GET_ACCOUNTS/>
    <使用-权限的Andr​​oid:名称=android.permission.WAKE_LOCK/>
    <使用-权限的Andr​​oid:名称=com.google.android.c2dm.permission.RECEIVE/>
    <允许机器人:名称=com.taxti.permission.C2D_MESSAGE
                安卓的ProtectionLevel =签名/>
    <使用-权限的Andr​​oid:名称=com.taxti.permission.C2D_MESSAGE/>
    <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
    <使用-权限的Andr​​oid:名称=com.google.android.providers.gsf.permission.READ_GSERVICES/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_LOCATION/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_FINE_LOCATION/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
    <使用-权限的Andr​​oid:名称=android.permission.CALL_PHONE/>
    <使用-权限的Andr​​oid:名称=android.permission.EXPAND_STATUS_BAR/>
    <使用-权限的Andr​​oid:名称=android.permission.CLEAR_APP_CACHE/>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:hardwareAccelerated =真
        机器人:标签=@字符串/ APP_NAME
        机器人:名称=com.taxti.Globals
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名称=com.taxti.InitialActivity
            机器人:标签=@字符串/ APP_NAME
            机器人:windowSoftInputMode =adjustPan
            机器人:screenOrientation =sensorLandscape
            机器人:主题=@风格/ MyTheme的>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动
            机器人:名称=com.taxti.MainActivity
            机器人:标签=@字符串/ APP_NAME
            机器人:launchMode =singleTop
            机器人:windowSoftInputMode =adjustPan
            机器人:screenOrientation =sensorLandscape
            机器人:主题=@风格/ MyTheme的>
        < /活性GT;

        <服务机器人:名称=。MainActivityForegroundService/>

        <意向滤光器>
                <作用机器人:名称=android.net`enter code here`.conn.CONNECTIVITY_CHANGE/>
        &所述; /意图滤光器>

       <意向滤光器>
            <作用机器人:名称=android.intent.action.CALL_PRIVILEGED/>
            <类机器人:名称=android.intent.category.DEFAULT/>
            <作用机器人:名称=android.intent.action.DIAL/>
            <作用机器人:名称=android.intent.action.CALL_BUTTON/>
            <类机器人:名称=android.intent.category.BROWSABLE/>
            <数据机器人:计划=电话/>
       &所述; /意图滤光器>

        &所述;元数据
            机器人:名称=com.google.android.gms.version
            机器人:值=@整数/ google_play_services_version/>
        &所述;元数据
            机器人:名称=com.google.android.maps.v2.API_KEY
            机器人:值=XXXX/>
        <使用库机器人:名称=com.google.android.maps/>

        <接收器
            机器人:名称=。GcmBroadcastReceiver
            机器人:权限=com.google.android.c2dm.permission.SEND>
            <意向滤光器>
                <作用机器人:名称=com.google.android.c2dm.intent.RECEIVE/>
                <类机器人:名称=com.taxti/>
            &所述; /意图滤光器>

        < /接收器>

    < /用途>

< /舱单>
 

解决方案

为了使你的应用推到前台,你必须调用 startActivity()从另一个上下文(无论是服务的BroadcastReceiver )。只需调用 startActivity()活动中的不会使你的应用推到前台。

您不需要在你意图,但你需要设置 Intent.FLAG_ACTIVITY_NEW_TASK 。

Situation:

  1. Let's say I have currently working launched application Activity A.
  2. After some time I am pressing "Home" button. Application A goes to background.
  3. At this time, I am starting to use another app B - youtube for example or etc.
  4. Something happens (doesn't matter what in this context, let's say timer finished calculating time) in the application A which currently is minimized to background.
  5. On the event occurrence, application A activity automatically resumes from background.

Question:

How to accomplish step 5? Basically I need to know how to resume application from background programmatically.

I tried to launch intent to "restart" my application activity but it didn't worked:

Intent intent = new Intent(context, MainActivity.class);
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            context.startActivity(intent);

My manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.taxti"
    android:versionCode="43"
    android:versionName="1.5.3" >     

    <uses-sdk
        android:minSdkVersion="13"
        android:targetSdkVersion="21" />

    <permission
        android:name="com.example.taxti.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="com.example.taxti.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />       
    <permission android:name="com.taxti.permission.C2D_MESSAGE"
                android:protectionLevel="signature" />
    <uses-permission android:name="com.taxti.permission.C2D_MESSAGE" />     
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />   
    <uses-permission android:name="android.permission.CALL_PHONE" />  
    <uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
    <uses-permission android:name="android.permission.CLEAR_APP_CACHE" />

    <application
        android:allowBackup="true"        
        android:icon="@drawable/ic_launcher"
        android:hardwareAccelerated="true"
        android:label="@string/app_name"
        android:name="com.taxti.Globals"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.taxti.InitialActivity"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustPan"
            android:screenOrientation="sensorLandscape"
            android:theme="@style/MyTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.taxti.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTop"
            android:windowSoftInputMode="adjustPan"
            android:screenOrientation="sensorLandscape"
            android:theme="@style/MyTheme">
        </activity>

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

        <intent-filter>
                <action android:name="android.net`enter code here`.conn.CONNECTIVITY_CHANGE" />                
        </intent-filter>

       <intent-filter>
            <action android:name="android.intent.action.CALL_PRIVILEGED" />
            <category android:name="android.intent.category.DEFAULT" />     
            <action android:name="android.intent.action.DIAL" />
            <action android:name="android.intent.action.CALL_BUTTON" />     
            <category android:name="android.intent.category.BROWSABLE" />           
            <data android:scheme="tel" />
       </intent-filter>

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />
        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="xxxx" />             
        <uses-library android:name="com.google.android.maps" />

        <receiver
            android:name=".GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND" >   
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />               
                <category android:name="com.taxti" />
            </intent-filter>

        </receiver>

    </application>

</manifest>

解决方案

In order to bring your app to the foreground, you must call startActivity() from another context (either a Service or a BroadcastReceiver). Just calling startActivity() from within an Activity won't bring your app to the foreground.

You don't need the ACTION and CATEGORY in your Intent, but you do need to set Intent.FLAG_ACTIVITY_NEW_TASK.

这篇关于如何以编程方式恢复安卓活动与背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆