如何在后台运行JavaScript?

时间:2012-02-27 07:19:50

标签: javascript android cordova

我已经在phonegap中创建了后台服务插件(在后台运行应用程序)。我已经创建了使用android在后台运行应用程序的服务。然后从插件启动服务。在index.html中我调用了该插件按钮单击事件。当我单击按钮时,服务启动。     有可能使用Android服务在后台运行javascript并从后台获取警报吗?怎么做?     现在我想在后台运行javascript。如何在后台获取警报也是可能的。还需要调用wcf休息服务。

请提前告诉我。谢谢。

我在Index.html中的代码

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
alert("hello");
navigator.notification.alert("hai");
}
启动服务后

我的应用程序在后台运行。我希望这个“alert(”hello“);”“navigator.notification.alert(”hai“);”在背景中。(即)在我的.html文件中找到警报。但是你给android中的活动代码。它应该在后台运行javascript并显示来自那个的警告。请指导我。谢谢你。我还有一个疑问这个服务还在后台和该服务的diaplay警报中运行我的wcf休息服务。请告诉我解决方案 我的服务代码:

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

在这段代码中我添加了一个player.so启动服务后.song在前台播放,播放歌曲,在模拟器(背景)中按下主页按钮后播放。我想显示这样的警告怎么办?

在我的插件中

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

}

我在启动服务后打电话给你的活动它在模拟器中按下主页按钮后只在前面显示警报。警报未来(来自后台)请告诉我解决方案

在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");
}

当我点击后台按钮服务created.and它从callservicesuccesscallback.it调用callNotificationFit在前景中显示警告。之后我点击模拟器中的主页按钮什么都没发生,警告没有显示背景,请提前指导我。谢谢我在单独的按钮中调用callNotificationFunction单击没有发生任何事情(警报未显示在前台)并且logcat中没有错误

2 个答案:

答案 0 :(得分:0)

为了在您的背景中显示警报。创建一个作为对话框运行的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();
}

}

同样在清单文件中使用此

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

如果您想要显示此类通知

,请调用此活动
Intent i = new Intent(context, AppNotification.class);
context.startActivity(i);

为了给出通知,请使用

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

正如您使用服务插件来调用服务一样,您使用了呼叫通知。然后执行此类操作

else if (CALL_NOTIFICATION_ACTION.equals(action)) {
Log.d(TAG, "CALL_NOTIFICATION_ACTION");

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

答案 1 :(得分:0)

您可以使用Service,WebView和WindowManager在Android后台运行Javascript。看看这篇文章here

相关问题