Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

java - How to resume Android Activity programmatically from background

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...