2012-07-10 49 views
1

我試圖讓我的服務在我的手機上有外撥電話時運行。但由於某種原因,我的服務在這種情況發生時無法運行。我知道「CallReceiver」的代碼會執行,因爲我使用了Toast消息來顯示它是否運行。我可以通過我的主要活動運行的服務,但這意味着無論撥出電話是否由它將運行....服務不從BroadcastReceiver運行? - Android

下面是我的代碼:

接收器:

package com.example.hiworld; 

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.widget.Toast; 

public class CallReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     context.startService(new Intent(context, CallService.class)); 

     Toast.makeText(context, "Call Receiver started", 
       Toast.LENGTH_LONG).show(); 

     Log.d("Calling Someone", "onReceived"); 

    } 


} 

的服務:

package com.example.hiworld; 


import android.app.IntentService; 
import android.content.Context; 
import android.content.Intent; 
import android.telephony.TelephonyManager; 
import android.widget.Toast; 

public class CallService extends IntentService { 

    public long StartTime=0; 
    public long EndTime =0; 
    public long TotalTime = 0; 
    public long NumFreeMins = 0; 


    public CallService() { 
      super("CallService"); 
     } 

    @Override 
    protected void onHandleIntent(Intent intent) { 


     StartTime = (System.currentTimeMillis())/60; 

     TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 




      if(tm.getCallState()==0) //getting the time whenever the phone is off 
      { 

       EndTime = (System.currentTimeMillis())/60; 
       TotalTime = EndTime-StartTime; 
       NumFreeMins = 300-TotalTime; 

       //notify user 
       this.displaymsg(); 

      } 



    } 

public void displaymsg() 
{ 
    Toast toast = Toast.makeText(getApplicationContext(), ""+NumFreeMins, Toast.LENGTH_SHORT); 
    toast.show(); 
} 
} 

我見過一些人使用日e行:代替

context.startService(new Intent(this, CallService.class)); 

context.startService(new Intent(context, CallService.class)); 

,但後者沒有工作對我來說...

+0

顯示您的日誌只是爲了瞭解啓動服務時是否有問題。我會將您的問題指向與標籤相關的清單中缺少的內容。 – yugidroid 2012-07-10 23:52:34

+0

查看本教程將幫助你[從廣播接收器開始服務](http://rdcworld-android.blogspot.in/2012/03/start-service-from-broadcast-receiver.html) – swiftBoy 2012-07-11 15:34:51

回答

0

嘗試

context.startService(new Intent(context.getApplicationContext(), CallService.class)); 
+0

嗯,仍然不能讓它工作。我現在嘗試過,但我仍然沒有得到任何東西。我甚至會在服務被調用時發出敬酒信息,並且永遠不會出現... – fouadalnoor 2012-07-10 23:19:28

+0

@SalilPandit:請參閱我的回答末尾有關「Toast」和「IntentService」的編輯。 – Squonk 2012-07-12 22:30:16

+0

很酷。我想我也學到了一些東西。謝謝! – 2012-07-13 00:53:48

1

嘗試爲您IntentService指定<intent-filter>在具體「行動」的清單中。例如...

<service 
    android:name=".CallService" > 
    <intent-filter> 
     <action android:name="com.example.hiworld.intent.DO_SOMETHING" /> 
    </intent-filter> 
</service> 
BroadcastReceiveronReceive(...)方法做類似下面

則...

Intent callReceiverIntent = new Intent("com.example.hiworld.intent.DO_SOMETHING"); 

// Put the Intent received by the BroadcastReceiver as extras so the 
// IntentService can process it... 
callReceiverIntent.putExtras(intent); 

context.startService(callReceiverIntent); 

編輯:

我建了一個簡單的測試應用基於您發佈到pasrebin的代碼。我爲清單,接收器和服務保留了相同的代碼,只需添加一個默認的Activity即可運行。

我可以在Eclipse的DDMS角度上說,CallReceiver成功接收NEW_OUTGOING_CALLIntent和呢,其實監控logcat中看到,啓動CallService

但是,試圖顯示的Toast會導致由於'泄漏的處理程序'導致異常並導致無提示崩潰的問題。

這背後的原因是,IntentService使用後臺線程來執行其工作,並試圖顯示Toast(即UI元素)從非UI線程將無法正常工作,因爲該應用程序沒有UI組件運行。因此,不管您是使用本地context變量還是使用getApplicationContext(),都沒有關聯Toast的UI上下文。

爲什麼它開始從ActivityCallService顯然是因爲Activity提供了可以由Toast使用的UI上下文時運作的原因。總之,一般看來,嘗試使用ToastIntentService是不是一個好主意,除非IntentService總是由UI組件開始,即使是這樣,有一個後臺線程創建UI元素(如Toast)可造成問題。

這可以與當前模型一起使用的唯一方法是將IntentService更改爲Service。默認情況下,在主(UI)線程上執行Service的代碼執行,並且Service顯示Toast是合法的,無論是否顯示任何應用的Activities。實際上,我修改了代碼以使用Service並將onHandleIntent中的代碼放在onStartCommand()中,我可以在撥打電話時看到Toasts

+0

嗯...我已經添加到我的清單文件: http://paste.ideaslabs.com/show/iyI2uUcKBz 但不知道如何做你說的其餘部分...是錯的我做過的方式? – fouadalnoor 2012-07-10 23:19:53

+0

您在清單中的''元素中添加了一個''節,該元素用於偵聽'NEW_OUTGOING_CALL'。我建議使用自己的自定義意圖操作「DO_SOMETHING」,併爲您的''元素添加一個''來爲其「偵聽」。然後,使用「DO_SOMETHING」動作啓動「IntentService」。 – Squonk 2012-07-10 23:35:21

+0

不......沒什麼。 新的清單文件,接收器和服務代碼是在這裏:http://paste.ideaslabs.com/show/AQInRptOhN 找不出我做錯了什麼:( – fouadalnoor 2012-07-11 17:53:22