This is a migrated thread and some comments may be shown as answers.

How to run cordova plugin in Android background service?

2 Answers 780 Views
Apache Cordova
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
mehsen
Top achievements
Rank 1
mehsen asked on 10 Jul 2014, 02:28 PM
I am working on mobile application developed on [cordova][1] . I want to implement a background service that do some work like open socket connection syncronise local database with remote one and notify the users on new remote pushes etc . The point is I have this code implemented in javascript but I want execute it i background.

 
I searched internet for a cordova background service plugin.

   - katzer/cordova-plugin-background-mode
   - jocull/phonegap-backgroundjs
   - [red-folder]

 The best one I think is [red-folder] but it is just for android and it does not let me to write javascript to be executed in background. but just exchange json between java and javascript.

I have read some topics about background service in android these are useful ones I found:


   - [create-a-service-on-android-with-phonegap-application]
   - [simple-android-service-example-code-description-start-stop-service]
   - [android-using-webview-outside-an-activity-context]

So I started writing cordova plugin (primarily on android) to execute the javascript code in background. I created a webview from the background service to execute the javascript from it. This works fine when I execute normal javascript but when it comes to cordova plugins js it fails for example the device `device.uuid` gives `null`.



This is the java service  code:


public void onStart(Intent intent, int startId) {
          Toast.makeText(this, "My Happy Service Started", Toast.LENGTH_LONG).show();
       
               createBackGroundView();
               super.onStart(intent,startId);
   }
 
 
          public void createBackGroundView(){
     
              
       WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
      LayoutParams params = new WindowManager.LayoutParams(
                       android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                       android.view.ViewGroup.LayoutParams.WRAP_CONTENT,
                       WindowManager.LayoutParams.TYPE_PHONE,
                       WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
                       PixelFormat.TRANSLUCENT
               );
              
       params.gravity = Gravity.TOP | Gravity.LEFT;
       params.x = 0;
       params.y = 0;
       params.width = 200;
       params.height = 200;
       
       LinearLayout view = new LinearLayout(this);
                
       view.setLayoutParams(new RelativeLayout.LayoutParams(
                        android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                        android.view.ViewGroup.LayoutParams.MATCH_PARENT
                ));
                
       WebView wv = new WebView(this);
       wv.setLayoutParams(new LinearLayout.LayoutParams(
                        android.view.ViewGroup.LayoutParams.MATCH_PARENT,
                        android.view.ViewGroup.LayoutParams.MATCH_PARENT
                ));    
       view.addView(wv);
       wv.getSettings().setJavaScriptEnabled(true);
       wv.setWebChromeClient(new WebChromeClient());
                wv.loadUrl("file:///android_asset/www/background.html");
       wv.setWebViewClient(new WebViewClient() {
  
@Override
public void onReceivedError(final WebView view, int errorCode,
String description, final String failingUrl) {
Log.d("Error","loading web view");
super.onReceivedError(view, errorCode, description, failingUrl);
}
});
 
       windowManager.addView(view, params);
       
         }

         

There is no error in the logcat.
So I tried to write the device object on the screen and that's what I get :
       
     
     
document.write(JSON.stringify(window.device))



And this is the result :


  { available : false, 
        plaform : null , 
        version : null , 
        uuid : null ,  
        cordova : null ,
        model : null 
       }

Any help about this problem ?



2 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 15 Jul 2014, 11:58 AM
Hi Mehsen,

Thank you for bringing this issue to our community.

Even as such inquiry is beyond the scope of our support services, I believe the AppBuilder forum is good place for such questions. Further, you can also try posting it on StackOverflow.

Regards,
Kaloyan
Telerik
 

Share what you think about AppBuilder and Telerik Platform with us in our feedback portal, so we can become even better!

 
0
Kaloyan
Telerik team
answered on 15 Jul 2014, 12:46 PM
Hi again Mehsen,

A colleague of mine noted that you mght need to use CordovaWebView instead of the default WebView in Android devices. Here is a good reference about this. If it works, here you will find the iOS one.

I hope it helps.

Regards,
Kaloyan
Telerik
 

Share what you think about AppBuilder and Telerik Platform with us in our feedback portal, so we can become even better!

 
Tags
Apache Cordova
Asked by
mehsen
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Share this question
or