Android: notification in service not workiong
I want to fire a notification in certain times of a day. I am having this code but i never get the notification. Any ideas?
*Note: parts of the whole code are deleted. I use that parts to change texts on the widget (I have a widget for the app), and I am using the same variables declared in the code (month_tod and day_tod) and that part of the code is working fine.
public final class UpdateService extends Service {
@Override
public void onStart(Intent i开发者_JAVA百科ntent, int startId) {
final Calendar today = Calendar.getInstance();
int month_tod = today.get(Calendar.MONTH)+1;
int day_tod = today.get(Calendar.DAY_OF_MONTH);
int hour_tod = today.get(Calendar.HOUR);
int min_tod = today.get(Calendar.MINUTE);
if ((month_tod==7) && (day_tod==10) && (hour_tod==11) && (min_tod==29))
{
NotificationManager mNotManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Reminder";
Notification mNotification = new Notification(
R.drawable.icon,
MyText,
System.currentTimeMillis() );
String name;
name = "Demoname";
String MyNotifyTitle ="Don’t forget";
String MyNotifiyText = "Take your medicine";
Intent MyIntent = new Intent( getApplicationContext(), MainActivity.class );
MyIntent.putExtra("extendedTitle", MyNotifyTitle);
MyIntent.putExtra("extendedText" , MyNotifiyText);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent,PendingIntent.FLAG_CANCEL_CURRENT);
mNotification.setLatestEventInfo( getApplicationContext(),
MyNotifyTitle,
MyNotifiyText,
StartIntent);
int HELLO_ID = 1;
mNotManager.notify( HELLO_ID , mNotification );
}
}
public void onCreate(Bundle savedInstanceState) {
}
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
Where is your service started from ? You need to use the AlarmManager in order to start it each x seconds/minutes.
please try to move codes into onStartCommand(). According to android doc, onStart() is deprecated.
http://developer.android.com/reference/android/app/Service.html#onStart(android.content.Intent,%20int)
精彩评论