指定された間隔でデータ使用量を含む SMS を送信することになっているアプリケーションを構築しました - アプリケーションが最初に起動されたときに 1 回 - その後、1 分ごとに 1 回 (これは単にテスト目的で 1 分に短縮されました) ただし、最初のSMS が送信されるので、アラームが期限切れになることはなく、私の意図を起動しているようです。
PS
CodeMagic で広範なテストを行った後でも、Alarm を期限切れにしてサービスを開始することはできません。
助言がありますか?
ソース:
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);
}
}