1

毎分ごとにネットワークの状態を監視するアプリケーションを開発しました....サービスコンポーネントを使用してネットワークの状態を監視し、ネットワークの詳細をデータベースに保存しています.AlarmManagerクラスを使用してブロードキャストレシーバーからサービスクラスを呼び出しています.. ..毎分ごとにサービスクラスを呼び出すためにAlarmManagerを繰り返しています....しかし、残念ながらサービスクラスは毎分正確に呼び出されません...代わりに、しばらくの間呼び出されて停止します.. . ここにソースコードを添付しました....

毎分ごとに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());
        }

    }




}

私の Broadcast Receiver クラス (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>

エラーの内容を教えてください...

4

1 に答える 1

0

これを試して

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

service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
        cal.getTimeInMillis(), 60000, pending);
于 2013-04-02T09:41:49.397 に答える