I create a notificaion but my app show notification everytime in the precise moment i kill the app in background after the time I have set, for example if I set time to 4 pm, the notification will be show at 4pm, after 4pm if I kill the app the notification pop up, how I can fix this problem? Please help me I try everything :(
ScarsdaleHome.java
public class ScarsdaleHome extends Fragment implements OnClickListener{
Database d;
private AlarmManager alarmManager;
private PendingIntent pendingIntent;
public ScarsdaleHome() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.scarsdale_home_activity, container, false);
d=new Database(getActivity());
String dataString=d.checkDataString();
int start = dataString.indexOf("/");
String dayString = dataString.substring(0,start);
String monthString = dataString.substring(start + 1,start+3);
String yearString = dataString.substring(start+4);
int day = Integer.parseInt(dayString);
int month = Integer.parseInt(monthString);
int year = Integer.parseInt(yearString);
Calendar calend = Calendar.getInstance();
calend.set(Calendar.DAY_OF_MONTH,day);
calend.set(Calendar.MONTH,month-1);
calend.set(Calendar.YEAR,year);
calend.set(Calendar.HOUR_OF_DAY, 15);
calend.set(Calendar.MINUTE, 25);
calend.set(Calendar.SECOND, 0);
Intent myIntent = new Intent(getActivity(), MyReceiver.class);
if(PendingIntent.getBroadcast(getActivity(), 0,
myIntent,
PendingIntent.FLAG_NO_CREATE) == null){
pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, myIntent,0);
alarmManager = (AlarmManager)getActivity().getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calend.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
return rootView;
}
public static ScarsdaleHome newIstance(){
ScarsdaleHome frag= new ScarsdaleHome();
return frag;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
MyReceiver.java
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Date changed", Toast.LENGTH_SHORT).show();
Intent service1 = new Intent(context, MyAlarmService.class);
context.startService(service1);
}
}
MyAlarmService.java
public class MyAlarmService 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", "deprecation" })
@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(),ScarsdaleHome.class);
Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", 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(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
mManager.notify(0, notification);
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
super.onDestroy();
}
}
0 comments:
Post a Comment