2012-02-27 32 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