1

I'm very new to configuring the AndroidManifest.xml file in an Android app, and the app I'm building is using Phonegap with Javascript and HTML, not native code.

I have some actions in my Javascript code that I want to trigger every time the user "opens" the app. What I've discovered is that the concept of "open" has more to it than I first understood. If the user opens the app, and then switches to another app, and then comes back to the first app, the first app has actually still been running in the background, so it's not starting up. I guess it would be more accurate to describe that as "switching" back to the first app.

My issue is that I have some Javascript that runs every single time the user switches to my app, whether opening it for the first time or if it had been running in the background. I didn't have to do any particular configuration to make that happen, it seems to be the default behavior.

However, some actions I need to execute are based on settings in the AndroidManifest.xml, but they ony execute if the app is being opened for the first time, not if the user is switching back to an app currently running in the background. Specifically, I want to execute actions based on whether or not the user opens the app from a link in an email, for which I set up an <intent-filter>.

Is there a way I can listen for when the user has launched my app from a link in an email, regardless of whether or not the app is already running in the background?

I think it might be relevant, so here is my <activity> tag in my AndroidManifest.xml that "listens" for the app being launched via URL:

    <activity
        android:name="com.xxxxxxx.xxxxxxx.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />

            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />

            <data
                android:host="xxxxxxx.com"
                android:scheme="http" />
            <data
                android:host="xxxxxxx.com"
                android:scheme="https" />
        </intent-filter>
    </activity>

Here is the the onCreate() function in my MainActivity.java file:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setIntegerProperty("splashscreen", R.drawable.splash);
    super.loadUrl(Config.getStartUrl(), 3000);

    adView = new AdView(this, AdSize.BANNER, AdMob_Ad_Unit);
        LinearLayout layout = super.root;
        layout.addView(adView);
        AdRequest request = new AdRequest();
        adView.loadAd(request); 
}
4

1 回答 1

1

把它放在你的 Launcher 活动中:

//this method is called every time the Activity is Created or Re-Created
//we check for null to see if the activity was only Created instead of Recreated
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState == null){ 
        myMethod(getIntent());
    }    
}

//This method will be called only when the Activity is already created and receives
//a new Intent
@Override
protected void onNewIntent(Intent it){
    super.onNewIntent(it);
    myMethod(it)
}

private void myMethod(Intent intent){
    if(intent.getAction().equals("put here the WebIntent action string or URL as they call it"){
        //your code here
    }
}
于 2013-09-03T14:58:07.200 回答