How to run javascript in the background?

I created a plugin for the background service (to run the application in the background) in phonegap. I created a service to run the application in the background using android. Then start the service from plugin.In index.html I call this plugin in an event button. When I click the button, the service starts. Is it possible, using the android service, to run javascript in the background and get a warning from the background? How to do it? Now I want to run javascript in background.Is possible how to get a warning in background. I also need to call the wcf rest service.

please guide me in advance.

My code in Index.html

document.addEventListener("deviceready", onDeviceReady, false); function onDeviceReady() { alert("hello"); navigator.notification.alert("hai"); } 

after starting the service. My application runs in the background. I need this "alert (" hello ");" "navigator.notification.alert (" hai "); in the background. (ie) found in my .html file. But you give the code for activity in android.It should run javascript in the background and display a warning from of this. please forward me.thanks in advance. I have one more doubt about this, the service also starts my wcf service in the background and warns about the diaplet from this service. tell me the solution My service code:

 public class MyService extends Service { private static final String TAG = "MyService"; MediaPlayer player; @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show(); Log.d(TAG, "onCreate"); player = MediaPlayer.create(this, R.raw.braincandy); player.setLooping(false); // Set looping } @Override public void onDestroy() { Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show(); Log.d(TAG, "onDestroy"); player.stop(); } @Override public void onStart(Intent intent, int startid) { Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show(); Log.d(TAG, "onStart"); player.start(); Toast.makeText(this, "alert Started", Toast.LENGTH_LONG).show(); } } 

In this code, I will add one player.so after starting the .song service is played in the foreground, the song is played after pressing the home button in the emulator (background). I want to show a warning like this. How to do it?

In my plugin

 if (CALL_SERVICE_ACTION.equals(action)) { Log.d(TAG, "CALL_SERVICE_ACTION"); ctx.startService(new Intent(ctx, MyService.class)); Intent i = new Intent(ctx, AppNotification.class); ctx.startActivity(i); } 

I invoke your activity after starting the service. Shows a warning only at the leading edge after pressing the home button in the emulator. A warning does not come (from the background), please tell me the solution

In index.html

 function callServiceFunction() { window.plugins.BackgroundService.callService('callService', callServiceSuccessCallBack, callServiceFailCallBack); } function callServiceSuccessCallBack(e) { alert("Success"); callNotificationsFunction(); } function callServiceFailCallBack(f) { alert("Failure"); } function callNotificationsFunction() { window.plugins.BackgroundService.callNotifications('callNotifications', callNotificationsSuccessCallBack, callNotificationsFailCallBack); } function callNotificationsSuccessCallBack(e) { alert("Success"); } function callNotificationsFailCallBack(f) { alert("Failure"); } 

while I press the background button button created by .and It calls a callNotificationFunction from callervicesuccesscallback.it displays a warning in the foreground. After I press the home button in the emulator, nothing happens, the warning does not appear in the background, please help me in advance. If I call callNotificationFunction in a separate button, nothing happens (warning does not appear in the foreground) and errors in logcat

+4
source share
2 answers

To show a warning in the background. Create an action that starts as a dialog. For example, create this kind of activity

 public class AppNotification extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AlertDialog alertDialog = new AlertDialog.Builder(AppNotification.this) .create(); alertDialog.setTitle("Alert"); alertDialog.setMessage("Something"); alertDialog.setButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { finish(); } }); alertDialog.show(); } } 

Also in the manifest file use this

  <activity android:name=".AppNotification" android:theme="@android:style/Theme.Dialog" > </activity> 

And call this action when you want to show some notifications, such as

 Intent i = new Intent(context, AppNotification.class); context.startActivity(i); 

In order to provide notifications, use

 navigator.notification.alert("hai"); // For foreground window.plugins.BackgroundService.callNotifications('callNotifications', callNotificationsSuccessCallBack, callNotificationsFailCallBack); // For Background 

Just like you used a service plugin to call a service that uses call notifications. Then follow actions like this

 else if (CALL_NOTIFICATION_ACTION.equals(action)) { Log.d(TAG, "CALL_NOTIFICATION_ACTION"); Intent i = new Intent(ctx, AppNotification.class); context.startActivity(i); } 
0
source

You can run Javascript in the background on Android using the service, WebView and WindowManager. Mark this post here .

0
source

Source: https://habr.com/ru/post/1398551/


All Articles