1

我不确定为什么,但我有一个服务计时器,它不会在冰淇淋三明治上重复,但在 jellybean 上完美运行(我已经在 6 台设备上测试过,它只在 jellybean 上正常运行)。

我不确定究竟是什么原因造成的。

资源:

public class DataCountService extends Service {  
    private Timer timer = new Timer();
    private long period;
    private long delayInterval;
    private long smsTimer; 
    private DeviceManagerUtilities dmUtilities = new DeviceManagerUtilities(); 
    private Bundle extras;


    @Override
    public void onCreate() { 
        Log.d(Constants.TAG, "Logging Service Created");
        // if development mode is enabled set timer to development interval
        if (!Config.DEVELOPMENT) {

            period = Constants.PERIOD;
            delayInterval = Constants.DELAY_INTERVAL;
            smsTimer = Constants.SMS_TIMER;

        } else {
            // if debug mode is enabled set timer to debug interval
            period = Constants.DEBUG_PERIOD;
            delayInterval = Constants.DEBUG_DELAY_INTERVAL;
            smsTimer = Constants.DEBUG_SMS_TIMER;

        } 

        startServiceTimer();

    }


    @SuppressWarnings("unused")
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(Constants.TAG, "Logging Service Started");

        extras = intent.getExtras();

        if (intent == null) {
            // Exit gracefully if service not started by intent
            Log.d(Constants.TAG, "Error: Null Intent");
        } else {

            if (extras != null) {

                if (extras.getString(Constants.DM_SMS_CONTENT).contains(
                        Constants.LOGGING_ENABLE_TAG + ";1")) {

                    // Set enabled/disabled status to enabled
                    SharedPreferences settings = getApplicationContext()
                            .getSharedPreferences(Constants.PREFS_NAME, 0);
                    Editor editor = settings.edit();
                    editor.putString(Constants.PREFS_KEY_SMS_ENABLE, "1");
                    editor.commit();
                    //Send logging SMS immediately
                    smsSend();
                }

                if (extras.getString(Constants.DM_SMS_CONTENT).contains(
                        Constants.LOGGING_ENABLE_TAG + ";0")) {

                    // Set enabled/disabled status to disabled
                    SharedPreferences settings = getApplicationContext()
                            .getSharedPreferences(Constants.PREFS_NAME, 0);
                    Editor editor = settings.edit();
                    editor.putString(Constants.PREFS_KEY_SMS_ENABLE, "0");
                    editor.commit();
                } 

            }
        }
        return START_STICKY;
    }


    private void startServiceTimer() {
        timer.schedule(new TimerTask() {
            public void run() { 

                smsSendCheck();

            }

        }, delayInterval, period);
    }


    //Check to ensure proper amount of time has lapsed
    private void smsSendCheck() {

        // Check enabled / disabled status
        SharedPreferences settings = getApplicationContext()
                .getSharedPreferences(Constants.PREFS_NAME, 0);
        Log.d(Constants.TAG,"Enable: " + settings.getString(Constants.PREFS_KEY_SMS_ENABLE, "1") + " Call: " + settings.getString(Constants.PREFS_KEY_CALL_MADE, "FALSE"));
        if (settings.getString(Constants.PREFS_KEY_SMS_ENABLE, "1").equals("1")
                && settings.getString(Constants.PREFS_KEY_CALL_MADE, "FALSE")
                .equals("TRUE")) {


            //Get current time 
            long currentTimeMillis = System.currentTimeMillis();

            // Get the timestamp from the last time an SMS was sent
            long smsTimeStamp = settings.getLong(Constants.PREFS_KEY_SMS_TIME_STAMP,
                    0);
            Log.d(Constants.TAG,"Current: " + currentTimeMillis + " Stamp: " + smsTimeStamp);

            // If an logging SMS has never been sent (ie phone first use)  do not sent an SMS 
            // until an initial logging period has expired
            if (smsTimeStamp == 0) { 
                smsTimeStamp = currentTimeMillis;
                Editor editor = settings.edit();
                editor.putLong(Constants.PREFS_KEY_SMS_TIME_STAMP, currentTimeMillis);
                editor.commit();
            }


            // compare the two values to check to ensure proper amount of time has lapsed
            if (currentTimeMillis - smsTimeStamp > smsTimer) {

                smsSend();
            }
        }
    }

    //Send data count SMS
    private void smsSend() {
        Log.d(Constants.TAG, "Sending SMS");
        SharedPreferences settings = getApplicationContext()
                .getSharedPreferences(Constants.PREFS_NAME, 0);
        final String newMdn = dmUtilities.swappedMdn(this);
        // Get Wifi and Mobile traffic info
        double totalBytes = (double) TrafficStats.getTotalRxBytes()
                + TrafficStats.getTotalTxBytes();
        double mobileBytes = TrafficStats.getMobileRxBytes()
                + TrafficStats.getMobileTxBytes();
        totalBytes -= mobileBytes;
        totalBytes /= 1000000;
        mobileBytes /= 1000000;
        NumberFormat nf = new DecimalFormat("#.###");

        // Get the date
        SimpleDateFormat s = new SimpleDateFormat("hh/mm/ss/MM/dd/yy",Locale.US);

        String tag = ";";
        String mobileStr = nf.format(mobileBytes);
        String totalStr = nf.format(totalBytes);
        String DToDevice = s.format(new Date()); 
        String info = String.format("USI%sCN%s,WN%s", tag + settings.getString("0", newMdn) + tag
                + DToDevice + tag, mobileStr,
                totalStr + settings.getString(Constants.PREFS_KEY_LAST_SMS, ""));

        String lastMonth = String.format(";" + "CO" + mobileStr + "," + "WO"
                + totalStr);

        // Save Network and Wifi data in sharedPreferences
        Editor editor = settings.edit();
        editor.putString(Constants.PREFS_KEY_LAST_SMS, lastMonth); 
        editor.putLong(Constants.PREFS_KEY_SMS_TIME_STAMP, System.currentTimeMillis());
        editor.commit();

        // Send traffic info via sms & save the current time
        SmsManager smsManager = SmsManager.getDefault();

        if (Config.DEVELOPMENT) {
            String shortCode = settings.getString(
                    Constants.PREFS_KEY_SHORT_CODE,
                    Constants.DEFAULT_SHORT_CODE);
            smsManager.sendTextMessage(shortCode, null, info, null,
                    null);

        } else {
            SmsManager ackSMS = SmsManager.getDefault();
            smsManager.sendTextMessage(Constants.DEFAULT_SHORT_CODE,
                    null, info, null, null);

        }
    }




    @Override
    public IBinder onBind(Intent intent) {

        // TODO Auto-generated method stub

        return null;

    }

    @Override
    public boolean onUnbind(Intent intent) {

        // TODO Auto-generated method stub

        return super.onUnbind(intent);

    }

}

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.provider.generic.clientprovisioninghandler"
    android:versionCode="7"
    android:versionName="d3.0" >

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.RECEIVE_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS" />
    <uses-permission android:name="android.permission.WRITE_APN_SETTINGS" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />
    <uses-permission android:name="com.provider.permission.WRITE_BRAND" />

    <permission
        android:name="com.provider.permission.DMOMACP"
        android:protectionLevel="signatureOrSystem" />

    <uses-sdk
        android:maxSdkVersion="15"
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

    <application
        android:icon="@drawable/rings"
        android:label="@string/app_name" >
        <receiver android:name="com.provider.generic.clientprovisioninghandler.DeviceManagerSMSIntentReceiver" >
            <intent-filter android:priority="2147483647" >
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.provider.generic.clientprovisioninghandler.DeviceManagerStartupIntentReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.provider.generic.clientprovisioninghandler.DeviceManagerCallActionReceiver">
            <intent-filter>
                <action android:name="android.intent.action.PHONE_STATE"/>
            </intent-filter> 
        </receiver>

        <service
            android:name="com.provider.generic.clientprovisioninghandler.DeviceManagerDmSmsService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.provider.generic.clientprovisioninghandler.dmsms" />
            </intent-filter>
        </service>
        <service
            android:name="com.provider.generic.clientprovisioninghandler.DeviceManagerDmOmacpService"
            android:protectionLevel="signatureOrSystem" >
            <intent-filter>
                <action android:name="com.provider.generic.clientprovisioninghandler.dmomacp" />
            </intent-filter>
        </service>
        <service
            android:name="com.provider.generic.clientprovisioninghandler.DataCountService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.provider.generic.clientprovisioninghandler.datacount" />
            </intent-filter>
        </service>


        <activity
            android:name="com.provider.generic.clientprovisioninghandler.DeviceManagerTest"
            android:label="@string/app_tester_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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



    </application>

</manifest>
4

0 回答 0