【问题标题】:AlarmManager Does Not Start ServiceAlarmManager 不启动服务
【发布时间】:2013-06-19 03:40:48
【问题描述】:

我已经构建了一个应用程序,它应该以指定的时间间隔发送包含数据使用情况的 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);

    }

}

【问题讨论】:

  • 你为什么不使用 [setRepeating](developer.android.com/reference/android/app/…, long, long, android.app.PendingIntent)) 而不是 set()?
  • 有什么优势?
  • setRepeating() 在给定的时间间隔内运行 AlarmManager。也许我错过了一些东西,但就目前而言,我看不到你告诉它每分钟运行的地方
  • cal.add(Calendar.MINUTE, 1);调用警报(cal.getTimeInMillis(),rowId); ...是我试图指定间隔的地方 - 但这可能是我出错的地方。
  • 我不小心编辑了你的而不是我的,但又改回来了。试试我现在在最后一行的内容

标签: java android service calendar alarmmanager


【解决方案1】:

尝试改变

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

您可能还需要创建PendingIntent 以在am 中使用

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

【讨论】:

  • 当我这样做时导致了 2 个新错误:令牌“PendingIntent”上的语法错误,删除此令牌无法在原始类型 long 上调用 getService(WifiMonitor, int, Intent, int)
  • 将 (int) System.currentTimeMillis() 更改为 int 而不是尝试投射。只需尝试使用 0
  • 这看起来正确吗? am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime PendingIntent.getService(this, int, 0);
  • 将PendingIntent.getService( this, int, 0);更改为PendingIntent.getService( this, 0, i, 0);
  • 所以我把它改成了:am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime PendingIntent.getService(this, 0, i, 0); 但最后是:方法 setRepeating(int, long AlarmManager 类型中的 , long, PendingIntent) 不适用于参数 (int, long, long) 语法错误,插入“;”完成 Statement 语法错误,插入“)”完成 MethodInvocation
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-07
  • 1970-01-01
  • 2011-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多