2012-02-27 114 views
4

我已经在phonegap中为后台服务(在后台运行应用程序)创建了插件。我已经创建了使用android在后台运行应用程序的服务。然后从plugin启动服务。在index.html中,我将该插件称为button点击事件。当我点击按钮时,服务启动。 是否有可能使用android服务在后台运行javascript并从后台获取警报?怎么做? 现在我想在后台运行javascript.Is它可能如何获得后台alert.also需要调用wcf rest服务。如何在后台运行javascript?

请提前指导我。

我的index.html中

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

代码开始我的应用程序在运行background.I服务后,希望这个 “警报(” 你好 “);” “navigator.notification.alert(” 辞海“) ;” (ie)在我的.html文件中发现警报。但是你给android中的活动提供代码。它应该在后台运行javascript并显示alert.please引导我。提前感谢。我还有一个疑问服务还在后台运行我的wcf rest服务,并从该服务运行diaplay警报。请告诉我解决 我的服务代码:

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,歌曲播放,在模拟器按home键(背景后)。我想要显示像这样的警报如何做到这一点?

在我的插件

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

} 

我打电话开始服役。它显示emulator.the警报按下home键后,只在地面脱颖而出警报不来(从背景)后您的活动,请告诉我解决

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呼叫callNotificationFunction在前景显示警报。我点击模拟器中的主页按钮什么都没发生,警报不显示从背景,请提前引导我感谢。如果我打电话callNotificationFunction在单独的按钮中单击没有任何发生(警报不显示在前景中)并且logcat中没有错误

+0

什么时候你想给通知?当服务启动或通知是动态的时候? – 2012-02-27 07:38:59

+0

当我点击按钮我的服务开始。然后我应该得到前景和背景的警报。(在我的index.html警报)例如:警报(“你好”)和我的WCF休息服务也运行在background.please指南我。在此先感谢您 – Mercy 2012-02-27 08:07:52

回答

0

为了在背景中显示警报。创建一个作为对话框运行的活动。 例如当过你想显示一些通知,这样

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

在创建这样

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

} 

而且在manifest文件中使用这种

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

的活动,并呼吁本次活动订购通知使用

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

正如你所使用的服务插件,给你打电话使用呼叫notifications.And然后执行类似这样的

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

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

感谢您的答复。我已编辑我的帖子。请看到。请引导我。提前感谢 – Mercy 2012-02-27 09:12:30

+0

嘿听,再次创建一个插件,并调用我共享的活动显示notification.Its简单.. .. – 2012-02-27 09:33:49

+0

我怀疑你的回复。通过在android中使用服务已创建的aplugin运行应用程序在background.so如何在该插件中调用此(AppNotification)活动我已编辑我的帖子请参阅。引导我,在此先感谢 – Mercy 2012-02-27 09:54:50

0

可以使用一个服务,一个web视图和窗口管理器,在Android后台运行JavaScript的行动服务。看看这个帖子here