3

I am using a TimePicker to get a specific time from the user. I am then using this time to set a repeating alarm at this time every day. When the alarm goes off i want a Notification to go be sent to user. My code seems to be right and i am not getting any errors in android studio, but when i run this app and set it at a specific time...IT NEVER GOES OFF. please help. Also i haven't been able to find anything that shows me how to get the AM or PM user selection with the TimePicker. My code is below. Thanks in advance.

Here is MyActivity ( the one that opens upon launch )

public class MyActivity extends Activity {

TimePicker mTimePicker;
Button setAlarm;
private int hour;
private int minute;
PendingIntent mPendingIntent;
int AM_PM;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);



    setAlarm = (Button) findViewById(R.id.setUpAlarm);


    mTimePicker = (TimePicker) findViewById(R.id.timePicker);

    setAlarm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            setAlarm();



        }
    });



}

private void setAlarm() {
    hour = mTimePicker.getCurrentHour();
    minute = mTimePicker.getCurrentMinute();

    Intent intent = new Intent(this, NotifyService.class);
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    mPendingIntent = PendingIntent.getService(this, 0, intent, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.SECOND , 0 );
    calendar.set(Calendar.MINUTE , 0 + minute);
    calendar.set(Calendar.HOUR , 0 + hour);
    calendar.set(Calendar.AM_PM , Calendar.PM);


    Toast.makeText(this, calendar.get(Calendar.MINUTE) + "    " + calendar.get(Calendar.HOUR), Toast.LENGTH_SHORT).show();

    //  * 60 * 60 * 24

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP , calendar.getTimeInMillis() , 1000 * 60 * 60 * 24 , mPendingIntent);

    // Toast.makeText(MyActivity.this , "Alarm Set" , Toast.LENGTH_SHORT).show();
}

Here is my notification class

public class NotifyService extends Service {
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onCreate() {

    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    NotificationManager mNM = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Intent intent = new Intent(this.getApplicationContext() , MyActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent ,0 );

    Notification mNotify = new Notification.Builder(this)
            .setContentTitle("Come Back!")
            .setContentText("Have you seen todays tip?")
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentIntent(pendingIntent)
            .setSound(sound)
            .build();

    mNM.notify( 1 , mNotify);

}

}

My manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidy.notificationapp" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Wildblue09
  • 337
  • 1
  • 3
  • 13

1 Answers1

3

You have to declare your Service in the manifest or it can't be started.

Also getCurrentHour() always returns the hour in 24h format, no need to know if the user entered in AM or PM.

http://developer.android.com/reference/android/widget/TimePicker.html#setCurrentHour(java.lang.Integer)

You also might want to consider to move all the work in your Service from onCreate() to onStartCommand() because onCreate() is only called if your Service doesn't exist yet, which it might, because you don't seem to stop it (which you might also consider doing).

http://developer.android.com/guide/components/services.html

ci_
  • 8,594
  • 10
  • 39
  • 63
  • would i declare my service in my manifest like @ci_ – Wildblue09 Mar 30 '15 at 22:06
  • And what would i return if i moved my Service to onStartComand? – Wildblue09 Mar 30 '15 at 22:07
  • You return one of the constants defined here http://developer.android.com/reference/android/app/Service.html#onStartCommand(android.content.Intent, int, int) or return a call to super for the default. If you follow the link in the answer it'll also show how to declare a service in the manifest. – ci_ Mar 30 '15 at 22:13