1
\$\begingroup\$

Here is AlarmService for setting up notifications on my phone. Currently, the code works and is functional. However, I noticed my code looks different than a few examples I found online. Should I be using Notification Compact builder instead?

Also, any advice on how to make it better/cleaner would be great.

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class AlarmService extends Service
{
 private NotificationManager mManager;
 @Override
 public IBinder onBind(Intent arg0)
 {
 // TODO Auto-generated method stub
 return null;
 }
 @Override
 public void onCreate()
 {
 // TODO Auto-generated method stub
 super.onCreate();
 }
 @SuppressWarnings("static-access")
 @Override
 public void onStart(Intent intent, int startId)
 {
 super.onStart(intent, startId);
 mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
 Intent intent1 = new Intent(this.getApplicationContext(),ScoreCollectionPager.class);
 Notification notification = new Notification(R.drawable.clock,"Score Collection!", System.currentTimeMillis());
 intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
 PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
 notification.flags |= Notification.FLAG_AUTO_CANCEL;
 notification.setLatestEventInfo(this.getApplicationContext(), "Score Collection!", "Please enter scores at this time!", pendingNotificationIntent);
 //Default notification alerts to vibrate and make a notification sound
 notification.defaults |= Notification.DEFAULT_VIBRATE;
 notification.defaults |= Notification.DEFAULT_SOUND;
 mManager.notify(0, notification);
 }
 @Override
 public void onDestroy()
 {
 // TODO Auto-generated method stub
 super.onDestroy();
 }
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Oct 17, 2014 at 16:18
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

I don't know much about Android, so I can't answer that specific question, but there's still some things to change.

 @Override
public IBinder onBind(Intent arg0)
{
 // TODO Auto-generated method stub
 return null;
}
@Override
public void onCreate()
{
 // TODO Auto-generated method stub
 super.onCreate();
}
@Override
public void onDestroy()
{
 // TODO Auto-generated method stub
 super.onDestroy();
}

It looks like your IDE generated these for you, but you don't need them. Get rid of them, they're just clutter.

PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);

Ouch! In Java, we put one space after each comma in the argument list, and sometimes spaces in expressions. This line should look like

PendingIntent pendingNotificationIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);

Also, if you put spaces around an operator, it should be one space on each side, so Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP should be Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP

notification.setLatestEventInfo(this.getApplicationContext(), "Score Collection!", "Please enter scores at this time!", pendingNotificationIntent);

If I recall correctly, on Android all user-facing strings should be handled by the OS so that you can localize them easily.

Other than that, this code looks pretty good.

answered Oct 29, 2014 at 18:33
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.