1

我開發了一個應用程序來監控每分鐘後的網絡狀態....我使用Service組件來監視網絡狀態並將網絡詳細信息存儲到Database.I正在調用服務類從一個使用AlarmManager類的廣播接收器....我重複AlarmManager以便在每分鐘後調用服務類......但不幸的是,服務類沒有在每分鐘後正確調用......而是被調用一段時間後,它就會停止......我重視這裏的源代碼....使用Alarm Manager調用服務類

請讓我知道如何在每分鐘後使用AlarmMagerclass後臺持續運行服務類...

在此先感謝.. ;-)

我的服務類

public class NetworkInfoService extends Service implements LocationListener{ 

    private String strNetworkInfo; 
    private LocationManager locationManager; 
    private NetworkDetailsVO networkDetailsVO = new NetworkDetailsVO(); 
    private NetworkInfoDatabaseHelper networkInfoDatabaseHelper; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 

     ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); 
     TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); 

     NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 

     if(networkInfo!=null && networkInfo.isConnected()){ 
      String networkState = getNetworkStateString(networkInfo.getState()); 
      String stateString = networkInfo.toString().replace(',', '\n'); 
      strNetworkInfo = String.format("Network Type: %s\n Network State: %s\n\n %s", 
        networkInfo.getTypeName(),networkState,stateString); 
     } 

     locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     /*locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 300f, this);*/ 

     telephonyManager.listen(new SignalStrengthDetector(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); 

     networkDetailsVO.setNetworkInfo(strNetworkInfo); 
     networkDetailsVO.setPhoneNumber(telephonyManager.getLine1Number()); 
     networkDetailsVO.setOperatorName(telephonyManager.getNetworkOperatorName()); 
     networkDetailsVO.setDateTime(new Date().toString()); 

     Log.i("Network DetailsFrom Service", networkInfo.toString()); 

     networkInfoDatabaseHelper = new NetworkInfoDatabaseHelper(getApplicationContext()); 
     networkInfoDatabaseHelper.saveRecord(networkDetailsVO); 

    } 

    @Override 
    public void onStart(Intent intent, int startId) { 

     ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); 
     TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE); 

     NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 

     if(networkInfo!=null && networkInfo.isConnected()){ 
      String networkState = getNetworkStateString(networkInfo.getState()); 
      String stateString = networkInfo.toString().replace(',', '\n'); 
      strNetworkInfo = String.format("Network Type: %s\n Network State: %s\n\n %s", 
        networkInfo.getTypeName(),networkState,stateString); 
     } 

     locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); 
     /*locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 10000, 300f, this);*/ 

     telephonyManager.listen(new SignalStrengthDetector(), PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); 

     networkDetailsVO.setNetworkInfo(strNetworkInfo); 
     networkDetailsVO.setPhoneNumber(telephonyManager.getLine1Number()); 
     networkDetailsVO.setOperatorName(telephonyManager.getNetworkOperatorName()); 
     networkDetailsVO.setDateTime(new Date().toString()); 

     Log.i("Network DetailsFrom Service", networkInfo.toString()); 

     networkInfoDatabaseHelper = new NetworkInfoDatabaseHelper(getApplicationContext()); 
     networkInfoDatabaseHelper.saveRecord(networkDetailsVO); 


    } 


    /* 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 

     return Service.START_STICKY; 
    }*/ 

    private String getNetworkStateString(NetworkInfo.State state){ 

     String stateString = "unknown"; 

     switch(state) 
     { 
     case CONNECTED: 
      stateString = "connected";    
      break; 

     case CONNECTING:   
      stateString = "connecting";  
      break; 

     case DISCONNECTED:  
      stateString = "disconnected"; 
      break; 

     case DISCONNECTING:  
      stateString = "disconnecting"; 
      break; 

     case SUSPENDED:   
      stateString = "suspended";    
      break; 

     default: 
      stateString = "unknown";  

      break; 

     } 

     return stateString; 
    } 


    @Override 
    public void onLocationChanged(Location location) { 

     networkDetailsVO.setLatitude(location.getLatitude()); 
     networkDetailsVO.setLongitude(location.getLongitude()); 
     networkDetailsVO.setAccuracy(location.getAccuracy()); 
    } 


    @Override 
    public IBinder onBind(Intent intent) { 

     return null; 
    } 


    @Override 
    public void onProviderDisabled(String string) { 


    } 

    @Override 
    public void onProviderEnabled(String provider) { 

     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, this); 
    } 

    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 


    } 


    private class SignalStrengthDetector extends PhoneStateListener{ 

     @Override 
     public void onSignalStrengthsChanged(SignalStrength signalStrength) { 
      super.onSignalStrengthsChanged(signalStrength); 
      networkDetailsVO.setSignalStrength(signalStrength.getGsmSignalStrength()); 
     } 

    } 




} 

我的廣播接收器類(它註冊爲android.intent.action.BOOT_COMPLETED意圖)

public class NetworkInfoListner extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 
     PendingIntent service = null; 
     Intent intentForService = new Intent(context.getApplicationContext(), NetworkInfoService.class); 
     final AlarmManager alarmManager = (AlarmManager) context 
         .getSystemService(Context.ALARM_SERVICE); 
     final Calendar time = Calendar.getInstance(); 
     time.set(Calendar.MINUTE, 0); 
     time.set(Calendar.SECOND, 0); 
     time.set(Calendar.MILLISECOND, 0); 
     if (service == null) { 
     service = PendingIntent.getService(context, 0, 
          intentForService, PendingIntent.FLAG_CANCEL_CURRENT); 
       } 

       alarmManager.setRepeating(AlarmManager.RTC, time.getTime() 
         .getTime(), 60000, service); 

    } 

我清單文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.example.networkinfodemo_v_03" 
     android:versionCode="1" 
     android:versionName="1.0" > 

     <uses-sdk 
      android:minSdkVersion="8" 
      android:targetSdkVersion="17" /> 

     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
     <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> 
     <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 

     <application 
      android:allowBackup="true" 
      android:icon="@drawable/ic_launcher" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme" > 
      <activity 
       android:name="com.example.networkinfodemo_v_03.activity.NetworkInfoActivity" 
       android:configChanges="orientation|keyboardHidden|screenSize" 
       android:label="@string/app_name" 
       android:theme="@style/FullscreenTheme" > 
       <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 

        <category android:name="android.intent.category.LAUNCHER" /> 
       </intent-filter> 
      </activity> 

      <service android:name="com.example.networkinfodemo_v_03.service.NetworkInfoService" android:enabled="true"> 
      </service> 

      <receiver android:name="com.example.networkinfodemo_v_03.broadcastreceiver.NetworkInfoListner" > 
       <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       </intent-filter> 
      </receiver> 
     </application> 
    </manifest> 

請讓我知道什麼是錯誤...

回答

0

試試這個

Calendar cal = Calendar.getInstance(); 
cal.add(Calendar.SECOND, 60); 

service.setInexactRepeating(AlarmManager.RTC_WAKEUP, 
     cal.getTimeInMillis(), 60000, pending); 
+0

嗨SRIKANTH,我想......遺憾的是沒有運氣:-( –

+0

您好,我得到了解決使用IntentService而不是服務... –

+1

確定... 。但我在我的項目中使用Service類實現了類似的需求:) –

相關問題