알람이 있습니다. 분당 한 번씩 실행되도록 예약되었지만 몇 밀리 초마다 실행됩니다 (분당 수백 개의 txt 메시지가 발생 함). 왜 그런지 모르겠다.분당 1 분 간격으로 알람이 몇 밀리 초마다 시작됩니다.
누구든지 문제를 발견 할 수 있습니까?
활동의 SOURCE :
public class WifiMonitor extends Activity {
Button sendButton;
EditText msgTextField;
private PendingIntent pendingIntent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView infoView = (TextView) findViewById(R.id.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(
"\tWifi Data Usage: %s MB\tMobile Data Usage, %s MB", totalStr,
mobileStr);
infoView.setText(info);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("7865555555", null, info, null, null);
if (info.length() > 0) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://wifiusage.atwebpages.com/receiver.php");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
nameValuePairs.add(new BasicNameValuePair("id", "12345"));
nameValuePairs.add(new BasicNameValuePair("message", info));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
Intent myIntent = new Intent(WifiMonitor.this, Alarm.class);
pendingIntent = PendingIntent.getService(WifiMonitor.this, 0,
myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
pendingIntent);
}
}
서비스 자료 제공 :
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) {
String alarm = Context.ALARM_SERVICE;
AlarmManager am = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, 0);
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 1);
calendar.set(Calendar.SECOND, 0);
Intent Aintent = new Intent("REFRESH_THIS");
PendingIntent pi = PendingIntent.getBroadcast(this, 0, Aintent, 0);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
// reschedule to check again tomorrow
Intent serviceIntent = new Intent(Alarm.this, Alarm.class);
PendingIntent restartServiceIntent = PendingIntent.getService(
Alarm.this, 0, serviceIntent, 0);
AlarmManager alarms = (AlarmManager) getSystemService(ALARM_SERVICE);
// cancel previous alarm
alarms.cancel(restartServiceIntent);
// schedule alarm for today + 1 day
calendar.add(Calendar.DATE, 1);
// schedule the alarm
alarms.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
restartServiceIntent);
// 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);
}
}
업데이트 : 라인에서 주석
- alarmManager.set (AlarmManager.RTC_WAKEUP,에 System.currentTimeMillis() pendingIntent); 따라서 더 이상 몇 밀리 초마다 발사하지 않습니다. 그러나 라인에 따라야한다고 생각하기 때문에 분당 한 번씩 시작하지는 않습니다 : calendar.set (Calendar.MINUTE, 1); (MINUTE, 1을 테스트 목적으로 설정하고 싶습니다. 나중에 확인한 후에 "DAY_OF_MONTH, 1"로 변경하십시오.
사용 **'ADB 쉘 dumpsys – CommonsWare