我已经构建了一个应用程序,该应用程序应该以指定的时间间隔发送包含数据使用情况的SMS - 一旦首次启动应用程序时 - 然后每分钟再次发送一次(为了测试目的,这已被缩短为一分钟)但是只发送了初始短信,所以似乎警报永远不会到期并启动我的意图。
P.S。
在使用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);
}
}
ALARM:
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);
}
}
答案 0 :(得分: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);
只需将1分钟添加到当前cal
值即可。而且我不确定是否有任何理由在我自己内部调用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);