是否有一个Android意向过滤器,每当用户切换到应用程序时就会触发



我对在Android应用程序中配置AndroidManifest.xml文件非常陌生,我正在构建的应用程序使用的是带有Javascript和HTML的Phonegap,而不是本机代码。

我在Javascript代码中有一些操作,每次用户"打开"应用程序时我都要触发这些操作。我发现,"开放"的概念比我最初理解的要多。如果用户打开应用程序,然后切换到另一个应用程序,再回到第一个应用程序时,第一个应用实际上仍在后台运行,因此它没有启动。我想把它描述为"切换"回第一个应用程序会更准确。

我的问题是,每当用户切换到我的应用程序时,无论是第一次打开还是在后台运行,我都会运行一些Javascript。我不需要做任何特定的配置就可以实现这一点,这似乎是默认的行为。

然而,我需要执行的一些操作是基于AndroidManifest.xml中的设置,但如果应用程序是第一次打开,它们就会执行,如果用户切换回当前在后台运行的应用程序,则不会。具体来说,我想根据用户是否从电子邮件中的链接打开应用程序来执行操作,为此我设置了<intent-filter>

当用户从电子邮件中的链接启动我的应用程序时,无论该应用程序是否已经在后台运行,我有没有一种方法可以监听?

我认为这可能是相关的,所以这里是我的AndroidManifest.xml中的<activity>标签,它"监听"通过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>

这是我的MainActivity.java文件中的onCreate()函数:

@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); 
}

将其放在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
    }
}

相关内容

最新更新