Java AlarmManager不启动服务

Java AlarmManager不启动服务,java,android,service,calendar,alarmmanager,Java,Android,Service,Calendar,Alarmmanager,我构建了一个应用程序,该应用程序应该以指定的时间间隔发送包含数据使用情况的SMS—在应用程序首次启动时发送一次—然后每分钟发送一次(仅出于测试目的,这已缩短为一分钟)然而,只有初始短信被发送,所以似乎警报永远不会过期并启动我的意图 附言 在使用CodeMagic进行了大量测试之后,我们仍然无法让警报过期并启动服务 有什么建议吗 资料来源: public class WifiMonitor extends Activity { Button sendButton; EditTe

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

    }

}
试着改变

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

cal.add(日历分钟,1)仅向当前
cal
值添加1分钟。我不确定是否有任何理由在其内部调用
invokeAlarm()
,除非我完全错过了您正在做的事情

am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime, PendingIntent.getService(
        this, 0, i, 0));
您可能还需要创建
pendingent
以在
am

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

为什么不使用[setRepeating](,long,long,android.app.pendingent))而不是
set()
?有什么好处?
setRepeating()
在给定的时间间隔内运行
AlarmManager
。也许我错过了一些东西,但就目前的情况而言,我看不出你在告诉它每分钟运行一次;invokeAlarm(cal.getTimeInMillis(),rowId)。。。这是我试图指定间隔的地方,但这可能是我出错的地方。我不小心编辑了你的,而不是我的,但又将其更改了回来。请尝试我在最后一行中所做的操作。当我这样做时,它导致了两个新错误:标记“PendingEvent”上的语法错误,删除此标记无法在原语类型longChange
(int)System.currentTimeMillis()
上调用getService(WifiMonitor,int,Intent,int),而不是尝试强制转换。试着用0这个看起来正确吗?setRepeating(AlarmManager.RTC_WAKEUP,invokeTime,waitTime PendingEvent.getService(this,int,0);将
PendingEvent.getService(this,int,0);
更改为
PendingEvent.getService(this,0,i,0);
因此我将其更改为:am.setRepeating(AlarmManager.RTC_WAKEUP,invokeTime,waitTime PendingEvent.getService(this,0,i,0);但最终的结果是:类型AlarmManager中的方法setRepeating(int,long,long,pendingContent)不适用于参数(int,long,long)语法错误,insert“;“要完成语句语法错误,insert”)”来完成方法调用
PendingIntent pi = PendingIntent.getService( this, 0, i, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, invokeTime, waitTime, pi);