2013-05-16 40 views
0

我有一個服務包括電話監聽狀態,並通過我的application.in版本2.3.3安卓TelephoneManager越來越空的服務

開始時,我退出申請電話經理越來越空,因此手機狀態偵聽器不起作用以下服務代碼任何想法?我在哪裏做錯了?

當我退出application.but時,服務不會銷燬NULL。在setCallListener()方法上設置電話管理器。

我有一個服務來初始化phonestatelistener。服務以兩種方式啓動:

1-by BOOT_COMPLETED receiver(工作正常的監聽程序不爲空,並捕獲調用)2 - 通過我的應用程序使用startService(new Intent(getApplicationContext(),MyPhoneStateListener.class));

問題是,當服務由我的應用程序啓動,然後我的應用程序在這種情況下完成我的聽衆不工作。我知道TelephoneManager =空值。我該如何提供使應用程序完成後服務意圖繼續?

public class MyPhoneStateListener extends Service{ 

    SmsBroadcastReceiver _smsbroadcast; 
    private Context context; 

    MyCustomStateListener myCustomStateListener; 
    TelephonyManager telephonymanager; 

    @Override 
    public IBinder onBind(Intent arg0) { 
     return null; 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) 
    { 
     setCallListener(); 
     _smsbroadcast=new SmsBroadcastReceiver(); 
     GetShieldState(); 


     return START_STICKY; 

    } 

    private void setCallListener() 
    { 

     try 
     { 
      if (telephonymanager==null) 
      {    
       telephonymanager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE); 
       myCustomStateListener = new MyCustomStateListener(context,telephonymanager); 
       telephonymanager.listen(myCustomStateListener, PhoneStateListener.LISTEN_CALL_STATE); 

      } 
     } 
     catch(Exception ex) 
     { 

     } 
    } 




    @Override 
    public void onCreate() 
    { 
     //Log.e("startservis","create"); 
     context=MyPhoneStateListener.this;  
     super.onCreate();  
    } 


    @Override 
    public void onDestroy() 
    { 
     Log.e("onDestroy","destroy");  
    } 



    @Override 
    public boolean onUnbind(Intent intent) 
    { 
     //Toast.makeText(getApplicationContext(),"unbind:", Toast.LENGTH_SHORT).show(); 
     return super.onUnbind(intent); 
    } 

} 

回答

0

我想你忘了給下列權限在AndroidManifest.xml文件

<uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
+0

我有它。我找到了解決方案。我使用System.exit(0)命令殺死了進程,2.3.3版本服務停止並重新創建,但沒有重新啓動。所以我的聽衆是空的。現在我完成改變了System.exit。我也設置了監聽器來創建服務的方法。 – user2331641