0

我已经构建了一个应用程序,它应该以指定的时间间隔发送包含数据使用情况的 SMS - 在应用程序首次启动时发送一次 - 然后每分钟再一次(为了测试目的,这已缩短为一分钟)但是只有初始发送了短信,因此警报似乎永远不会过期并启动我的意图。

附言

在使用 CodeMagic 进行广泛测试后 - 我们仍然无法让警报过期并启动服务。

有什么建议么?

资源:

public class WifiMonitor extends Activity {

    Button sendButton;

    EditText msgTextField;

    private PendingIntent pendingIntent;

    private Date myDate;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView infoView = (TextView) findViewById(R.id.traffic_info);


        // 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("#.##");
        String totalStr = nf.format(totalBytes);
        String mobileStr = nf.format(mobileBytes);
        String info = String.format(
                "Wifi Data Usage: %s MB\tMobile Data Usage: %s MB", totalStr,
                mobileStr);
        infoView.setText(info);

        // send traffic info via sms
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("7865555555", null, info, null, null);
        String alarm = Context.ALARM_SERVICE;

        // get the current date
        Date date = new Date(System.currentTimeMillis());

        // convert the date to milliseconds
        long millis = date.getTime();

        // save the date to shared preferences
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());

        myDate = new Date(prefs.getLong("time", 0));
    }

    // set the alarm to expire 30 days from the date stored in sharePreferences


    public void invokeAlarm(long invokeTime, long rowId) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(myDate);

        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent i = new Intent(this, Alarm.class);
        i.putExtra("rowId", String.valueOf(rowId));
        long waitTime = 1000*10*1;
                am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime, PendingIntent.getService(
                        this, 0, i, 0));
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime, pi);


    }

}

警报:

public class Alarm extends Service {


    // compat to support older devices
    @Override
    public void onStart(Intent intent, int startId) {
        onStartCommand(intent, 0, startId);
    }

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

        // check to ensure everything is functioning

        Toast toast = Toast.makeText(this, "WiFi Usage Sent", 2000);
        toast.show();

        // send SMS
        String sms = "";
        sms += ("\tWifi Data Usage: "
                + (TrafficStats.getTotalRxBytes()
                        + TrafficStats.getTotalTxBytes() - (TrafficStats
                        .getMobileRxBytes() + TrafficStats.getMobileTxBytes()))
                / 1000000 + " MB");

        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("7865555555", null, sms, null, null);

        return START_STICKY;
    }

    @Override
    public void onCreate() {

        // TODO Auto-generated method stub
    }

    @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);

    }

}

在 codeMagic 的最新响应之后尝试的方法:

    // set the alarm to execute the service

    public void invokeAlarm(long invokeTime, long rowId) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(myDate);

        AlarmManager am = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        Intent i = new Intent(getApplicationContext(), Alarm.class);
        i.putExtra("rowId", String.valueOf(rowId));
        long waitTime = 1000 * 10 * 1;
        am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime,
                PendingIntent.getService(this, 0, i, 0));
        PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
        try {
            am.cancel(pendingIntent);
        } catch (Exception e) {

        }
        int timeForAlarm=10000;
        am.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+timeForAlarm, timeForAlarm,pendingIntent);

    }

}
4

1 回答 1

0

尝试改变

am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(
            this, (int) System.currentTimeMillis(), i, 0));

long waitTime = 1000*60*1
am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime PendingIntent.getService(
            this, (int) System.currentTimeMillis(), i, 0));

cal.add(Calendar.MINUTE, 1);只需在当前cal值上增加 1 分钟。而且我不确定是否有任何理由invokeAlarm()在自身内部调用,除非我完全错过了你正在做的事情

am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime, PendingIntent.getService(
        this, 0, i, 0));

您可能还需要创建PendingIntentam

PendingIntent pi = PendingIntent.getService( this, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime, pi);
于 2013-06-18T15:24:44.567 回答