HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Building 30 day alarm

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
alarmdaybuilding

Problem

I'm building an alarm set to reoccur every 30 days. I was wondering if I could get a few extra sets of eyes to look things over and see if you can notice anything I may have done wrong. Any/all input is greatly appreciated!

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("7862611848", null, info, null, null);

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.getT

Solution

I'm not familiar with Android, so just some generic note:

-
calendar.set(Calendar.DAY_OF_WEEK, 0);


What is this line supposed to do? Javadoc of DAY_OF_WEEK says the following:


Field number for get and set indicating the day of the week. This field takes
values SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, and SATURDAY.

Unfortunately, none of the mentioned constants has 0 as value. (SUNDAY = 1, MONDAY = 2. ..., FRIDAY = 6) You should use on the constants above here for better clarity.

-
I'd move the Calendar creation and and modification to a named method where the name explains the purpose of the method. It would be easier just reading the name of the method to figure out what contains the created Calendar instance than reading through the calls of setter methods.

-
The alarm variable is unused. Remove it.

-
(AlarmManager) getSystemService(Context.ALARM_SERVICE) is duplicated in the code. You could use the same object and call it twice or create a getAlarmManager() method which does the casting too to remove the duplication.

-
Calendar calendar = Calendar.getInstance();
calendar = Calendar.getInstance();


Could be simply

Calendar calendar = Calendar.getInstance();


It's the same.

Code Snippets

calendar.set(Calendar.DAY_OF_WEEK, 0);
Calendar calendar = Calendar.getInstance();
calendar = Calendar.getInstance();
Calendar calendar = Calendar.getInstance();

Context

StackExchange Code Review Q#27482, answer score: 2

Revisions (0)

No revisions yet.