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
286 views
in Technique[技术] by (71.8m points)

android - Phonegap - Bring from background to foreground

I'm developing my team along with an application that must run in the background when an event called by sockets should put the application in the foreground .

The application must come to foreground similar to viber or whatsapp call. I stopped at this point. My application can now call an audio and vibrate, but I have to draw the screen to the foreground.

I'm using version 5.1.1 phonegap.

I am this plugin: https://github.com/katzer/cordova-plugin-background-mode

Could someone give me a hand? Very grateful this already.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

I found a way! Using the "toForeground" plugin. https://github.com/caioladislau/cordova-toforeground

      cordova.plugins.backgroundMode.enable();

      cordova.plugins.backgroundMode.onactivate = function() {
        setTimeout(function(){ 
          toForeground("MainActivity", "com.me.myapp", function() {
            navigator.notification.vibrate(1000);
          }, function(){
            navigator.notification.vibrate(5000);
          }); 
        }, 4000);
      };

Note where it is called in:

toForeground(mainClassName, packageName, successFunction, errorFunction);

To find the "mainClassName" and "packageName" I searched: platforms/android/src/com/me/myapp/MainActivity.java, and I found:

package com.me.myapp;

import android.os.Bundle;
import org.apache.cordova.*;

public class MainActivity extends CordovaActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // Set by <content src="index.html" /> in config.xml
        loadUrl(launchUrl);
    }
}

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

...